vinitius
vinitius

Reputation: 3274

Migrate from ActionBar to ToolBar

I need to upgrade an app to use Material Design. The first thing I'm trying to do is show the new Toolbar, but I'm not sure on how I should do this.

I have my main FragmentActivity which I inflate some menu and do:

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main_color)));

I also use NavigationDrawer, so I set an icon for the drawer too.

Said that, how can I achieve the same behaviour using the new Toolbar?

Upvotes: 3

Views: 1788

Answers (1)

Kirill Shalnov
Kirill Shalnov

Reputation: 2216

Just look to a android developers blog. It contains explanation with "how to"

Shortly

Add Toolbar like a View to your activity's layout

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

You should to set your Toolbar as an ActionBar

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

And don't forget about styling. Add new style.xml to a resource dir values-21v, after that your Toolbar will be colored by your colorPrimary

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name=”colorPrimary”>@color/my_awesome_color</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name=”colorPrimaryDark”>@color/my_awesome_darker_color</item>

    <!-- colorAccent is used as the default value for colorControlActivated,
     which is used to tint widgets -->
    <item name=”colorAccent”>@color/accent</item>

    <!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight, and colorSwitchThumbNormal. -->

</style>

Upvotes: 7

Related Questions