0

I have a Toolbar in my layout and I'm setting it as action bar. The problem is that I want to change the navigation icon and add some action buttons, and none of these work.

My layout is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.emilpana.carsharing"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        style="@style/Toolbar"
        app:navigationIcon="@drawable/ic_car"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:titleTextAppearance="@style/ToolbarTitle" />

    <ImageView
        style="@style/SecondaryShadow"
        android:contentDescription="@null" />

</LinearLayout>

This layout is included in my activities. I set it like this in the activity:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_car);

// getSupportActionBar().setHomeButtonEnabled(false);

getSupportActionBar().setTitle(null);

The icon is not set, it's the default hamburger. I've tried 2 styles of the App:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowActionBar">false</item>
</style>

and

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

When I don't set the Toolbar as actionBar the methods above to change the navigation icon are working(adding of action buttons works too). I've tried dozens of combinations of code and layout but none worked. Does anybody has an idea why the toolbar can't be customized ? Thanks.

Emil Pana
  • 468
  • 5
  • 14

1 Answers1

0

For navigation icon:

Toolbar toolbar = getActionBarToolbar();
toolbar.setNavigationIcon(R.drawable.id);
toolbar.setNavigationOnClickListener();

For title:

getSupportActionBar().setTitle(title);

For getActionBarToolbar() method:

protected Toolbar getActionBarToolbar() {
    if (actionBarToolbar == null) {
        actionBarToolbar = (Toolbar) findViewById(R.id.phone_actionbar_toolbar);
        if (actionBarToolbar != null) {
            setSupportActionBar(actionBarToolbar);
        }
    }
    return actionBarToolbar;
}

Theme:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Toolbar:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"
iosched:theme="@style/MyActionBar"
android:id="@+id/phone_actionbar_toolbar"
iosched:titleTextAppearance="@style/MyTextView"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:layout_height="?actionBarSize" />
Andy
  • 953
  • 1
  • 8
  • 18
  • Sorry but it doesn't work to change icon, it's like those methods are never called. Can you tell me what styles do you have for your application/toolbar ? It seems that the app theme is pretty important. – Emil Pana Nov 28 '14 at 17:58
  • @EmilPana I have added my theme in the answer. – Andy Nov 28 '14 at 19:06
  • @EmilPana I have added my toolbar maybe it will help you. – Andy Nov 29 '14 at 18:20