I am trying to create a custom action bar for my app. I want the app-name to appear in the center of the the action bar, on the left side I want to have a menu item, and on the right side I want to have a custom button. The problem is when I add the menu item, the app-name is displaced from the center towards right side as if something is pushing it there, how can I fix this issue, such that the app name will always remain in the center? Thank you!
Here is my action-bar layout 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="wrap_content"
android:background="@color/colorWhite">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/action_bar_text"
android:gravity="center"
android:layout_centerHorizontal="true"
android:textColor="@color/colorBlack"
android:textStyle="bold"
android:textSize="16sp"
android:text="@string/app_name"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/customButton"/>
</RelativeLayout>
Here is my menu-item xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.rashish.myapplication.MainActivity">
<item
android:id="@+id/back_action"
android:orderInCategory="100"
android:title="@string/back"
app:showAsAction="always"
/>
<item
android:id="@+id/app_icon"
android:orderInCategory="100"
android:title=""
android:icon="@drawable/logo"
app:showAsAction="always"
android:enabled="false"
/>
</menu>
This is how I set the custom action bar:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorWhite)));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
This is how I add the menu items:
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.menu_main, mMenu);
MenuItem itemBack = mMenu.findItem(R.id.back_action);
MenuItem appIconItem = mMenu.findItem(R.id.app_icon);
itemBack.setVisible(false);
appIconItem.setVisible(true);
return true;
}
UPDTAE
The new layout file using Mike M. suggested solution. The button is placed correctly however the text with the title is still displaced towards the button.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
android:id="@+id/action_bar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
android:textSize="20sp"
android:text="@string/app_name"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:text="click me"/>
</android.support.v7.widget.Toolbar>
UPDATE
This the screenshot of my result.
This is the result that I get, it seems to me that the App name is pushed towards the button rather then being centered, and it seems that the toolbar layout isn't full width.