user2456676
user2456676

Reputation:

How do I open a new activity with a button click?

I recently started coding in Eclipse and I haven't done much yet so this is more or less my first app. I'm trying to make my school scheudele, it's simple: first activity shows 5 buttons, each button leading to a new activity (monday - friday).

How would I make so that when I click a certain button a new activity (let's say monday) would pop up?

I've seen hundreds of these questions already asked and answered on here but I just don't get it. It's useless to copy & paste code from here if I still don't get what's going on. I know I have to create a new intent and buttonlistener but I just don't get it what for and what to do then.

Could someone explain it to me as detailed as you can how exactly switching between activities using buttons work and how to actually do it?

I have:

So how would I code button1 to switch from MainActivity.java to Monday.java?

Upvotes: 1

Views: 594

Answers (3)

Voicu
Voicu

Reputation: 17860

Start by adding android:onClick="onClick" to each of your buttons' XML elements. This will make your buttons execute the onClick method whenever an onClick event is triggered on them.

Then in your MainActivity class add the following method:

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        // Monday
       Intent intent = new Intent(MainActivity.this, Monday.class);
       startActivity(intent);
       break;
    case R.id.button2:
        // Tuesday
       Intent intent = new Intent(MainActivity.this, Tuesday.class);
       startActivity(intent);
       break;
    // the rest of the buttons go here
    default: Log.e("YourTAG", "Default in onClick hit!");
        break;
    }
}

So every time there is an onClick event on any of your five buttons, the onClick method above will execute with the argument representing the View you just clicked on.

Details regarging intents and how they work here

And as @Edward noted, don't forget to add your new activities in your AndroidManifest.xml file under the application element, such as:

<activity android:name=".Monday" android:label="@string/app_name"></activity>

Upvotes: 1

amrinder007
amrinder007

Reputation: 1475

OK. Lets say you have following button in your layout file:-

<Button
    android:id="@+id/my_btn"
    android:layout_width="55dp"
    android:layout_height="22dp"
    android:onClick="goToMonday"  // function name which will be in MainActivity.java
/>

Now in your MainActivity.java:-

public void goToMonday(View v){
    Intent monday_intent = new Intent(MainActivity.this, Monday.class);

    MainActivity.this.startActivity(monday_intent);
}

This is the way to change activities.

Upvotes: 0

Emil Adz
Emil Adz

Reputation: 41129

what you should do is to create an Intent that will fire your Activity you do that by this code:

startActivity(new Intent(YourCurrentActivity.this, Monday.class));

You will have to fire a different Intent on each button that will create the appropriate Activity. Of course don't forget to declare your Activitys in your manifest file.

Upvotes: 0

Related Questions