Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Create Option Menu in Android using Kotlin?
Next article icon

How to Customize Option Menu of Toolbar in Android?

Last Updated : 14 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to add icons and change background color in the options menu of the toolbar. Option menu is a collection of menu items of an activity. Android Option Menus are the primary menus of the activity. They can be used for settings, search, delete items, etc. We will first create vector assets for the icon. Then we will set the icon in the toolbar items. Similarly, for the background, we will first create a custom style for the toolbar. Then we will set the theme in the toolbar. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. 

Step 2: Implement Option Menu

To create option menu of an activity in Android Studio refer to How to implement Options Menu in Android.

Step 3: Add Vector Assets

Right Click on the drawable folder and go to the new vector asset. Search from many vector icons and create one. You can also add an image from the outside but with the right dimension.

Step 4: Define Colors

It is always better to pre-define strings and colors instead of hard coding them hence we will define the colors.

  • Open the colors.xml file by navigating to the app -> res -> values -> colors.xml.
  • Create a color tag inside the resources tag with a name and set a color with its hex code.

Add the below lines inside the colors.xml file.

XML
<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="black">#FF000000</color>     <color name="white">#FFFFFFFF</color>     <color name="green">#0F9D58</color> </resources> 

Step 5: Create Custom Style

We need to create a new style with parent="ThemeOverlay.AppCompat.Light" as the parent theme. Below is the code for style.

XML
<resources>     <style name="MyToolbarStyle" parent="ThemeOverlay.AppCompat.Light">         <item name="android:colorBackground">@color/green</item>         <item name="android:textColor">@color/black</item>     </style> </resources> 

Step 6: Set Style in Toolbar

In the toolbar tag set the style in the popupTheme attribute. Look at the code below for reference.

XML
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout      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=".MainActivity">      <androidx.appcompat.widget.Toolbar         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/toolbar"         android:background="@color/black"         app:popupTheme="@style/MyToolbarStyle"         app:title="GFG" />  </RelativeLayout> 

Step 7: Add the Icons to the Items

  • Go to the menu.xml file where the items are created
  • Set the main item tag's showAsAction attribute to always
  • Set the icon in the sub-items tag using the icon attribute
XML
<?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/overflowMenu"         android:icon="@drawable/ic_baseline_more_vert_24"         android:title=""         app:showAsAction="always">         <menu>             <item                 android:icon="@drawable/ic_baseline_settings_24"                 android:title="Setting"                 />             <item                 android:icon="@drawable/ic_baseline_login_24"                 android:title="Logout"                 />         </menu>     </item> </menu> 

Output UI:


Next Article
How to Create Option Menu in Android using Kotlin?

N

namanjha10
Improve
Article Tags :
  • Technical Scripter
  • Android
  • Technical Scripter 2020

Similar Reads

  • How to Collapse Toolbar Layout in Android?
    In this article, we are going to create the CollapsingToolbar app that is fascinating and much useful. CollapsingToolbarLayout gives the facility of adjusting the size of toolbar title text when it is expanded or contracted. A sample GIF is given below to get an idea about how CollapsingToolbarLayou
    4 min read
  • How to Create Option Menu in Android using Kotlin?
    In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
    2 min read
  • How to Create Google Glass Options Menu in Android?
    Google Glass is a wearable device developed by Google that allows users to perform various tasks such as taking photos, recording videos, and sending messages. In this article, we will show you how to create a Google Glass options menu in Android. Step By Step Implementation Step 1: To create a new
    2 min read
  • How to Add Share Button in Toolbar in Android?
    In this article, we are going to create a simple Share Button in the Toolbar in Android. The Share Button is used to share information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media. We can share any type of message like text, images, vide
    4 min read
  • How to Customise MDC Sliders in Android?
    In the article of Material Design Components Sliders in Android, it's been discussed how to implement the Material Design Component sliders in android and also handled the click listener for all types of sliders. In this article, it's been discussed how to customize the Material Design Component sli
    5 min read
  • How to Add Custom Toolbar Background in Android?
    A toolbar is basically a form action bar that contains many interactive items. A toolbar supports a more focused feature than an action bar. The toolbar was added in Android Lollipop (API 21) and is the successor of the ActionBar. The toolbar is a ViewGroup that can be put anyplace in the XML layout
    3 min read
  • How to Change Font of Toolbar Title in an Android App?
    Google Fonts provide a wide variety of fonts that can be used to style the text in Android Studio. Appropriate fonts do not just enhance the user interface but they also signify and emphasize the purpose of the text. In this article, you will learn how to change the font-family of the Toolbar Title
    2 min read
  • How to Create Your Own Shortcut in Android Studio?
    Android Studio is the official integrated development environment for Google’s Android operating system, built on JetBrains’ IntelliJ IDEA software and designed specifically for Android app development. Android Studio offers a lot of shortcuts to users. It also provides to configure Keymap and add y
    2 min read
  • How to add Custom Spinner in Android?
    Spinner is a widget that is used to select an item from a list of items. When the user tap on a spinner a drop-down menu is visible to the user. In this article, we will learn how to add custom spinner in the app. Steps of Implementing Custom SpinnerStep 1: Create a new layout for each item in Spinn
    5 min read
  • How to Implement Custom Dialog Maker in Android?
    In this article, we are going to make an application of Custom Dialog Maker in android studio. In this application, we can create dialogs of our own choice of style, type, and animation. What is a dialog? A dialog is a small window that prompts the user to make a decision, provide some additional in
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences