Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Created August 4, 2014 13:22
Show Gist options
  • Save gabrielemariotti/ae63392e1c70bc33af8b to your computer and use it in GitHub Desktop.
Save gabrielemariotti/ae63392e1c70bc33af8b to your computer and use it in GitHub Desktop.
Android-L: A little example of the new Toolbar view.
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
//Title and subtitle
toolbar.setTitle("MY toolbar");
toolbar.setSubtitle("Subtitle");
//Menu
toolbar.inflateMenu(R.menu.toolbar_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.action_share:
Toast.makeText(ToolbarActivity.this,"Share",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
//Navigation Icon
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ToolbarActivity.this,"Navigation",Toast.LENGTH_SHORT).show();
}
});
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp">
</Toolbar>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="example.gmariotti.it.test21.ToolbarActivity" >
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_share"
android:title="@string/action_share"
android:icon="@drawable/ic_share"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
@yelinaung
Copy link

It looks a lot like new ActionBar ? Or is it the ActionBar ? 😕

Copy link

ghost commented Sep 9, 2014

Nice, how do you set the navigation icon in xml?

@siddharth96
Copy link

any screenshots?

@bamsbamx
Copy link

It would be good to show how navdrawer works too

@Defuera
Copy link

Defuera commented Nov 27, 2014

@balrampandey19
Copy link

HOW TO MAKE TOOLBAR TRANSPARENT

@doodeec
Copy link

doodeec commented Feb 5, 2015

@balrampandey19

<style name="ToolbarTheme" parent="Theme.AppCompat">
    <item name="android:background">**your color**</item>
</style>

and then use it like this in layout

<Toolbar
        ...
        app:theme="@style/ToolbarTheme"/>

@smiletolead
Copy link

I am exploring the Toolbar. I don't find a way to define android.support.v7.widget.Toolbar using XML completely. What I mean by this is that the toolbar components such as title, subtitle need to added through code but not using XML. Is there a way to add these components using XML? If not, why toolbar is designed like this?

@ebrirad
Copy link

ebrirad commented Mar 18, 2017

very good bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment