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)
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.