Android 4.4 Kitkat custom view actionbar not filling the whole width

user1940676 picture user1940676 · Nov 24, 2013 · Viewed 7.6k times · Source

I am trying to have a Simple actionbar with a custom view, but I get the following result:

For demonstration I created a simple xml with a yellow background color which is supposed to take the whole width.

Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow"
    android:orientation="vertical" >

    <TextView
        android:visibility="gone"
        android:id="@+id/action_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/action_save"
        android:gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/white"
        android:text="@string/save" />

    <ImageView
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_actionbar_logo" />

</RelativeLayout>

And I use the following theme for my Application:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>

actionbar

This is the actionbar initialization code:

    actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(R.layout.actionbar);

Answer

user1940676 picture user1940676 · Nov 24, 2013

The solution is adding :

actionBar.setDisplayShowTitleEnabled(false);

The code should look something like:

actionBar = getSupportActionBar(); 
actionBar.setDisplayShowHomeEnabled(false); 
actionBar.setDisplayShowCustomEnabled(true); 
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.actionbar);//set the custom view