0

I have created custom titlebar with my own layout to it. I would like to have displayed text in title bar aligned to horizontal center (= the same as default titlebar). But this is not working for me, my text is still alligned to the top of fitlebar, not to the centre. Could you give me some advice?

I am using this code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/top_back">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/title"
        android:layout_gravity="center_vertical"
        android:paddingLeft="15dip"
    />
</LinearLayout>
Waypoint
  • 17,283
  • 39
  • 116
  • 170

5 Answers5

3

For simple understanding about gravity and layout_gravity:

android:gravity => sets the gravity of the content of the View its used on.

android:layout_gravity => sets the gravity of the View or Layout in its parent.

For more: Gravity and layout_gravity on Android

Now, you need to include android:gravity="center_horizontal" inside your <TextView>.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1

Cange the android:layout_height="match_parent" to "wrap_content" . IT will work

 <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:text="center"
    android:layout_gravity="center"></TextView>
mainu
  • 625
  • 6
  • 14
0

This will work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/top_back">
<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/title"
    android:gravity="center_vertical"
    android:paddingLeft="15dip"
/>

By changing, android:layout_width and android:gravity

Nermeen
  • 15,883
  • 5
  • 59
  • 72
0

You may use RelativeLayout instead of LinearLayout. RelativeLayout lets work better with the atributes of the view.

Use this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/top_back">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/title"
        android:layout_alignParentTop = "true"
        android:layout_centerHorizontal = "true"
    />
</RelativeLayout>

In the code i have changed the last lines of your XML for this:

   android:layout_alignParentTop = "true"
   android:layout_centerHorizontal = "true"

The first line set the TextView on the top of the view, and the second line sets center horizontally at the top.

Victor
  • 193
  • 1
  • 2
  • 10
0

Change android:layout_gravity="center_horizontal"

Please Have a look at the code, Need to do exactly same way to make it in center.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.playing_video);  // Actual Layout Name

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.custom_layout); // Layoutname of Title Text

    TextView titleText= (TextView) findViewById(R.id.title);


    titleText.setText("Your Text");

}

It worked for me, Let me know if it works for you.

moDev
  • 5,248
  • 4
  • 33
  • 63