Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix the width of a TextView

I have the following LinearLayout below. I use it for a row in a ListView.

I have set the Textview's weight to 1 so they should all be of equal width.

The problem is when some of the textviews have more characters in, the width of the textview changes. This has an overall effect on how the ListView appears, as in each row the data in not lined up correctly. (i have headings above the ListView).

when some of the TextViews have more text in them, there is still space either side of the text and the textview just gets bigger, which i don't want as i've set the weights to 1 each.

Is there a way of fixing the width of each textview so it doesn't change according to the length of text in it?

thanks in advance

Matt

<?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:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/rowstarttimeweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2" />

    <TextView
        android:id="@+id/rowplannedcallsweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/rowncrsweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/rowqasweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" 
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/rowplannedhoursweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:33"
        android:layout_weight="1"
        android:gravity="right"/>

    <TextView
        android:id="@+id/rowactualcallsweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" 
        android:layout_weight="1"
        android:gravity="right"/>

    <TextView
        android:id="@+id/rowactualhourssweeksum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2:01" 
        android:layout_weight="1"
        android:gravity="right"/>


</LinearLayout>
like image 617
turtleboy Avatar asked Jan 07 '16 10:01

turtleboy


1 Answers

One small change and your problem is solved

change width of all TextView from wrap_content to 0dp

android:layout_width="0dp"

Whenever you will set height/width to 0dp, it will manage height/width of view as per android:layout_weight

like image 133
Ravi Avatar answered Oct 24 '22 15:10

Ravi