This program is for me to learn how to design and code android program. I got prize winner idea program as I attended at Toronto Android Meet-Up meeting.In order to activate componets, we need Intents.
On this example, I wanted two components (textview,button) on main screen and add other three components (two textview, button) on the sub screen. I was wondering where I need to put java.util.random code.As you see the picture on left side, I created PrizeDemo project and two java classes (IntentA.java and IntentActionDemo.java).

I degined one text view and one button as I dragged and drop from the view on the left side menu on main.xml I created.
I also desinged another xml file named intentA.xml. Just dragged and drop the button and two text view componets.
Here are two class codes:
IntentActionDemo.java
package com.example.IntentActionDemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class IntentActionDemo extends Activity implements OnClickListener { /** Called when the activity is first created. */
// @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.intentButton);
button.setOnClickListener(this);
}
public void onClick(View src) {
Intent i = new Intent(this, IntentA.class);
startActivity(i);
}
}
IntentA.java
package com.example.IntentActionDemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import java.util.*;
public class IntentA extends Activity implements OnClickListener{
//@Override
public void onClick(View src) {
Intent i = new Intent(this, IntentActionDemo.class);
startActivity(i);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intenta);
Button button = (Button) findViewById(R.id.ButtonIntentA);
button.setOnClickListener(this);
java.util.Random r = new Random();
TextView t = (TextView) findViewById(R.id.TextView01);
t.setText("Przie Winner:"+r.nextInt(15));
}
}
Here is string.xml under res/values.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Choose Prize winner!</string>
<string name="app_name">Choose Prize Winner Program (Intent Action Demo)</string>
<string name="IntentA">To find Winner</string>
<string name="prize">Prize!</string>
<string name="Intent0">Try again!!</string>
</resources>
Here is Androidmanifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.IntentActionDemo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IntentActionDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="IntentA">
</activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>
Once I run the program, I can see the below pictures.
15 people attended on prize contest, Shwing one number on the screen will be the winner.