Reputation: 4659
I want to display to use a custom view on my action bar that completely covers the action bar (width match parent)
But my custom view does not cover the entire width of the action bar.
public class MyActivity extends AppCompatActivity {
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_entry_screen);
setActionBar();
}
private void setActionBar() {
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View abView = actionBar.getCustomView();
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams layout = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
View v = inflator.inflate(R.layout.action_bar, null);
actionBar.setCustomView(v, layout);
actionBar.setDisplayShowCustomEnabled(true);
}
action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mvp_logo"
android:layout_alignParentRight="true" />
</RelativeLayout>
I have referred the following links:
How to display custom view in ActionBar?
Manually inflating custom view yields different layouts for ActionBar custom view
Android center custom view in actionbar
Android: How to center a component in a custom view in action bar?
Thanks
Upvotes: 1
Views: 503
Reputation: 21753
The Toolbar might be better for you, but as a note, this is breaking the design guidelines and will make your app not 'fit' in the Android ecosystem, that area is usually where the context menu is found and most users will have learned that behaviour.
You can fix the behaviour by removing content insets as follows
View myLogo = mInflater.inflate(R.layout.actionbar_custom, null);
getSupportActionBar().setCustomView(myLogo);
((Toolbar) myLogo.getParent()).setContentInsetsAbsolute(0,0);
If you've already changed to Toolbar you can fix it in the XML for the custom view
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
Upvotes: 1