0

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.

Community
  • 1
  • 1
Umair
  • 6,078
  • 15
  • 42
  • 48

2 Answers2

1

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>
Silambarasan Poonguti
  • 9,190
  • 3
  • 43
  • 37
  • I tried your code it wasn't helping but then I only wrote the xml from you answer and it worked... thanks – Umair Apr 24 '15 at 11:59
  • can you edit your answer and only write xml here so that I can accept it ? – Umair Apr 24 '15 at 12:00
0
<?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.