I started a simple quiz application as below it consists of TextView to show the questions and a RadioGroup consisting of 3 RadioButtons for 3 options. When you finish the quiz it shows number of correct answers and number of wrong answers and your degree. But I have some problems as below Please help me to solve it.
the problems:
When i click on next question its transfer me to result activity not to next questions.
The Result activity page did not show the total result correctly
MainActivity
public class MainActivity extends ActionBarActivity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2,rb3;
String questions[]={"qqqqq","dddddd"};
String ans[]={"",""};
String opt[]={"","","","","",""};
int flag=0;
public static int marks,correct,wrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvque);
btnNext = (Button) findViewById(R.id.button1);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rb1 = (RadioButton) findViewById(R.id.radio0);
rb2 = (RadioButton) findViewById(R.id.radio1);
rb3 = (RadioButton) findViewById(R.id.radio2);
tv.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
Toast.makeText(this,"Nigative mark", 1000).show();
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText=uans.getText().toString();
if(ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<questions.length)
{
tv.setText(questions[flag]);
rb1.setText(opt[flag*3]);
rb2.setText(opt[(flag*3)+1]);
rb3.setText(opt[(flag*3)+2]);
}
else
{
marks=correct;
}
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
}
});
}
}
ResultActivity
public class ResultActivity extends ActionBarActivity {
TextView tv;
Button btRestart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv=(TextView) findViewById(R.id.textView1);
btRestart=(Button) findViewById(R.id.button1);
StringBuffer sb=new StringBuffer();
sb.append("Correct ANS:"+MainActivity.correct);
sb.append("Wrong ANS:"+MainActivity.wrong);
sb.append("Final Score:"+MainActivity.marks);
tv.setText(sb);
MainActivity.correct=0;
MainActivity.wrong=0;
btRestart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
}
When i click on next question its transfer me to result activity not to next questions.
You are telling it to go to the ResultsActivity in your Intent
// 2nd param is the target Activity
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
To fix that, you would need to use the name of the Activity where your next question is to replace ResultActivity. Unless you store them in an Array or other type of list then you could just replace the question TextView text with the next question.
The Result activity page did not show the total result correctly
This is nearly impossible to answer without knowing your expected and actual results.
Related
I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.
i have a one layout and 3 buttons and textView , when i cick a button it change the textView , button3 i have a song to play and textView show lyrics, and my problem here is how to make the MediaPlayer stopping when click any other button
here is my code i used :
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles= MediaPlayer.create(Langue.this, R.raw.iles);
iles.start();
}
});
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final MediaPlayer iles= MediaPlayer.create(Langue.this, R.raw.iles);//creating iles here
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles.start();
}
});
I believe this will do., in the above code i created a iles as MediaPlayer object and used it to stop the song...
so a quick question. In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. Here is what I have
First activity
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle = getIntent().getExtras();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second activity
Button btn;
RadioGroup rg2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz2);
btn = (Button) findViewById(R.id.nextBtn2);
rg2= (RadioGroup) findViewById(R.id.rg2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz3.class);
Bundle bundle = getIntent().getExtras();
int id = rg2.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg2", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
This continues for about 7 activities
Final activity (where the result and the character are shown)
public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}
Now, in the final activity, I want to have a code where if for example:
if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected),
Quiz2: options 2 or 4
Quiz3: options 1 or 4
Quiz2: options 2 or 3
and so on...
then change a textview to something specific like "your character is x"
I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple.
Any help would be appreciated, thank you <3
EDIT:
TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
&& bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
|| bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
&& bundle.getString("rg6").equals("Yes"))) {
textviewResult.setText("x");
}else{
textviewResult.setText("not x");
}
The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x)
You can use the String equals() method inside your if statement. It returns true if both Strings are equal.
E.g.:
if(bundle.getString("rg").equals("YourRadioButtonText")){
...
}
Your code could look something like this:
`public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
// NEW
TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
textviewResult.setText("It is not A");
}
else {
textviewResult.setText("Mhhh");
}
// END
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}`
I am trying to make a quiz where users choose an answer in each activity, and the final page provides an answer based on the chosen options. Therefore, I want to pass these values to the final activity, but only the last chosen value seems to show.
First Activity:
public class Quiz extends Activity {
Button btn;
RadioGroup rg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
btn = (Button) findViewById(R.id.nextBtn);
rg= (RadioGroup) findViewById(R.id.rg);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz1.class);
Bundle bundle = new Bundle();
int id = rg.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second Activity:
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle1 = new Bundle();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle1.putString("rg1", radioButton.getText().toString());
intent1.putExtras(bundle1);
startActivity(intent1);
}
}
});
}
}
Now this follows for a total of 7 activities.. not including the final activity
Here is the 7ths (called Quiz6) activity:
public class Quiz6 extends Activity {
Button btn;
RadioGroup rg6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz6);
btn = (Button) findViewById(R.id.nextBtn6);
rg6= (RadioGroup) findViewById(R.id.rg6);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg6.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent6 = new Intent(getApplicationContext(), Final1.class);
Bundle bundle6 = new Bundle();
int id = rg6.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle6.putString("rg6", radioButton.getText().toString());
intent6.putExtras(bundle6);
startActivity(intent6);
}
}
});
}
}
You get the idea :)
Here is the FINAL activity (called Final1) here the results are show
here is the code
public class Final1 extends Activity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
Bundle bundle1 = getIntent().getExtras();
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView.setText(bundle1.getCharSequence("rg1"));
Bundle bundle2 = getIntent().getExtras();
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView.setText(bundle2.getCharSequence("rg2"));
Bundle bundle3 = getIntent().getExtras();
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView.setText(bundle3.getCharSequence("rg3"));
Bundle bundle4 = getIntent().getExtras();
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView.setText(bundle4.getCharSequence("rg4"));
Bundle bundle5 = getIntent().getExtras();
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView.setText(bundle5.getCharSequence("rg5"));
Bundle bundle6 = getIntent().getExtras();
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView.setText(bundle6.getCharSequence("rg6"));
btn = (Button)findViewById(R.id.restartBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
}
}
What ends up happening once I run the program is that only "textView" ends up changing to the chosen choice on the activity right before the final activity, shown above
Any help is appreciated, thanks lots <3
An activity's intent only has one Bundle associated with it. Thus, you are only passing the last bundle to your final activity, the others are passed to the immediately following activity instead. You can fix this issue by doing
Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
instead of creating a new bundle each time as you do now. then pass this bundle along to the following activity. That way, each time you get a new answer, you are adding it to your bundle of answers, rather than creating a new bundle with only one item each time.
Then in your final activity, you only have to get the one bundle, and can pull all the answers out of it. NOTE: You'll want to make sure that your'e updating the correct textview with the correct text. In your question, textView is the only view that gets updated, 6 times, and the others do not get updated at all.
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}