Options menu

Finally, we need to make the SettingsActivity available from the MainActivity. As we said before, we will use the options menu on the MainActivty for this.

First of all, let's create a menu.xml file under the app/src/main/res/menu directory. The structure of the menu will be set to the following:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings_black"
android:title="Settings" />
</menu>

This means that there will be just a single menu entry named settings. Also, we have added an icon for the entry:

@drawable/ic_settings_black

Usually, icons can be generated using Android Studio with File||New||Image Asset. This can be seen here:

Next, we will enable this menu in the MainActivity by adding menu initialization code in the MainActivity class:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

The last step is to handle the action when the menu option gets selected by having the following in the MainActivity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}

That's it; now we have a fully working SettingsActivity class. After the settings option is clicked on, the user should be presented with this screen:

When a user clicks on Symbols or Keywords entries, a dialog box will be shown where the Keywords or Symbols can be entered:

The values here are separated by spaces.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.26.221