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,366
  • 15
  • 42
  • 50
  • 2
    Set `android:gravity="center"` for textview. – Piyush Apr 24 '15 at 11:00
  • @PiyushGupta no effect still the same – Umair Apr 24 '15 at 11:01
  • Have you used `getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);` in your code? – Piyush Apr 24 '15 at 11:03
  • yup I have used it as the answer to the question suggested but there was no effect to the text ... – Umair Apr 24 '15 at 11:07
  • Try to set `getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); and getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE );` both. – Piyush Apr 24 '15 at 11:14
  • @PiyushGupta yes I tried both but it didn't had any effect on the text. that why I posted the question here no other solutions are working :) – Umair Apr 24 '15 at 11:16
  • Set `android:layout_gravity="center"` for `LinearLayout` too. – Piyush Apr 24 '15 at 11:24
  • @PiyushGupta alas no effect . – Umair Apr 24 '15 at 11:29
  • follow this tutorial http://javatechig.com/android/actionbar-with-custom-view-example-in-android – Chandru Apr 24 '15 at 12:20
  • @Chandru yeah that code is right nothing is wrong with it but it wasn't upto my requirements, Maybe that problem occurs with some devices ;) – Umair Apr 24 '15 at 12:39

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,386
  • 4
  • 45
  • 38
  • 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.