Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.4 Kitkat custom view actionbar not filling the whole width

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);
like image 627
user1940676 Avatar asked Nov 24 '13 07:11

user1940676


2 Answers

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
like image 73
user1940676 Avatar answered Nov 15 '22 02:11

user1940676


@NemesisDoom: Please make sure that you use ActionBar from the support.v7 library rather that one from the Android API (this second one will not work certainly). If still doesn't work, add the further changes:

styles.xml (your action bar style section):

 <item name="android:homeAsUpIndicator">@drawable/_pixel</item>
    <item name="homeAsUpIndicator">@drawable/_pixel</item>

_pixel is 1x1.png image, transparent.

your activity:

actionBar.setDisplayHomeAsUpEnabled(true);
like image 5
Gille Avatar answered Nov 15 '22 03:11

Gille