Alex DG
Alex DG

Reputation: 1869

TextView gravity attribute doesn't work

I have a textView in a LinearLayout which displays its text not always in center, even if I use gravity="center" attribute. Also I've set up layou_width with "match_parent.

This textView is in a LinearLayout which is in a LinearLayout which is in the parent LinearLayout of the view.

My textView is in a ListView and represents the end of the item line.

My adapter uses a ViewHolder pattern.

Here I really don't understand why sometime text is center and sometime it's on the left.

Here 2 screenshots of my view with different states where you can see the alignment issue:

example1

example2

example3

What I would like to have: Here it's just about the text Now or the time which is displayed at this place, I would like to display this text only in center and sometimes it's displayed on the left as you can see on the screenshots above.

good result

ListView layout:

<ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/dialogTitle"
                    android:listSelector="@drawable/listitem_selector"
                    tools:listitem="@layout/item_customer_in_queue">
                </ListView>

Items layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/item_in_queue_list"
              android:layout_width="match_parent"
              android:layout_height="@dimen/in_queue_item_height"
              android:minHeight="@dimen/in_queue_item_height"
              android:background="@color/bckg"
              android:orientation="vertical">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.95">

        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent">

            <ImageView.../>
            <ImageView.../>
            <ImageView.../>

        </RelativeLayout>
        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent">

            <ImageView.../>
            <ImageView.../>

        </RelativeLayout>

        <TextView.../>
        <TextView.../>
        <TextView.../>

        <LinearLayout                                 <=here my LinearLayout of my issue
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <View
                    android:layout_width="fill_parent"
                    android:layout_height="20dp"
                    android:layout_weight="0.1"
                    android:layout_gravity="bottom"/>
            <TextView
                    android:id="@+id/list_dueTime"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"                 <=here my issue
                    android:text="due"
                    android:layout_weight="0.8"
                    android:textColor="@color/dark_grey"
                    android:textAppearance="?android:attr/textAppearanceSmall" />
         </LinearLayout>

    </LinearLayout>

    <ProgressBar.../>

</LinearLayout>

Upvotes: 1

Views: 779

Answers (3)

Make a layout xml and paste the below code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:weightSum="1" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_weight="0.4"
        android:background="#f0f0f0" >

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_weight="0.6"
        android:background="#f0f0f0"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/txt_now"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:textSize="18sp"
            android:text="Now" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:text="12 Mins Overdue"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

</LinearLayout>

Upvotes: 0

Patrick Chan
Patrick Chan

Reputation: 1029

When you are talking about align "center", the center is determined by the width of the view. To help you debugging, you can add a colored background to the View and see its boundary.

Upvotes: 0

Yoganand.N
Yoganand.N

Reputation: 907

TRY THIS: In this case i have provided the gravity to the text view's parent and changed the text view width to wrap content . . . . . .

 <LinearLayout                                
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
                android:gravity="center"  
            android:orientation="vertical"> 
        <View 
                android:layout_width="fill_parent" 
                android:layout_height="20dp" 
                android:layout_weight="0.1" 
                android:layout_gravity="bottom"/> 
        <TextView 
                android:id="@+id/list_dueTime" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="due" 
                android:layout_weight="0.8" 
                android:textColor="@color/dark_grey" 
                android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 

. . . .

Upvotes: 3

Related Questions