16

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);
user1940676
  • 4,348
  • 9
  • 44
  • 73

2 Answers2

26

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
user1940676
  • 4,348
  • 9
  • 44
  • 73
  • @NemesisDoom worked for me, add it right after `getSupportActionBar()` call. – user1940676 Dec 03 '13 at 07:00
  • @chhameed have you called that method right after getSupportActionBar(); call? – user1940676 May 20 '14 at 14:27
  • yes exactly, i called it right after getSupportActionBar() call. but it does nothing .... – chhameed May 21 '14 at 04:33
  • 1
    write the following code: actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowTitleEnabled(false); – user1940676 May 21 '14 at 06:19
  • @JerryGu Since this post is for Android 4.4, I asked a new question [here](http://stackoverflow.com/questions/27298282/android-actionbars-custom-view-not-filling-parent) – Shalmezad Dec 04 '14 at 19:27
  • i still have problem on android 5 – Mahdi Oct 20 '15 at 15:31
5

@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);
Gille
  • 316
  • 3
  • 4