Reputation: 15
I cant seem to link more than one button on a page. The page basically has a heading, and 3 buttons below that which link to 3 different topics The code that i have tried (which i found on this site) was:
package com.ICTrevisionapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class topicstoquiz extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.topics);}
public void onClick(View v) {
{
Button clickedButton = (Button) v;
setContentView(0);
switch(clickedButton.getId())
{
case R.id.button2:
setContentView(R.layout.topic1);
Intent myIntent = new Intent (v.getContext(),topicstotopicone.class);
startActivityForResult(myIntent, 0);
break;
case R.id.button3:
setContentView(R.layout.topic2);
break;
}
}
}
The case part.
I have also tried the code:
Intent myIntent = new Intent (view.getContext(),topicstoquiz.class);
startActivityForResult(myIntent, 0);
but I can only seem to get It to link to one activity and only from one button.
Im probably doing this completely wrong, so how would I link each button on the page, to a seperate activity so I can link them to other pages. (If that makes sense)
Upvotes: 1
Views: 2876
Reputation: 2332
Button myButton = (Button) findViewById(R.id.my_button); myButton.setOnClickListener ( or something like that)
Upvotes: 0
Reputation: 4254
I prefer to go "the long way" about this:
In your onCreate:
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(yourListener);
Then create the listener method:
private OnClickListener yourListener = new OnClickListener(){
public void onClick(View v){
Intent yourIntent = new Intent(yourCurrentActivity.this, classYoureNavigatingToo.class);
startActivity(yourIntent);
}
};
So you can set up separate listeners for each button, pointing to a different class. If it helps don't forget to mark as answer!
Update:
public class topicstoquiz extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.topics);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
button2.setOnClickListener(button2Listener);
button3.setOnClickListener(button3Listener);
button4.setOnClickListener(button4Listener);
}
Set up your click listeners like I showed above. You are specifing your activity in your AndroidManifest.xml correct? Post stack trace result when you get the force close please.
Upvotes: 2
Reputation: 3911
You could also user the android:onClick Attribute which you set in your xml-layout for the corresponding buttons. This eliminates the need for switch-case completely.
android:onClick="onClickYourFirstButton"
In your activity class add the onClick method
public void onClickYouFirstButton(View v) {
http://developer.android.com/reference/android/view/View.html#attr_android%3aonClick
Upvotes: 1
Reputation: 1870
For multiple buttons I personally prefer a switch statement method similar to what you have done. First, impliment the OnClickListner into your activity then impliment the switch.
public class MainActivity extends Activity implements OnClickListener {...
then inside the activity:
public void onClick(View view) {
Intent i;
switch(view.getId()) {
case R.id.buttonOneId:
i = new Intent(getApplicationContext(), Activity1.class);
startActivity(i);
break;
case R.id.buttonTwoId:
i = new Intent(getApplicationContext(), Activity2.class);
startActivity(i);
break;
}
}
To enable that function to work with the buttons in the activity do this for each button:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(this);
alternative, the OnClickListner can be set in-line like so:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent i = new Intent(getApplicationContext(), Activity.class);
startActivity(i);
}
});
if using the in-line method, repeat the code for the other buttons.
Upvotes: 1