android option menu

Report
Question
755 views

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.

Please explain why do you think this question should be reported?

Report Cancel

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>  

 

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

  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> 

 

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

Thread Reply

Leave an comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>