2

I've made an XML which include a TextView having a textAlignement:center in the following code :

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">
        <Button
            android:layout_width="0px"
            android:layout_weight="0.24"
            android:text="gestion"
            android:layout_height="wrap_content"
            android:id="@+id/GoGestion"/>

        <View
            android:layout_width="0px"
            android:layout_weight="0.01"
            android:layout_height="match_parent"></View>
    <TextView
        android:layout_width="0px"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="Creation Sav"
        android:textSize="40dp"
        android:layout_gravity="center"
        android:textAlignment="center"/>

        <View
            android:layout_width="0px"
            android:layout_weight="0.01"
            android:layout_height="match_parent"></View>
     <Button
            android:layout_width="0px"
            android:layout_weight="0.24"
            android:text="Archive"
            android:layout_height="wrap_content"
            android:id="@+id/GoArchive"/>
    </LinearLayout>

And the fact is that My textView isn't centered on a tablet having 4.1.2 version (jellybean) but is on the other tablet 4.4.2 (kitkat).
My app compiles with minSdkVersion 15 so jellybean is more recent. Nowhere in the doc is specified that Jellybean does not run textAlignement.

Why is it not working on jelylbean and how can I fix this?

3 Answers3

5

You can set this in your TextView

 android:gravity="center"

Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

Please check Gravity and layout_gravity on Android . Hope this helps .

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
4
  <TextView
        android:layout_width="0px"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="Creation Sav"
        android:textSize="40dp"
        android:layout_gravity="center"
        android:textAlignment="center"/>

change to

<TextView
        android:layout_width="0px"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="Creation Sav"
        android:textSize="40dp"
        android:gravity="center"
        android:textAlignment="center"/>

android:gravity sets the gravity of the contents wiz.subviews inside container.

android:layout_gravity sets the gravity of the View/Layout relative to its parent Layout/Container. Everything with layout_ defines something that effects the elements outside.

Madhur
  • 3,303
  • 20
  • 29
0

Put this code

 <?xml version="1.0" encoding="utf-8"?>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">

        <Button
            android:id="@+id/GoGestion"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.24"
            android:text="gestion" />

        <View
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="0.01"></View>

        <TextView
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.5"
            android:text="Creation Sav"
            android:gravity="center"
            android:textAlignment="center"
            android:textSize="40dp" />

        <View
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="0.01"></View>

        <Button
            android:id="@+id/GoArchive"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.24"
            android:text="Archive" />
    </LinearLayout>
Suhas Bachewar
  • 1,230
  • 7
  • 21