In the world of Android development, there are many ways to navigate through an app. One of the most common ways is through a menu. Menus provide a convenient and organized way for users to access different features of an app. However, what if you want to trigger an activity from a menu item? In this article, we will explore how to call an activity from a menu item in Android.
To start off, let's create a new project in Android Studio. Once the project is created, we will have a default MainActivity with a simple layout. In order to add a menu to our app, we need to create a new resource file called "menu" under the "res" folder. Right-click on the "res" folder, go to New > Android Resource File and name it "menu". In the "Available qualifiers" section, select "Menu" from the drop-down menu and click "OK".
Next, we need to add a menu item to our newly created menu resource file. In this example, we will add a menu item called "Settings" with an ID of "settings". The code for the menu item will look like this:
<item android:id="@+id/settings"
android:title="Settings" />
Once the menu item is added, we need to inflate the menu in our MainActivity. To do this, open the MainActivity.java file and add the following code inside the onCreate() method:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
This code will inflate our menu resource file and display the menu item in our app. However, clicking on the menu item will not do anything at the moment. In order to call an activity from the menu item, we need to implement the onOptionsItemSelected() method. This method is called whenever a menu item is clicked. Inside this method, we will check if the menu item clicked is the "settings" item and if it is, we will start a new activity called "SettingsActivity". The code will look like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
In this code, we first get the ID of the menu item clicked. Then, we check if the ID matches our "settings" item. If it does, we create a new Intent and pass in the current activity (MainActivity) and the activity we want to start (SettingsActivity). Finally, we call the startActivity() method to start the new activity.
Now, let's create the SettingsActivity. Right-click on the "java" folder, go to New > Activity > Empty Activity and name it "SettingsActivity". This will create a new activity with a layout file. In this example, we will just add a simple TextView to the layout to display a message. The code for the layout will look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SettingsActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the Settings Activity" />
</LinearLayout>
Now, when we click on the "Settings" menu item in our app, the SettingsActivity will be launched and the message will be displayed.
In conclusion, calling an activity from a menu item in Android is a simple process. By creating a menu resource file, inflating it in our activity, and implementing the onOptionsItemSelected() method, we can easily navigate to different activities in our app. Menus provide a convenient way for users to access different features and activities in an app, making the user experience more efficient. So the next time you want to add a menu to your app, don't forget to implement the code to call an activity from a menu item. Happy coding!