- Add Dependency In build.gradle (Module:App) And Sync
implementation 'com.android.support:design:28.0.0'
2. Goto activity_main.xml And Add This Code
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar" <!-- Set Id for Find the Toolbar-->
android:background="@color/colorPrimary" <!-- Set Background Color -->
android:theme="@style/ThemeOverlay.AppCompat.Dark"> <!--Set Theme -->
app:logo="@drawable/logo"><!-- logo for the Toolbar-->
app:logoDescription="LOGO"><!-- logo and logo description for the Toolbar-->
app:navigationIcon="@drawable/logo"><!-- navigation icon for the Toolbar-->
app:navigationContentDescription="Navigation Description"
app:title="AbhiAndroid">
<!-- title for the Toolbar-->
app:titleTextColor="#F00" <!-- title text color for the Toolbar-->
app:subtitle="AbhiAndroid">
<!-- sub title for the Toolbar-->
app:subtitleTextColor="#F00">
<!-- sub title text color for the Toolbar-->
</android.support.v7.widget.Toolbar>
3.Go to MainActivity.java And Add This Code For Setting Toolbar as An ActionBar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // get the reference of Toolbar
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar
If You Need To Define Toolbar Property Programmatically You Can Use Below Important Function For Toolbar .
setLogo(int resId): Example : toolbar.setLogo(R.drawable.logo); // setting a logo in toolbar.
setLogo(Drawable drawable): Example :toolbar.setLogo(getResources().getDrawable(R.drawable.logo)); // setting a logo in toolbar
setLogoDescription(CharSequence description):Example :toolbar.setLogoDescription("LOGO"); // set description for the logo
setLogoDescription(int resId): Example :toolbar.setLogoDescription(getResources().getString(R.string.descrition)); // set description for the logo
getLogoDescription(): Example :CharSequence logoDescription=toolbar.getLogoDescription(); // get the logo description from Toolbar
setNavigationIcon(int resId): Example : toolbar.setNavigationIcon(R.mipmap.ic_launcher); // set icon for navigation button
getNavigationIcon(): Example :Drawable navigationIcon = toolbar.getNavigationIcon(); // get navigation icon from Toolbar
setTitle(int resId): Example : toolbar.setTitle(getResources().getString(R.string.title)); // setting a title for this Toolbar
setTitle(CharSequence title): Example : toolbar.setTitle("AbhiAndroid"); // setting a title for this Toolbar
setSubtitle(int resId): Example : toolbar.setSubtitle(getResources().getString(R.string.subTitle)); // setting a sub title for this Toolbar
.setNavigationIcon(R.drawable.logo) : Example : toolbar.setNavigationIcon(int resId); // set icon for navigation button
setTitleTextColor(int color): Example : toolbar.setTitleTextColor(Color.RED); // set title text color for Toolbar.
setSubtitleTextColor(int color): Example : toolbar.setSubtitleTextColor (Color.RED); // set sub title text color for Toolbar
setNavigationOnClickListener(View.OnClickListener listener): Example : // implement setNavigationOnClickListener event
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// add code here that execute on click of navigation button
}
});
Step By Step To Set Action Menu In Toolbar/Action bar
Go to the res / menu / menu_main.xml for your project and add a new menu with a single element You Can Set Any Name For Your Menu Layout File Here We Define menu_main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_favorite"
android:orderInCategory="300"
android:title="User"
android:icon="@drawable/ic_favorite"
app:showAsAction="ifRoom"></item>
<item <a>android:id="@+id/action_add</a>"
<a>android:icon="@drawable/ic_menu_add</a>"
<a>android:title="ifRoom</a>"
<a>android:showAsAction="ifRoom</a>"></item>
<item <a>android:id="@+id/action_delete</a>"
<a>android:icon="@drawable/ic_menu_delete</a>"
<a>android:title="Delete</a>"
<a>android:showAsAction="never</a>"></item>
</menu>
The most important property is android:showAsAction . You See Above Show As Icon Set The Visibility On Menu Area .
Inflate Menu Layout To Toolbar Menu Area .
@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_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_favorite) {
Toast.makeText(MainActivity.this, "Action clicked", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}