Tuesday, March 16, 2021

Android Options Menu

 

Android Option Menu Example

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.

Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images.

Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.

Android Option Menu Example

Let's see how to create menu in android. Let's see the simple option menu example that contains three menu items.

activity_main.xml

We have only one textview in this file.

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.optionmenu.MainActivity">  
  8.   
  9.     <android.support.design.widget.AppBarLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:theme="@style/AppTheme.AppBarOverlay">  
  13.   
  14.         <android.support.v7.widget.Toolbar  
  15.             android:id="@+id/toolbar"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="?attr/actionBarSize"  
  18.             android:background="?attr/colorPrimary"  
  19.             app:popupTheme="@style/AppTheme.PopupOverlay" />  
  20.   
  21.     </android.support.design.widget.AppBarLayout>  
  22.   
  23.     <include layout="@layout/content_main" />  
  24.   
  25. </android.support.design.widget.CoordinatorLayout>  
File: context_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  8.     tools:context="example.javatpoint.com.optionmenu.MainActivity"  
  9.     tools:showIn="@layout/activity_main">  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="Hello World!"  
  15.         app:layout_constraintBottom_toBottomOf="parent"  
  16.         app:layout_constraintLeft_toLeftOf="parent"  
  17.         app:layout_constraintRight_toRightOf="parent"  
  18.         app:layout_constraintTop_toTopOf="parent" />  
  19.   
  20. </android.support.constraint.ConstraintLayout>  

menu_main.xml

It contains three items as show below. It is created automatically inside the res/menu directory.

File: menu_main.xml
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     tools:context="example.javatpoint.com.optionmenu.MainActivity">  
  5.   
  6.     <item  android:id="@+id/item1"  
  7.         android:title="Item 1"/>  
  8.     <item  android:id="@+id/item2"  
  9.         android:title="Item 2"/>  
  10.     <item  android:id="@+id/item3"  
  11.         android:title="Item 3"  
  12.         app:showAsAction="withText"/>  
  13. </menu>  


Activity class

This class displays the content of menu.xml file and performs event handling on clicking the menu items.

File: MainActivity.java
  1. package example.javatpoint.com.optionmenu;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.support.v7.widget.Toolbar;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  17.         setSupportActionBar(toolbar);  
  18.     }  
  19.   
  20.     @Override  
  21.     public boolean onCreateOptionsMenu(Menu menu) {  
  22.         // Inflate the menu; this adds items to the action bar if it is present.  
  23.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  24.         return true;  
  25.     }  
  26.   
  27.     @Override  
  28.     public boolean onOptionsItemSelected(MenuItem item) {  
  29.        int id = item.getItemId();  
  30.         switch (id){  
  31.             case R.id.item1:  
  32.                 Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();  
  33.                 return true;  
  34.             case R.id.item2:  
  35.                 Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();  
  36.                 return true;  
  37.             case R.id.item3:  
  38.                 Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();  
  39.                 return true;  
  40.             default:  
  41.                 return super.onOptionsItemSelected(item);  
  42.         }  
  43.     }  
  44. }  

Output:

Output without clicking on the menu button.

android option menu example 1

Output after clicking on the menu button.


android option menu example 2

Output after clicking on the second menu item .


android option menu example 3

Option Menu with Icon

You need to have icon images inside the res/drawable directory. The android:icon element is used to display the icon on the option menu. You can write the string information in the strings.xml file. But we have written it inside the menu_main.xml file.

File: menu_main.xml
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     tools:context="example.javatpoint.com.optionmenu.MainActivity">  
  5.   
  6.     <item  android:id="@+id/item1"  
  7.         android:title="Item 1"  
  8.         app:showAsAction="always"  
  9.         android:icon="@android:drawable/btn_star"/>  
  10.     <item  android:id="@+id/item2"  
  11.         android:title="Item 2"  
  12.         app:showAsAction="ifRoom"  
  13.         android:icon="@android:drawable/btn_plus"/>  
  14.     <item  android:id="@+id/item3"  
  15.         android:title="Item 3"  
  16.         app:showAsAction="withText"  
  17.         android:icon="@android:drawable/btn_plus"/>  
  18. </menu>  
android option menu example 4 android option menu example 5

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile