How to Implement Android Toolbar to Display Different Activity Navigation

The Android toolbar has replaced the old Android action bar. In fact, Android has changed a lot of visual designs to build modern Android applications. And, with the introduction of Material Design with the release of Android Lollipop, the design changes now focus on offering a rich user experience, user interface, and added the Android toolbar.

The Android toolbar provides a great control to customize the appearance of action bar. Moreover, by implementing a toolbar in Android, developers can show multiple toolbars with different animations for developing a modern application

Today, in the Android app tutorial, we’ll build an Android app to demonstrate the basic implementation process of the Android toolbar.

Let’s Get Started

👉 Open your Android Studio and create a new project under the file menu.

Implement Android Toolbar

👉 In the next tab, select the targeted Android device.

Android Toolbar Example

👉 Select Empty Activity and click on next.

Android Toolbar Example

👉 Lastly, customize the activity.

Android Toolbar Demo

Start Code Integration

👉 Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:titleTextColor="#ffffff">

</android.support.v7.widget.Toolbar>
</LinearLayout>

Create menu.xml in res/menu folder

👉 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/miCompose"
android:icon="@drawable/ic_email"
app:showAsAction="ifRoom"
android:title="Compose">
</item>
<item
android:id="@+id/miProfile"
android:icon="@drawable/ic_profile"
app:showAsAction="ifRoom|withText"
android:title="Profile">
</item>
</menu>

Want To Develop An Android Application?

Validate your app idea and get a free quote.

👉 MainActivity.java

package com.spaceo.demotoolbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolBar();
}
public void initToolBar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.toolbarTitle);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow);
toolbar.setNavigationOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicking the Back!", Toast.LENGTH_SHORT).show();
}
}
);
}
// Menu icons are inflated just as they were with actionbar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.miCompose:
Toast.makeText(MainActivity.this, "clicking on email", Toast.LENGTH_SHORT).show();
return true;
case R.id.miProfile:
Toast.makeText(MainActivity.this, "clicking on profile", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

And done!

After completing all these steps, run your Android toolbar tutorial, and it’ll give you output like following screenshot.

Android Toolbar Example

However, there is a lot more customization can be done in Android toolbar, but if you’re developing an app for your business or startup, then it’s advisable that you talk to experienced developers, or hire Android app development company so that it could be implemented suiting your startup app idea.

Get a free copy of Android Toolbar Example from Github.

Author Bio
Amit Patolia
Amit Patolia
Designation: Android Team Lead

Amit Patolia is an Android Team Lead at Space-O Technologies. He has over 9+ years of experience in Android app development. He has guided to develop over 100 Android apps with unique features and functionalities. He also has expertise in Kotlin-based apps.