Question

I am developing an app. In that I am getting data from webservice and I am inserting it in TextView. But, the alignment of the textview is getting scrambled. Please check the image.

First 3 rows(black colored) is in proper order, from the 4th it is scrambled. I googled on it so many times but not getting the exact solution as I wanted.

enter image description here

XML 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="70dp"
android:orientation="horizontal"
>

     <TextView
        android:id="@+id/schedule_1_textView_day_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="KKR"
        android:layout_weight="1.5"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"            
        android:textSize="7sp"
        />        

    <TextView
        android:id="@+id/schedule_1_textView_matchno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:layout_weight="1.5"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp"
        />        

    <TextView
        android:id="@+id/schedule_1_textView_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="6"
        android:textColor="@color/Black"
        android:textSize="7sp"
        />   

    <TextView
        android:id="@+id/schedule_1_textView_team1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="4"
        android:textSize="7sp"
        android:textColor="@color/Black"
        />       

    <TextView
        android:id="@+id/schedule_1_textView_team2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_weight="1"
        android:layout_marginTop="10dp"          
        android:textColor="@color/Black"
        android:textSize="7sp"
        />       

    <TextView
        android:id="@+id/schedule_1_textView_venue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp"
        /> 

    <TextView
        android:id="@+id/schedule_1_textView_blank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"
        android:layout_marginTop="20dp"
        android:textColor="@color/Black"
        android:textSize="6sp"
        android:visibility="invisible"
        /> 


 </LinearLayout>
Was it helpful?

Solution

Try this..

You forget to add android:layout_weight="1" for below two TextView Use android:singleLine="true" for all TextView

<TextView
    android:id="@+id/schedule_1_textView_venue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="6"
    android:layout_marginTop="10dp"
    android:textColor="@color/Black"
    android:textSize="7sp"
    /> 

<TextView
    android:id="@+id/schedule_1_textView_blank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="B"
    android:layout_marginTop="20dp"
    android:textColor="@color/Black"
    android:textSize="6sp"
    android:visibility="invisible"
    /> 

OTHER TIPS

you might wanna assigh android:layout_weight property to the TextViews like:

<TextView
    android:id="@+id/schedule_1_textView_venue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="6"
    android:layout_marginTop="10dp"
    android:textColor="@color/Black"
    android:textSize="7sp" />

you will need to assign weight according to your requirement. i.e for bigger views you want to assign higher values.

[EDIT] and for the last TextView you may want to set android:layout_gravity="center|right" and android:layout_gravity="center|left" for the first for the text alignment for the rest you should use android:layout_gravity="center"

Add gravity to your TextView like:

android:gravity="center_vertical|center_horizontal"

change gravity to left, right or center as you need...

have you tried adding weight to schedule_1_textView_venue?

It's a little more work, but did you try using TableLayout? This is very useful tutorial that can help you organizing the correct order of views.

EDIT: I think i know what causes the mismatch of the TextViews. It comes from the different length of the values in your last column named VENUE. Take for example the values HYDEBARAD and DELHI - the first one is much longer than the second which makes all the TextViews on the row to go left. I am sure that if you put identical padding to the TextViews you will align them. For example: take your TextViews in MATCH column and write the following: myTextView.setPadding(40, 0, 0, 0) (padding on the left side) this will put them exactly in a straight row. Do the same with the rest TextViews with the proper padding value, take into consideration the length of the values in the other columns... It is a little bit tricky though. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top