Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar navigation icon never set

I'm trying the new Toolbar component and having some trouble with the navigation icon. I want to implement a custom icon for back navigation :

In my manifest i set a parent to my activity :

<activity android:name=".CardsActivity" android:parentActivityName=".MainActivity">     <!-- Parent activity meta-data to support API level 7+ -->     <meta-data         android:name="android.support.PARENT_ACTIVITY"         android:value=".MainActivity" /> </activity> 

I declare the toolbar like this :

<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"     android:paddingBottom="@dimen/activity_vertical_margin"     tools:context="com.example.lollitest.MainActivity" >          <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:layout_marginBottom="10dp"         android:background="?attr/colorPrimary" />      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/my_awesome_toolbar"         android:text="@string/hello_world" />  </RelativeLayout> 

Then in my activity i configure the Toolbar like this :

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); toolbar.setNavigationIcon(R.drawable.ic_good); toolbar.setTitle("Title"); toolbar.setSubtitle("Sub"); toolbar.setLogo(R.drawable.ic_launcher); setSupportActionBar(toolbar); 

Which giving me : Toolbar with back button

The back icon is not the one i set with setNavigationIcon() ! Whatever drawable i give to the method the navigation icon is always the back arrow.

I have tried to remove the parent association in the manifest but the only effect is (obviously) to prevent the button to go back.

On contrary if i want the default back arrow icon and don't call setNavigationIcon() i don't have any icon at all.

What is the correct way to handle the navigation icon in toolbar (custom and default) ?

NOte : i'm running my test on Android 4.4

like image 906
grunk Avatar asked Oct 23 '14 09:10

grunk


People also ask

What is Toolbar Android?

In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.


2 Answers

Currently you can use it, changing the order: (it seems to be a bug)

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar);  toolbar.setNavigationIcon(R.drawable.ic_good); toolbar.setTitle("Title"); toolbar.setSubtitle("Sub"); toolbar.setLogo(R.drawable.ic_launcher); 
like image 121
Gabriele Mariotti Avatar answered Oct 06 '22 08:10

Gabriele Mariotti


Specific to the navigation icon, this is the correct order

// get the actionbar as Toolbar and set it up Toolbar toolbar = (Toolbar) findViewById(R.id.signIn_toolbar); setSupportActionBar(toolbar); 

Inform the Toolbar to provide back navigation. This will set the icon to the default material icon

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

Later override the icon with the custom one, in my case the Holo back icon

toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_36dp); 
like image 26
Raffaeu Avatar answered Oct 06 '22 07:10

Raffaeu