I want to create a custom action bar by placing it ImageViewin the middle and getting another icon with some information on the right side of the action bar as follows:

I can make the image in the middle already, but the problem
- when I inflate the layout in
onCreateOptionMenu()and set showAsAction="always", the right view accepts all the spaces of the action bar like this

but if I set it to "never", the middle image will appear, but the menu item will disappear
here is my code:
private void setActionBar(){
actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
LayoutInflater linflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = linflater.inflate(R.layout.actionbar_layout, null);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
view.setLayoutParams(lp);
actionBar.setCustomView(view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.actionbar_right_icon, menu);
MenuItem menuItem = menu.findItem(R.id.user_info);
View rightIcon = menuItem.getActionView();
RelativeLayout rightIconLayout = (RelativeLayout) rightIcon.findViewById(R.id.actionbar_right_icon);
TextView user = (TextView) rightIconLayout.findViewById(R.id.actionbar_user);
TextView id = (TextView) rightIconLayout.findViewById(R.id.actionbar_id);
user.setText("User:test");
id.setText("ID:xxxxx");
return true;
}
menu / actionbar_right_icon
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/user_info"
android:actionLayout="@layout/actionbar_right_icon_layout"
android:icon="@drawable/user_icon"
android:showAsAction="always"
/>
</menu>
how can i fix this?
early
source
share