Basically I am gonna include 14 more buttons in this and all those will go to same activity with just an spinner change in the second activity. So I don’t wanna create an Intent object for every button to lead to same activity(since it slows down the app). So I assigned tags to button to get the id and tried to pass that id to the new activity. But its not working. The AVD shows only the first activity and nothing happens after clicking on the button.
My main screen(Test1) activity :
public class Test1Activity extends Activity {
public static final String pass="com.sanjay.test1._clicked";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button) findViewById(R.id.button1);
Button button2=(Button) findViewById(R.id.button2);
button1.setId(1);
button2.setId(2);
button1.setOnClickListener( new MyOnClickListener());
button2.setOnClickListener( new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v)
{
int id=v.getId();
Intent i=new Intent(Test1Activity.this,Sanjay.class);
i.putExtra(pass, id);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Length" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight" />
</LinearLayout>
the second class(Sanjay) Activity :
public class Sanjay extends Activity {
/** Called when the activity is first created. */
int a;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
text=(TextView) findViewById(R.id.text);
a=getIntent().getExtras().getInt(Test1Activity.pass);
text.setText(" "+a);
// TODO Auto-generated method stub
}
}
Second.xml :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sanjay.rotation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".RotationActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You need to add startActivity(i) in the onClick function if not the activity will never start. And you should add it into your Android Manifest too.
Like this:
And your manifest: