How to add a Toolbar to Application in Android Studio and add menu items to it.
In this tutorial am going to show you how to add a Toolbar to the application.Then add Search and settings menu.
Open Android Studio and create a new project
Project name: ToolbarExample
Next Open Manifest.xml file and add this.
Create the MainActivity.java and ensure you check Backwards compatibility(AppCompat) checkbox.
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
This code will ensure that NoActionBar is created this giving us the chance to define our own Toolbar.
Create a folder under res> and name it menu.
Inside menu create a xml file and name it toolbar_menu.xml
Note:
For this project to work, make sure you add image asset icon and label it "ic_action_search"
Output Screenshots
Open Android Studio and create a new project
Project name: ToolbarExample
Next Open Manifest.xml file and add this.
Create the MainActivity.java and ensure you check Backwards compatibility(AppCompat) checkbox.
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
This code will ensure that NoActionBar is created this giving us the chance to define our own Toolbar.
AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.techoverload.www.toolbarexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Create a folder under res> and name it menu.
Inside menu create a xml file and name it toolbar_menu.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/action_search"
android:title="Search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
/>
</menu>
activity_main.xml code
<?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"
tools:context="net.techoverload.www.toolbarexample.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
<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>
MainActivity.java
package net.techoverload.www.toolbarexample;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
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.my_toolbar);
setSupportActionBar(toolbar);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.toolbar_menu,menu);
return true;
}
}
For this project to work, make sure you add image asset icon and label it "ic_action_search"
Download Source code here
Output Screenshots