Another name for App Bar is action bar.The main use of App bar is to make the application visual structure consistent.
Another name for App Bar is action bar.The main use of App bar is to make the application visual structure consistent.
You can add search actions, give app identity and location to users.In this tutorial am going to use v7 appcompat support library to add Toolbar because it can work in many devices.
Let now add some action to the menu items.
You can add search actions, give app identity and location to users.In this tutorial am going to use v7 appcompat support library to add Toolbar because it can work in many devices.
Adding Toolbar to activity.
- Make sure you add v7 appcompat support library
- In your activity extend class AppCompatActivity public
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
}
- Prevent the theme from using its default ActionBar by adding below code I manifest file.
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
</application>
Add Toolbar to layout
<?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.appbarexample.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package net.techoverload.www.appbarexample;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolBar=(Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolBar);
}
}
By now the application should run and show a Toolbar with just the application title.
Let now go ahead and add some menu to the Toolbar.
Open res>create a new resource menu and XML menu file called example_menu.xml and paste below code.
After creating our menu now we need to add it to our MainActivity.java
Let now go ahead and add some menu to the Toolbar.
Open res>create a new resource menu and XML menu file called example_menu.xml and paste below code.
<?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:icon="@drawable/ic_search"
android:title="action_search"
app:showAsAction="ifRoom"
/>
<item android:id="@+id/action_favorite"
android:icon="@drawable/ic_action_favorite"
android:title="action_favorite"
app:showAsAction="ifRoom"
/>
<item android:title="Settings"
android:id="@+id/action_settings"
app:showAsAction="never"
/>
<item android:title="Most Viewed"
android:id="@+id/most_viewed"
app:showAsAction="never"/>
</menu>
After creating our menu now we need to add it to our MainActivity.java
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.example_menu,menu);
return true;
}
NB: You will be required to import
import android.view.MenuInflater;
import android.view.Menu;
Let now add some action to the menu items.
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_favorite:
//Do something
return true;
case R.id.action_search:
//Do something
return true;
case R.id.action_settings:
//Do something
return true;
}
return super.onOptionsItemSelected(item);
}
Whole MainActivity.java Code.
package net.techoverload.www.appbarexample;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolBar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolBar);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.example_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_favorite:
//Do something
return true;
case R.id.action_search:
//Do something
return true;
case R.id.action_settings:
//Do something
return true;
}
return super.onOptionsItemSelected(item);
}
}