Android Options Menu Example
Hello, Readers in this article we will learn how to create options menu for android. Android Option Menus are the essential menus of android Application. They can be used for settings, searching, deleting an item and so on
Here, we will see two events of other menus. First, the basic menus of selection and second, the selection menus with pictures.
Here, we spray the menu by calling the extension process () of the MenuInflater class. To perform occasion taking care of on menu things, you need to abrogate onOptionsItemSelected() technique for Activity class.
Android Option Menu Example
How about our view on how to make a menu on android. We should see a different basic menu model that contains three menu items.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu Example"
android:layout_centerInParent="true"/>
</RelativeLayout>
Here we are using a TextView in activity_main, however we do not have to use this layout.First of all, we will create a menu directory to create option menu, in this case you will have to right-click on the res and click on the Android resources directory and create a menu folder (res >> new >> Android Resource Directory). Like this picture
After you do this you will see a menu folder like this picture.
After right-clicking on the menu folder, you will create a Menu resources file and provide the name of your choice as I have given you the main_menu.
After that you will set a menu title, id attribute in this menu layout. Like the code below
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1"
android:title="Item1"/>
<item android:id="@+id/item2"
android:title="Item2"/>
<item android:id="@+id/item3"
android:title="Item3"/>
</menu>
MainActivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.item1:
Toast.makeText(this, "item 1", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "item 2", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "item 3", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "item 4", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}
In this MainActivity we will create an onCreateOptionsMenu (Menu menu) and through getMenuInflater () we will inflate the main_menu layout and name the menu layout and fill in onCreateOptionsMenu (Menu menu) as a menu parameter. By doing this, a menu folder will start to appear but by clicking on the menu item you will not be confused. For this, you will override the onOptionsItemSelected method and by clicking (item.getItemId ()) we will click on all items.
You can now launch this app and see your output, below are some screenshots provided..
Here we see a three-dot selection menu.
Here we have the full menu item selection
Here we see the result after clicking on the object.
If the app wants to display an icon in this options menu, you will need to make minor changes to the menu structure like the code below.
main_menu.xml after change
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1"
android:title="Item1"
app:showAsAction="always"
android:icon="@android:drawable/ic_share"/>
<item android:id="@+id/item2"
android:title="Item2"
app:showAsAction="always"
android:icon="@android:drawable/ic_share"/>
<item android:id="@+id/item3"
android:title="Item3"/>
</menu>
In setting up showAsAction: always in this code
Hope this code works for you, thanks you
0 Comments
Please do not enter any spam link in the comment box
Emoji