Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

Showing posts with label Toolbar. Show all posts
Showing posts with label Toolbar. Show all posts

Android: How to use Toolbar

Toolbar is a replacement to Android ActionBar. It has many features to customize its appearance unlike old ActionBar.

Toolbar is introduced in Android Lollipop, API level 21.
Google provides fully supported Toolbar features to lower android os devices via AppCompact support library.

In AppCompat, Toolbar is implemented in the android.support.v7.widget.Toolbar class.

Create Toolbar:

1. First of all, we need to disable ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

2. We have to add AppCompact library in build.gradle file
    compile "com.android.support:appcompat-v7:21.0.+"

3. Apply the theme to Activity.
   <activity android:name="com.example.sample.MyActivity"

      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
   </activity>

4. Add Toolbar in activity layout
   <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"

            android:background="#000000"
            android:layout_width="match_parent"

            android:layout_height="wrap_content" >                                                </android.support.v7.widget.Toolbar>

5. You just need to instantiate the Toolbar and add it to your activity by using         setSupportActionBar(Toolbar) method.

    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.widget.Toolbar;
    public class MyActivity extends ActioBarActivity {
       @Override
        protected void onCreate(Bundle savedInstanceState)
        { 

           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

           setSupportActionBar(toolbar);
        }
    }

Android L: Tool Bar widget

Toolbar widget introduced in Android 5.0(API 21).This widget also available in support library for older versions.

A Toolbar is a generalization of action bars.

Toolbar supports a more focused feature set than ActionBar. The new Toolbar contains the following elements:

  • A navigation button:This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar.
  • A branded logo image: This may extend to the height of the bar and can be arbitrarily wide.
  • A title and subtitle: The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
  • One or more custom views: The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
  • An action menu: The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions.

Different types of Toolbar examples: