Java code for an Android Toolbar

Android Toolbars: 1 2 3 4 5 6

You'll need to do three things in your code: use onCreate to point to your toolbar, use the onCreateOptionsMenu method to attach your menu items to the toolbar, use onOptionsItemSelected to get which menu item was selected.

Open up your MainActivity.java file. Just after setContentView in the onCreate method, add this line:

Toolbar myToolbar = (Toolbar) findViewById(R.id.myToolbar);

This line gets a reference to your toolbar, the one on the activity_main.xml file. Now add this line:

setSupportActionBar(myToolbar);

With the setSupportActionBar method, you're setting up support for your toolbar so that it can be used on a wide range of devices. In between the round brackets of setSupportActionBar, you type the name of your toolbar.

Next, you need to override onCreateOptionsMenu. Add this just after the onCreate method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

return true;

}

The only line you need to add (before return true) for the onCreateOptionsMenu method is this one:

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

What you're doing here is inflating the menu you created, the one called menu_items. This will attach your menu to the toolbar. Your java code should look like this:

Java code to set up an Android Toolbar and menu items

You can try it out, now. Run your app. It will look like this in the Emulator:

Android Emulator showing an app with a Toolbar

Click on the three dots, the Overflow Menu. You should see a sub menu appear:

The Overflow menu on Android

We haven't added any code for menu item selection, so nothing will happen when you click on the items. But rotate your Emulator or device. You should see this:

The Share item in Landscape view

Notice that we have a new icon, just to the right of the smiley-face, in the image above. This is the Share icon, which is an inbuilt Android one. You get that because you used ShareActionProvider in the XML for your menu items. As the showAsAction property, we used ifRoom. There's no room for the Share icon in portrait view, but in landscape view there is plenty of room to display the icon. For the Settings item, however, we used never as the showAsAction property. This means that the Settings item will never appear on the toolbar as an icon but only in the Overflow menu.

Stop your app from running and we'll add code to detect if a menu item was selected.

To check which menu item was selected, you can override the onOptionsItemSelected method. Add the following method to your code, just after your onCreateOptionsMenu method.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

}

If you get any red text, press ALT + ENTER on your keyboard to add a reference to the correct library. Or just type import android.view.MenuItem; at the top of your code.

We haven't added a return statement yet, so you'll probably get errors in your code.

First, we can get a reference to the TextView on the layout:

TextView displayTextView = (TextView) findViewById(R.id.displayText);

Next, we can set up an switch statement. This will examine the item variable that is between the round brackets of onOptionsItemSelected. The item variable is a MenuItem type. You can use getItemId on it to see which menu item was selected:

switch ( item.getItemId( ) ) {

}

You can then check if the item id matches one of your menu item IDs.

switch (item.getItemId()) {

case R.id.favourites_page:

displayTextView.setText(R.string.favs);

return true;

}

Inside of the switch statement, we have this:

case R.id.favourites_page:

As soon as you type a dot after R.id you'll see a menu appear. This one:

Context menu in Android Studio

On the list are the IDs you set up for your menu items. Select the favourites_page ID.

The only thing we want to happen when this menu item is selected is to display some text in our TextView. We do it like this:

displayTextView.setText(R.string.favs);

In between the round brackets of setText, you can grab one of those strings you set up in your strings.xml file:

R.string.favs

In fact, you should a list of them appear, when you type the dot after string:

Context menu showing string resources

Select favs from the list.

The only other thing in the case part of the switch statement is return true.

Now add three more case parts, just under the first one:

case R.id.web_page:

displayTextView.setText(R.string.web);

return true;

case R.id.settings:

displayTextView.setText(R.string.settings_page);

return true;

case R.id.action_share:

displayTextView.setText(R.string.share_this);

return true;

There is one case for every item we added to our menu. The only thing we need is a default case for our switch statement. Add this, just under the final case:

default:

return super.onOptionsItemSelected(item);

This returns a call to the super class for onOptionsItemSelected.

The whole of your onOptionsItemSelected method should look like this:

Java code for the Android method onOptionsItemSelected

And the whole of your code should look like this:

Android project code for a Toolbar

Try it out. Run your app and click on your toolbar menu items. You should see some text appear in your display TextView area. The only time you won't see any text appear is when you rotate the device and click on the inbuilt Share icon. This icon doesn't register with the onOptionsItemSelected method. You'll see how to fix that soon. Before doing that, we'll add an new Activity to the project. You'll learn how to launch this new Activity when an icon is clicked or tapped.