Home and Learn: Android Course
We've created a toolbar with icons in previous lessons. However, nothing happens when you tap the icons. We'd like to launch an activity when we tap an icon. To do that, you need to write some java code.
You'll need to do three things in your code: use the onCreate method to point to your toolbar, use the onCreateOptionsMenu method to attach your menu items to the toolbar, and 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 = findViewById(R.id.myToolbar);
If you get red flagged, locate the import lines at the top of the code. Add the following import:
import androidx.appcompat.widget.Toolbar;
Back to the code. The line gets a reference to your toolbar, the one on the activity_main.xml file. (The toolbar is called myToolbar. If you left yours on the default, go back and change the id attribute.)
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, myToolbar for us..
Next, you need to override onCreateOptionsMenu. Add this just after the onCreate method:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
(The lines above might not copy and paste well into Android Studio, If you get lots of red flags, delete the above code and type it out for yourself. Bit of a chore, but that's programming! If you get red flagged for Menu, click inside the text, then press ALT + Enter on your keyboard. Or add import android.view.Menu; to the other import lines at the top of the code.)
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:
You can try it out, now. Run your app. It will look like this in the Emulator:
We haven't added any code for menu item selection, so nothing will happen when you click on the items.
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 (ALT + ENTER to add the import line, or copy and paste thos to the top of your code: import android.widget.TextView;):
TextView displayTextView = (TextView) findViewById(R.id.displayText);
Next, we can set up an if 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:
if ( item.getItemId() == R.id.favourites_page ) {
displayTextView.setText(R.string.favs);
return true;
}
If the id of the favourites_page matches item.getItemId() then we set the text for the TextView using one of those strings we set up.
Now add some else if parts to your if statement. Add these:
else if (item.getItemId() == R.id.web_page) {
displayTextView.setText(R.string.web);
return true;
} else if (item.getItemId() == R.id.settings) {
displayTextView.setText(R.string.settings_page);
return true;
} else {
return super.onOptionsItemSelected(item);
}
The whole of you onOptionsItemSelected method should look like this:
The else part of our if statement returns a call to the super class for onOptionsItemSelected. This will take care of any errors.
Try it out. Run your app and click on your toolbar menu items. You should see some text appear in your display TextView area. In the next lesson, 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.
< Android Vector Assets | Menu Item - New Activity >
Back to the Android Contents Page
Email us: enquiry at homeandlearn.co.uk