Search code examples
androidxmlandroid-actionbardrawerlayout

ActionBar logo move left


I want to use logo instead of homeAsUp on actionBar to open drawerLayout, I managed to do it but it seems that the space of homeAsUp is left, so it looks like the logo has leftMargin.

This is what I did:
style.xml

<item name="android:logo">@drawable/logo_file</item> 
<item name="android:displayOptions">showHome|useLogo</item>

logo_file.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/mylogo"
        android:left="-10dp"
</layer-list>

Activity

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayUseLogoEnabled(true);

Doing android:left="-10dp" does not solve the problem it only removes 10dp from the left of logo.

So please help me to move the logo left.


Solution

  • I tend to do the following in the onCreate method of the activity.

    ActionBar ab = getSupportActionBar();
    ab.setDisplayUseLogoEnabled(true);
    ab.setDisplayShowHomeEnabled(true);
    ab.setDisplayShowHomeEnabled(false);
    ab.setHomeAsUpIndicator(R.drawable.logo_actionbar); //this is your app logo or whatever youre using
    ab.setDisplayHomeAsUpEnabled(true);
    

    As long as the logo is cropped correctly it will be laid out as expected without having to worry about padding and offsets, although if you are using a drawer there is a great sample on the android website which shows how to set up a draw toggle properly