Android ActionBar's custom view not filling parent [duplicate]
Asked Answered
N

4

19

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.

Necrophobia answered 4/12, 2014 at 15:52 Comment(1)
H
32

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>
Hage answered 3/6, 2015 at 1:54 Comment(5)
Can you explain how this specifically addresses the issue with the latest release the OP asked about?Valenevalenka
@Hage but i m getting NPE when i m trying to launch my app first time and errro is on this line parent.setContentInsetsAbsolute(0, 0); this shows that parent is nullOuch
@Ouch check your layout's id or your Actionbar's version, i use android.support.v7.appcompatHage
@tuder, Thanks a lot. it works..Shamblin
Thank you. Tried option 1, which worked. Note: here are the import statements I had to add: import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.LayoutParams; import android.support.v7.widget.Toolbar;Aerophagia
P
7

use Toolbar parent = (Toolbar) customNav.getParent(); parent.setContentInsetsAbsolute(0, 0); it will work

Polychasium answered 29/7, 2015 at 6:25 Comment(0)
M
6

add like this in your onCreate method

 ActionBar action = getSupportActionBar();
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
        toolbar.setContentInsetsAbsolute(0, 0);
        toolbar.getContentInsetEnd();
        toolbar.setPadding(0, 0, 0, 0);
Micropyle answered 13/6, 2016 at 7:17 Comment(0)
G
3

In your XML code add these attributes:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/re_app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:contentInsetEnd="0dp"
    android:contentInsetLeft="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp">

it will fit all width of the view.

Gleet answered 25/5, 2017 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.