user2573690
user2573690

Reputation: 5973

Custom image as action bar logo margin?

I am trying to add a custom image as the action bar logo, which works, however, there is about 30-50dp of padding in front of this image when it is added to the action bar.

The image itself has no padding on the left, so it should be almost right next to the edge of the screen.

Where does this padding get added from at runtime? Here is my code for the custom layout I am using for the actionbar:

<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:id="@+id/actionbar">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/logo"
</RelativeLayout>

This is my code for setting up the action bar layout in my activity:

 ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.actionbar, null);
        actionBar.setCustomView(v);

The only way I have found to remove this padding is by adding android:layout_marginLeft="-30dp" to the layout of the custom action bar, but I definitely don't think this is the correct approach.

Any ideas why there would be extra padding in from of my image? Thanks in advance for the help!

EDIT

I managed to figure this out, I added the following two lines to my action bar setup in my activity:

Toolbar parent = (Toolbar) v.getParent();
parent.setContentInsetsAbsolute(0, 0);

Upvotes: 0

Views: 906

Answers (1)

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4578

Try modifying ActionBar sytles in your styles.xml

<style name="MyActionBarTheme" parent="Widget.AppCompat.ActionBar">
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
</style>

Then is your App Theme

<item name="android:actionBarStyle">@style/MyActionBarTheme</item>

Or Incase you are using ToolbarLayout

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:paddingLeft="0dp">

<!--rest of content -->

</android.support.v7.widget.Toolbar>

Happy coding :)

Upvotes: 1

Related Questions