Ask Your Question
0

The custom view in AppCompat ActionBar on Android Lollipop doesn't occupy the entire width of the screen.

asked 2022-03-11 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-08-05 23:00:00 +0000

scrum gravatar image

This is likely due to the default padding that is applied to the ActionBar in AppCompat. You can remove this padding by setting the content insets to zero:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(customView, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
    actionBar.setPadding(0, 0, 0, 0);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Remove elevation/shadow from ActionBar
        actionBar.setElevation(0);
    }
}

Note: customView should be a View that you have already inflated or created for your custom ActionBar layout.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-11 11:00:00 +0000

Seen: 12 times

Last updated: Aug 05 '22