Material Design Using Toolbar as Standalone

This post is related to my last post where i discussed using toolbar as an action bar. In this post i will discuss using it as standalone, now the situations will you be using the toolbar as a standalone may be summarized as

  • Show multiple toolbars.
  • Vary the width of the toolbar etc.

Here you see how to use it as standalone

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.my_layout);

  Toolbar toolbar = (Toolbar) findViewById(R.id.my_basic_toolbar);

  // Set an OnMenuItemClickListener to handle menu item clicks
  toolbar.setOnMenuItemClickListener(
    new Toolbar.OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
        // Handle the menu item
        return true;
      }
      });

      // Inflate a menu to be displayed in the toolbar
      toolbar.inflateMenu(R.menu.my_toolbar_menu);
    }

#####Note

when using toolbar as standalone we don’t have to disable the action bar.Rest things are same as discussed in my previous post

Gangnam style busts youtube video counter

“We never thought a video would be watched in numbers greater than a 32-bit integer (=2,147,483,647 views), but that was before we met PSY. ‘Gangnam Style’ has been viewed so many times we have to upgrade!” These words are not mine but the youtube google plus post which mentions that gangnam style had got actually more views than the actual number of views shown.

The problem was that google was using a 32-bit integer to represent the number of video view counter.The number of views that gangnam style were actually more than the range of 32-bit integer. So to correct the thing, the people at youtube have now shifted to using a 64-bit integer that has an astonishing limit of 9,223,372,036,854,775,808.

Material Design for pre lollipop devices

With the release of Android 5.0 a.k.a Lollipop new features were added to the android ecosystem,but the top most notable feature of android 5.0 is the Material Design Spec. The most notable feature for any android app is is Toolbar widget.

This is a generalization of the ActionBar pattern but gives you much more control and flexibility in using it. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate, react to scroll events.

How to use.

If you’re using Gradle then the first thing to do is to add appcompat as a dependency in your build.gradle:

   dependencies {
     ...
     compile "com.android.support:appcompat-v7:21.0.+"
   }

There are two ways for using Toolbar either standalone or Action bar. I will discuss using it as an Aciton bar.

  • Firstly the Activity should extend from ActionBarActivity but note to use import it from appcompact-v7.
  • Change the values/themes.xml:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Here we setting appcompat’s actionBarStyle -->
<item name="actionBarStyle">@style/MyActionBarStyle</item>

<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>

<!-- The rest of your attributes -->
</style>
  • You need to create a Toolbar instance, usually via your layout XML:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_basic_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
  • Then add the toolbar layout to your activity layout as:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include layout="@layout/toolbar" />

</RelativeLayout>
  • Final step is to to set your Toolbar as your action bar in the Activity.java:
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.my_layout);

Toolbar toolbar = (Toolbar) findViewById(R.id.my_basic_toolbar);
  setSupportActionBar(toolbar);
}

This is the Result:

a relative link

In my next post i will talk about using toolbar as standalone.