Stevie Kideckel Stevie Kideckel - 3 years ago 124
Android Question

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

So, I just updated my codebase to Lollipop, and I'm having issues with the Action Bar. I'm using AppCompat and ActionBarActivity, and inflating a custom view. It seems that the custom view no longer takes up the whole width of the screen, leaving a thin strip on the left

Building with 19
Way it used to look

Building with 21
Way it looks now

This is the code I'm using to set up the Action Bar. Anyone have any ideas?

final ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
// actionBar.setStackedBackgroundDrawable(null);
TextView title = (TextView) actionBar.getCustomView().findViewById(;
ImageView back = (ImageView) actionBar.getCustomView().findViewById(;
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {


Taking out the custom view and changing the background now takes up the whole width. So the issue is, how can we make a CustomView take up the whole width of the ActionBar?

Answer Source

Looks like this is caused by the latest changes to the ActionBar in the recent appcompat-v7 update. It seems like that there are significant changes to how you should handle action bars.

I faced the same issue and after reading the ActionBar documentation, and especially the following quote I found a solution.

Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout. The application may signal to the Activity which Toolbar should be treated as the Activity's action bar. Activities that use this feature should use one of the supplied .NoActionBar themes, set the windowActionBar attribute to false or otherwise not request the window feature.

The way I see it, the AppCompat themes were changed and on one hand seemed to break a few things but provide much more flexibility on the other. I recommend following these steps:

  1. Use .NoActionBar style in your activity as described in the above quote
  2. Add a to your Activity layout
  3. Set the app:contentInsetStart="0dp" attribute. This is the main issue with the margin that you describe in your question
< xmlns:android=""
    app:contentInsetStart="0dp" >

It's usually recommended that you do that in a separate layout file and use include in your activity layout so you will only need to customize the Toolbar in one place if used in multiple activities

<include layout="@layout/view_action_bar" />
  1. Use findViewById and setSupportActionBar in your Activity onCreate to signal to the Activity which Toolbar should be treated as the Activity's action bar
Toolbar actionBar = (Toolbar) findViewById(;
  1. Once you do that, all actions added in onCreateOptionsMenu will be added to the toolbar and it will be treated as the activity action bar.
  2. Further customize the Toolbar as desired (Add child views etc.)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download