Introduction to ToolBar
APP Bar1 App bar or action bar, using the application bar can keep your application consistent with other Android applications, allowing users to quickly understand how to use your application and get a first-class experience. The main functions of the application bar include:
- A dedicated area that identifies your application and indicates the user's location in the application.
- Important operations such as search are accessed in a predictable manner.
- Support navigation and view switching (via tabs or drop-down lists);
Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.
For this reason, you should use the support library's Toolbar class to implement your activities' app bars. Using the support library's toolbar helps ensure that your app will have consistent behavior across the widest range of devices. For example, the Toolbar widget provides a material design experience on devices running Android 2.1 (API level 7) or later, but the native action bar doesn't support material design unless the device is running Android 5.0 (API level 21) or later.
Starting with Android 3.0 (API level 11), ActionBar is used as the application bar for all Activities that use the default theme. However, with the evolution of different Android versions, the application bar functionality has gradually been added to the native ActionBar. Therefore, the behavior of native ActionBar varies with the version of Android system used by the device. By contrast, the latest features have been added to the Toolbar version of the support library and can be used on any device that can use the support library.
Therefore, you should use the Toolbar class that supports libraries to implement the Activity application bar. Using the toolbar of the support library helps ensure that your application behaves consistently on the widest range of devices. For example, the Toolbar widget can provide a Material Design experience on devices running Android 2.1 (API level 7) or higher, but unless the device runs Android 5.0 (API level 21) or higher, the native action bar will not support Material Design.
Therefore, the official recommendation is to use the Toolbar to support libraries to implement all activities.
Add toolbars to Activity
The following steps illustrate how to set the Toolbar to the Application Bar of Activity:
1. Introduce v7 appcompat support library;
2. Make Activity inherit AppCompatActivity, e.g.
public class MyActivity extends AppCompatActivity
{
// ...
}
3. Add one of the NoAction Bar themes of appcompat
In the application list, set the < Application > element to one of the NoAction Bar themes using appcompat, or to a separate theme for an Activity. Using one of these topics prevents applications from using native ActionBar classes to provide application bars. For example:
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
/>
Or set a theme for an Activity:
<activity
android:name="...MyActivity"
...
android:theme="@style/AppTheme.NoActionBar">
...
</activity>
4. Add a Toolbar to the Activity layout:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
5. Use Toolbar in Activity:
In Activity's onCreate() method, call Activity's setSupportActionBar() method, and pass Activity's toolbar. This method sets the toolbar as the activity application bar. For example:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}