Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 276
Shalmezad Avatar asked Dec 04 '14 15:12

Shalmezad


4 Answers

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>
like image 102
tuder Avatar answered Nov 11 '22 16:11

tuder


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

like image 44
jesto paul Avatar answered Nov 11 '22 17:11

jesto paul


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);
like image 44
Juturu Yugandhar Avatar answered Nov 11 '22 18:11

Juturu Yugandhar


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.

like image 3
Fakher Avatar answered Nov 11 '22 18:11

Fakher