• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to open different pages when different buttons are clicked

Hi All,

I am new to Android and I am trying to create an app in which the main xml form has around 4 to 5 buttons and when we click on each button each page should pop up and do an activity...Actually I tried for one button and it generated the required result but I am trying the same for 2nd button and so on.. How do I go about this.. Here is my file...


MainActivity.java


package com.example.twopg;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}

public void addListenerOnButton() {

final Context context = this;

button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent intent = new Intent(context, MainActivity1.class);
startActivity(intent);




}

});
}

}



This code worked fine and I got the page required at the click of one button, I wrote the code with button two to open up second page on the click of second button.. but now even the first button doesn't work

package com.example.twopg;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;
Button button1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
Button button1=(Button)findViewById(R.id.button2);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this,button.class);
startActivity(intent);

}
});


button1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Intent intent2 = new Intent(MainActivity.this,button1.class);
startActivity(intent2);

}
});
}
}


Can some one let me know how I can add the button2 and when its clicked it should open up an activity....

If u need my XML files and other Java files please let me know..
 
Back
Top Bottom