Search code examples
androidandroid-stylesandroid-actionbar-compatandroid-actionbaractivity

getSupportActionBar().setCustomView(view) does not fill entire actionbar


I'm trying to set the customview of my app and the layout i made does not fill the entire actionbar. I've tried so many style settings but none has worked for me.

here's my code in activity

View view = getLayoutInflater().inflate(R.layout.actionbar_customized_home, null);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(view);

here's the layout placed set as view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/actionbar_color"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo"/>



</RelativeLayout>

Even the background of the menu items like the navdrawer how can I make my customview as background color?

I really appreciate the help


Solution

  • Set your main container to this:

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_horizontal"
            android:background="@color/actionbar_color"
            >
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_centerInParent="true"
                android:scaleType="fitCenter"
                android:src="@drawable/logo"/>
    
        </RelativeLayout>
    

    The important part is the android:layout_gravity="fill_horizontal that should solve your problem.

    EDIT:

    if that did not work try doing this:

    View view = getLayoutInflater().inflate(R.layout.actionbar_customized_home, null);
    LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    getSupportActionBar().setCustomView(view, layout);