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
Way it used to look
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.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setCustomView(R.layout.action_bar_content_search_custom_view);
actionBar.setBackgroundDrawable(null);
// actionBar.setStackedBackgroundDrawable(null);
TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
title.setText(R.string.youtube);
ImageView back = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
Edit
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?
Looks like this is caused by the latest changes to the
ActionBar
in the recentappcompat-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.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:.NoActionBar
style in your activity as described in the above quoteandroid.support.v7.widget.Toolbar
to your Activity layoutapp:contentInsetStart="0dp"
attribute. This is the main issue with the margin that you describe in your questionIt'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 activitiesfindViewById
andsetSupportActionBar
in your ActivityonCreate
tosignal to the Activity which Toolbar should be treated as the Activity's action bar
onCreateOptionsMenu
will be added to the toolbar and it will be treated as the activity action bar.None of the other answers worked for me, so I took a look at the actual AppCompat v7 styles, found here.
If you look at the Base.Widget.AppCompat.ActionBar style, it has:
So obviously we just need to override those properties in our own action bar style:
This worked great for me, hope it helps others too.
I just encountered this problem today as well, then I found out I was having
res/values-v21/styles.xml
inside my project which was generated automatically by Android Studio and that's the cause.Why?
Because my
res/values-v21/styles.xml
's content was:And my
res/values/styles.xml
contained something like:Then when I ran my app on Lollipop, the
res/values-v21/styles.xml
was used, and that caused exact problem that OP had. So I came to a simple fix is just deleting:in
res/values-v21/styles.xml
to the point:
make sure importing the right toolbar -
android.support.v7.widget.Toolbar;
After much beating my head against the monitor, this worked for me
The getCustomView().getParent() is what did the trick
Write these two line in addition with your code