Android ActionBar's custom view not filling parent

Shalmezad picture Shalmezad · Dec 4, 2014 · Viewed 13.8k times · Source

Similar to the question here, but has some key differences. Most notably being that the accepted answer did work until the latest support library releases.

Here's the custom view layout:

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

</LinearLayout>

Now, when you try to set just the custom view:

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

You end up with a custom layout that doesn't fill the entire width: enter image description here

To make this more interested, I have a similar setup in a different app:

  ActionBar actionBar = getSupportActionBar();
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(R.layout.actionbar);
  actionBar.setDisplayHomeAsUpEnabled(false);
  actionBar.setHomeButtonEnabled(false);

This did work, until I updated the support libraries to v21. Once I did, I ended up with the same issue on apps that previously haven't had that issue.

So, my question is, is there a way to make a custom view actionbar fill the width using the latest support library?

Edit After doing some more tests, I found that compiling with targetSdkVersion/compileSdkVersion set to 19 and having v7:19.0.1 (or v7:19.1.0) for the library doesn't have this issue (has the home icon, which is taken care of with the later code), confirming that this is definitely an issue with the latest release.

Answer

tuder picture tuder · Jun 3, 2015

1

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View view = getLayoutInflater().inflate(R.layout.actionbar_title_back,
            null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
actionBar.setCustomView(view, layoutParams);
Toolbar parent = (Toolbar) view.getParent();
parent.setContentInsetsAbsolute(0, 0);

2 Use Toolbar

toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

XML

<?xml version="1.0" encoding="utf-8"?>       
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:popupBackground="@color/red"
        app:popupTheme="@style/PopupTheme"
        app:theme="@style/ToolbarTheme" >

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Toolbar Title"
            android:textColor="@color/white"
            android:textSize="17.0sp" />
    </android.support.v7.widget.Toolbar>
</LinearLayout>