views:

82

answers:

2

Hi guys, I am a beginner in programming with Java Android.

I am currently confused with how to enable button to work in Android 2.1

My current project requires a lot of different activities to work together in one program.

Let's say I have abutton inside the main.xml and assume the function inside ButtonAdroid.class is the one below:

public class ButtonAndroid extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
    }
}

My goal is to make connection between the ButtonAndroid.class to another class, let's say its name is NextPage.java.

Do you guys know what kind of commands do I need to put inside "public void onClick(View v)" that will enable to transfer the current activity to NextPage.java?

Thank you so much.

+2  A: 

If I'm understanding you correctly and you want to move on to another activity that displays a different view, you need to do this by using Intent:

Intent i = new Intent(ButtonAndroid.this, NextPage.class);
startActivity(i);
Blumer
A: 

Hi Blumer,

After using your answer, apparently there is still an error. I have 2 classes that is named HelloRelativeLayout and HelloRelativeLayout2.

The error says, the application has stopped unexpectedly. Does this mean I have to add intent-filter kind of thing inside the XML?

public class HelloRelativeLayout extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button button = (Button) findViewById(R.id.signIn);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
         Intent i = new Intent(HelloRelativeLayout.this, HelloRelativeLayout2.class);
         startActivity(i);
        }
    });

}

}

Sammm