Umair
Umair

Reputation: 6426

Custom ActionBar title is not being center aligned

I am creating a custom actionBar. everything is working fine but I have failed to put the text in the center of the actionBar. Searched the internet for the solutions there were many of them but none for me. Can anyone help me to achieve it. I tried to implement the following question's answer but it also had no effect.How to center align the ActionBar title in Android?

Though I can put the text in center by giving the padding to textview but I think that's not the proper approach.

Below is the code:

Custom ActionBar Code:

private void createCustomActionBarTitle()
{
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8B58A3")));

    LayoutInflater inflater = LayoutInflater.from(this);
    View v = inflater.inflate(R.layout.titleview, null);

    TextView frag1 = (TextView)v.findViewById(R.id.titleFragment1);
    frag1.setTypeface(vivaldiiFont);

    actionBar.setCustomView(v);
}

And xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:paddingTop="7dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/titleFragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Title"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:layout_gravity="center" />

This is how the actionBar is now

enter image description here

this text should be in center not towards left.

Upvotes: 0

Views: 974

Answers (2)

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

Try this...

1. custom_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="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_light" >

<TextView
    android:id="@+id/apptitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:padding="8dp"
    android:text="@string/app_name"
    android:textColor="#FFFFFF"
    android:textSize="16sp" />

 </RelativeLayout>

Upvotes: 1

Basant Kumar
Basant Kumar

Reputation: 66

<?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:orientation="horizontal"
android:background="@drawable/header" > 

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:id="@+id/mytext"
    android:textColor="@color/header_text_color"
    android:textSize="18sp"
    android:textStyle="bold"/>
 </RelativeLayout> 

Do it like this you will get as you want.

Upvotes: 0

Related Questions