Launching a new Activity from a Toolbar icon

Android Toolbars: 1 2 3 4 5 6

In the previous lesson, you saw how to add code for items selected from a Toolbar. In this lesson, you'll learn how to start a new Activity for Toolbar menu items.

Instead of just displaying some text when a menu item is selected, you'll want to do something useful. Typically, you'll want to start a new activity. For example, we have a favourites menu item. When this item is selected, we might want to display images of our favourite people, or favourite cats, or favourite anything. All you do here is to create a new intent, just like you did in previous sections. Let's see an example.

Add a new Activity to your project by right-clicking on your app folder in the explorer on the left of Android Studio. From the menu that appears, select New > Android Empty Activity. Give it the name DisplayFavs:

Dialogue box to create a new Android Activity

Make sure Backwards Compatibility is checked, and click Finish to create your new Activity.

You can create a toolbar for your new Activity quit easily by copying the XML for the one you already have.

Click back on your activity_main.xml file. Make sure you are in Text view rather than Design view. Now highlight all the XML for the toolbar:

XML for an Android Toolbar

Copy this text and go to your new XML file, which should be called activity_display_favs.xml. Again on the Text tab, paste your toolbar XML:

Changing the android ID for a Toolbar

Notice the highlighted android:id at the top. You need to change this from my_toolbar to something else. Give it the ID favs_toolbar:

Android XML highlighting an ID change

Now click back to the Design tab to see your layout. It should look like this:

Android Activity with a Toollbar

Have a look at the Component Tree on the left and your new toolbar should be on the list:

Component tree in Android Studio showing a Toolbar added

(If it's not, close down your project and open it back up again.)

You can set the title property for toolbar, just like you did before. We've added a new string to our strings.xml file, in the image below, and set the text of the string to Fav Activity. The name of the string was second_activity.

You can attach the toolbar and menu to the second Activity in the same you did for the Main Activity. Add this code to the onCreate method of your DisplayFavs.java file (add it just after setContentView):

Toolbar aToolbar = (Toolbar) findViewById(R.id.favs_toolbar);
setSupportActionBar(aToolbar);

This time, we're using favs_toolbar after R.id. If you get any red text, press ALT + ENTER to add the correct library. Or add import android.support.v7.widget.Toolbar; to the top of your code.

Next, add your menu items by overriding the onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_items, menu);

return true;

}

(Press ALT + ENTER for any red text. Or add import android.view.Menu; to the top of your code.)

Your DisplayFavs code should look like this:

Java code for a second Android Activity

This second Activity won't have an up arrow, however. (The up arrow is the left pointing one in the toolbar that takes you back to the main screen.)

To get an up arrow, you need to make a call to the getSupportActionBar method. This can return an ActionBar object. Add this to your onCreate method, just below setSupportActionBar.

ActionBar myActionBar = getSupportActionBar();

You'll need an import line at the top of your code: import android.support.v7.app.ActionBar; (Or press ALT + ENTER on any red text as a shortcut.)

Once you've got an ActionBar object, you can set an up arrow with setDisplayHomeAsUpEnabled. It's a good idea to test if your ActionBar object isn't null, though. Add these lines then:

if ( myActionBar != null ) {

myActionBar.setDisplayHomeAsUpEnabled(true);

}

In between the round brackets of setDisplayHomeAsUpEnabled you only need a true or false.

Your DisplayFavs code should look like this:

Using setDisplayHomeAsUpEnabled in Java code

There's one more thing to do for the up arrow, however. If you remember, in a previous tutorial, we added a line to the Android Manifest file when we set an up arrow. We'll need to do the same again.

Open up your AndroidManifest.xml file, which is in the manifests folder in the explorer area to the left of Android Studio. Locate this line:

<activity android:name=".DisplayFavs"></activity>

Change it to this:

<activity android:name=".DisplayFavs" android:parentActivityName="MainActivity" >

As before, we've added a parentActivityName attribute, setting it to the MainActivity java file.

Because older devices might not recognise the parentActivityName attribute, you can add a meta-data tag. Add this line just below the one you've changed:

<meta-data

android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />

Your manifest file should look like this:

The Android MAnifest file showing amendments

OK, we're done with the AndroidManifest.xml file. You can close it down.

Go back to your MainActivity.java file. Your code for the menu item selection looks like this:

Java code for a Toolbar menu

We can start our new Activity from the favourites_page case. Comment out the line for displayTextView. Instead, we're going to create a new Intent. Add this code:

Intent favIntent = new Intent(this, DisplayFavs.class);
startActivity(favIntent);

(ALT + ENTER any red text, or add import android.content.Intent; to the top of your code.)

You've met this code before, in a previous section. It just creates a new Intent object and starts a new Activity. Your switch statement should look like ours:

A new Android Activity started from a menu item

Try it out. Run your app. Click the Favourites icon in the toolbar. You should see a new Activity start:

A running app showing a second Activity and UP arrow

Notice the up arrow on the left. Click or press this to get back to the main screen.

 

We can now write some code for the Share item on the toolbar. (We won't do the web toolbar item. You'll see how to add Web Views in a later tutorial.)