android option menu
ReportQuestion
2 years ago 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.
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="example.javatpoint.com.optionmenu.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- </android.support.design.widget.CoordinatorLayout>
context_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="example.javatpoint.com.optionmenu.MainActivity"
- tools:showIn="@layout/activity_main">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
menu_main.xml
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- tools:context="example.javatpoint.com.optionmenu.MainActivity">
- <item android:id="@+id/item1"
- android:title="Item 1"/>
- <item android:id="@+id/item2"
- android:title="Item 2"/>
- <item android:id="@+id/item3"
- android:title="Item 3"
- app:showAsAction="withText"/>
- </menu>
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- switch (id){
- case R.id.item1:
- Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
- return true;
- case R.id.item2:
- Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
- return true;
- case R.id.item3:
- Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- }
Thread Reply