Create custom toolbar in Android Studio

Hey guys, Here we will learn how we create custom toolbar and menu in simple steps.

In Android, a toolbar is a basic part of the app. We can see a toolbar at the top of the activity in every app. We can easily add title, add your app logo, change color, add back button, and Menu list, and many more things. A toolbar is also part of the app UI so it’s must look good and attractive to users. Let’s follow the below step and create a Toolbar and Menu.

Step 1. Add Design Support Library:

To use Toolbar in Android app we need to add a design support library in build.gradle file.

Gradle Scripts>build.gradle(Module: app)> Inside dependencies

Add below dependency if you are using new version of Android Studio.

Now add below dependency if you are using old version of Android Studio.

Step 2. Add colors in colors.xml

Open values>colors.xml and add below colors code. If you want to edit the toolbar color here you can put the hex code of the color.

Attributes use in colors:
  • colorPrimary – color primary is the default color for toolbar and widgets. If you want to change the color of widgets you must change here. It’s become easy to change the color attire app.
  • colorPrimaryDark – color is the default color of the action bar.
  • colorAccent – color Accent color is used for the widgets default color like progress bar color and many more

Step 3: Add background xml file

Add a gradient_bg.xml file in drawable folder. Add code In gradient_bg.xml.

Attributes in background xml file:
  • shape – Used for shape. If you want to create any shape like oval, rectangle, etc.
  • gradient – Used to create a gradient using 2 or 3 colors.
  • angle – Used to set angle in gradient. It starts 0 to 360.
  • startColor – Used to start color in the gradient.
  • centerColor – Used to center color in the gradient.
  • endColor – Used to end color in the gradient.
  • corners – Used to create rounded corners.

Step 4. Add toolbar.xml

Create new xml file toolbar.xml file and add below code.

Attributes in toolbar.xml file
  • background – Used to set a background color. If you want to change background-color change here.
  • minHeight – Used to set min-height for toolbar.
  • titleTextColor – Used to set title color.

Step 5. Add xml code MainActivity.xml

To include any xml file in activity we used include attribute.

Step 6. MainActivity.java

Step 7. Create Menu folder

Add menu folder, right-click on res folder and select New>Android Resources Directory>put menu name inside directory name.

Now create new file inside menu folder for menu item. Here we will add more menu items which we have need.

Step 8. Add Java code in MainActivity

Add java code in mainActivity.java

Thanks for reading.

Leave a Comment