bryan
bryan

Reputation: 49

Android: Button action not being called

I'm trying to make an app where you start at a menu, click a button and are brought to a list of items (which I later hope to make clickable). But I can't seem to make it call my next activity. Can anyone help?

Upvotes: 0

Views: 100

Answers (2)

Selzier
Selzier

Reputation: 101

Your main class / activity:

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

// Menu Button
Button startNewActivity = (Button)findViewById(R.id.startnew);
startNewActivity.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Intent newActivityIntent = new Intent(YOUR-CLASS-NAME.this,NewActivity.class);
        startActivity(newActivityIntent);
    }
});

Your NewActivity Class:

public class NewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new);
    }

}

Upvotes: 2

Alexander Lucas
Alexander Lucas

Reputation: 22371

Is the question "How do I call the next activity" ?

If so, it's pretty easy - Assuming the Activity you want to call is "SomeActivity", call this:

Intent someActivity = new Intent(getBaseContext(), SomeActivity.class);
startActivity(someActivity);

There's also a "startActivityForResult" method, if you want data back from the Activity you're calling. For reference, the Activity page of the API Documentation can be found here. Good luck!

Upvotes: 1

Related Questions