Create Options Menu in ActionBar in Android using Jetpack Compose
Last Updated :
30 Mar, 2022
In Android, ac ActionBar or a TopBar is a UI element that is present at the top of the activity screen. An ActionBar by default displays the activity name inside it. However, we can add other elements like the back button, images, options menu, etc inside an ActionBar. So in this article, we will show you how you could Create Options Menu in ActionBar in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.geeksforgeeks.jcactionbarmenuoptions
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Calling the composable function
// to display element and its contents
MainContent()
}
}
}
// Creating a composable function
// to display Top Bar and options menu
@Composable
fun MainContent() {
// Create a boolean variable
// to store the display menu state
var mDisplayMenu by remember { mutableStateOf(false) }
// fetching local context
val mContext = LocalContext.current
// Creating a Top bar
TopAppBar(
title = { Text("GFG | Menu Options", color = Color.White) } ,backgroundColor = Color(0xff0f9d58),
actions = {
// Creating Icon button favorites, on click
// would create a Toast message
IconButton(onClick = { Toast.makeText(mContext, "Favorite", Toast.LENGTH_SHORT).show() }) {
Icon(Icons.Default.Favorite, "")
}
// Creating Icon button for dropdown menu
IconButton(onClick = { mDisplayMenu = !mDisplayMenu }) {
Icon(Icons.Default.MoreVert, "")
}
// Creating a dropdown menu
DropdownMenu(
expanded = mDisplayMenu,
onDismissRequest = { mDisplayMenu = false }
) {
// Creating dropdown menu item, on click
// would create a Toast message
DropdownMenuItem(onClick = { Toast.makeText(mContext, "Settings", Toast.LENGTH_SHORT).show() }) {
Text(text = "Settings")
}
// Creating dropdown menu item, on click
// would create a Toast message
DropdownMenuItem(onClick = { Toast.makeText(mContext, "Logout", Toast.LENGTH_SHORT).show() }) {
Text(text = "Logout")
}
}
}
)
}
// For displaying preview in
// the Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MainContent()
}
Output:
You can see that we are able to create options menu as well as a drop-down menu as seen in the below output video.
Similar Reads
Swiping Action Box in Android using Jetpack Compose If you are an Android user, you must have seen applications that display the list of items and each of those items could be dragged left or right to perform some particular action. Gmail application for Android is the most common example, where you can drag an item left or right from the inbox to ar
3 min read
Floating Action Button in Android using Jetpack Compose Floating Action Button is added to the android application to perform some important within the android application. These are implemented in the android application UI for some primary actions within the application. There are different types of floating action buttons such as simple, square, and e
3 min read
Jetpack Compose Navigation and Passing Data in Android Almost every app uses some kind of navigation, allows users to move from one screen to another. In this article, we will learn to implement Navigation in Jetpack Compose using Compose way. We will build a simple app demonstrating Jetpack compose navigation, It will have three screens(Home, Profile,
4 min read
How to Create Outlined Text in Android using Jetpack Compose? In Android, we can customize various aspects of a Text for displaying them accordingly. We can change the font size, and font family as well as apply various functions like bold, italics, etc. However, it is not possible to display an outline font for the same text. In case, you are unaware of what
3 min read
Deep Links in Android using Jetpack Compose A deep link is a URL that is used to direct users to a specific page or specific activity within the application. We can also pass data to our application with the help of these deep links. In this article, we will take a look at How to implement Deep Links in Android using Jetpack Compose. Step by
7 min read