Reputation: 3071
How can I have actionbar text set to the center. I have used below code to set text to center:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:text="Example"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:gravity="center" />
</RelativeLayout>
Activity:
ActionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom, ActionBarDisplayOptions.ShowCustom);
LayoutInflater inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View v = inflater.Inflate(Resource.Layout.layout_actionbar_centerTitle, null);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent,
GravityFlags.Center);
var title = ((TextView)v.FindViewById(Resource.Id.title));
title.Text = AgliveResource.ContactUs;
ActionBar.SetCustomView(v, p);
ActionBar.SetDisplayShowTitleEnabled(true);
ActionBar.SetDisplayHomeAsUpEnabled(true);
This works perfectly when I have hamburger menu to right along with back icon to left. But there are some screens which does not require hamburger menu, at that time the text does not appear correctly at center.
Image 1 with menu: Image 2 without menu:
Upvotes: 1
Views: 2896
Reputation: 1552
You probably need to change textview width android:layout_width="match_parent"
to android:layout_width="wrap_content"
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/navBar"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/app_color_Brown">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/backBtn"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="@drawable/back_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_centerInParent="true"
android:text="Privacy Policy"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Upvotes: 2
Reputation: 2600
Give a margin of 56dp to the right if you do not have a hamburger menu
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="56dp"
android:gravity="center">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:text="Example"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:gravity="center" />
</RelativeLayout>
Upvotes: 4