Option menu in android is the primary menus of android. They can be used for things like settings, search, and edit items, etc.
The Options menu allows you to perform actions that have a global impact on the application. Option Menu is created by Overriding the onCreateOptionsMenu() function .
Handling Android Options Menu Clicks Event
The onOptionsItemSelected() event function in Android can be used to handle option menu item click events.
The following are some of the most commonly used options menu control properties in Android applications.
Attribute | Description |
---|---|
android:title | Used to set the item’s title |
android:icon | Used to set the item’s icon from the drawable folder. |
android:showAsAction | Used to specify how the item should appear as an action item in the app bar. |
Preview: Option Menu
<–Option Menu Example
File:menu_main.xml
–> Create a New Resource file and Add the given below code to your file
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/item1" android:icon="@drawable/ic_baseline_card_travel_24" android:title="Item 1" app:showAsAction="ifRoom" /> <item android:id="@+id/item2" android:icon="@drawable/ic_baseline_gps_fixed_24" android:title="Item 2" app:showAsAction="never" /> <item android:id="@+id/item3" android:icon="@drawable/ic_baseline_camera_alt_24" android:title="Item 3" app:showAsAction="never" > <menu> <item android:id="@+id/subitem1" android:title="Sub item 1" android:icon="@drawable/ic_baseline_camera_alt_24"/> <item android:id="@+id/subitem2" android:title="Sub item 2" android:icon="@drawable/ic_baseline_gps_fixed_24"/> </menu> </item> </menu>
File: activity_main.xml
–>Add the given below code to your file activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Developers Dome" android:textAlignment="center" android:textSize="30sp" android:textColor="@color/teal_200" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
File: MainActivity.kt
–>Add the given below code to your file MainActivity.kt
package com.example.option_menu import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.* import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.menu_main, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.item1 -> { Toast.makeText(applicationContext, "click on item 1", Toast.LENGTH_LONG).show() true } R.id.item2 -> { Toast.makeText(applicationContext, "click on item 2", Toast.LENGTH_LONG).show() true } R.id.item1 -> { Toast.makeText(applicationContext, "click on item 3", Toast.LENGTH_LONG).show() true } R.id.subitem1 -> { Toast.makeText(applicationContext, "click on subitem 1", Toast.LENGTH_LONG).show() true } R.id.subitem2 -> { Toast.makeText(applicationContext, "click on subitem 2", Toast.LENGTH_LONG).show() true } else -> super.onOptionsItemSelected(item) } } }
If you look at the code above, you’ll notice that we’ve overridden the activity’s onCreateOptionsMenu() method to construct an options menu and loaded a defined menu resource using MenuInflater.inflate ().
The Android Studio will often call the onCreate() callback method during the start of our activity to obtain the required layout for an activity.
ViewPager Tutorial using Fragment in Android
Data Binding in Android with Example
Pingback: Difference between Linear layout and Relative layout