11

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

dev.doc
  • 569
  • 3
  • 12
  • 18
alex_0709
  • 119
  • 1
  • 1
  • 9
  • Try changing the width and height of the RelativeLayout to match_parent instead – Mus Dec 03 '14 at 02:24

4 Answers4

24

This is all you need to add in your onCreate()

getSupportActionBar().setCustomView(new CustomView(this));
getSupportActionBar().setDisplayShowCustomEnabled(true);
clocksmith
  • 6,226
  • 3
  • 32
  • 45
  • 1
    getSupportActionBar().setDisplayShowCustomEnabled(true); is what did it for me. Thank you. – Alexi V. Oct 19 '17 at 23:54
  • This worked. Apparently is required in newer versions. Thank you! – Aswin G Sep 13 '18 at 18:46
  • this does not work for me, it doesn't fill the entire action bar (minus the head/back button on left, and added action buttons on the right). The accepted answer did work – Maverick Meerkat Nov 14 '18 at 08:16
11

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); 
JLONG
  • 805
  • 10
  • 20
1

Custom views only occupy the area between the nav/up button and the overflow menu. To change the background and other colors see this - https://developer.android.com/training/basics/actionbar/styling.html

Mus
  • 1,860
  • 2
  • 16
  • 19
  • hi! thank you for sharing this, now I just need to set the menu item background to the same color as the layout in my custom view. – alex_0709 Dec 03 '14 at 04:38
  • I've read that post and tried the styles but it won't change the background of the menu item like the drawertoggle button. I'm using appcompat v7 btw and ActionBarDrawerToggle to help me with the button – alex_0709 Dec 03 '14 at 04:40
0
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewCustom = inflator.inflate(R.layout.custom_searchview_actionbar, null);

        ActionBar actionBar = getSupportActionBar();

        if (actionBar != null) {
            actionBar.setCustomView(viewCustom, layout);
            actionBar.setDisplayShowCustomEnabled(true);
            Log.i("SEARCH", "ACTIONBAR IS NOT NULL");
        } else
            Log.i("SEARCH", "ACTIONBAR IS NULL :((");
O Thạnh Ldt
  • 1,103
  • 10
  • 11