user2085965
user2085965

Reputation: 403

android:layout_weight property changes according to the text length

I am working on Android project, where I have to show a dynamic list. Each list item contain 3 text view.

My problem is, the list is not displaying in a proper format. The weight property is changing dynamically.

Please look at the screen shot.

The Change% values(0.14, 0.01, -1.2) are not in vertically straight, they are in zig-zag.

I want the all company values, Change% values and LTP values should be vertically same order.

My Code: the layout which give for the adapter

<?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="match_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:id="@+id/custLayout"
        android:layout_height="50dp"
        android:layout_width="fill_parent">
         <TextView
            android:id="@+id/symbol_list"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Symbol"
            android:layout_weight="1"
            android:textColor="#FFFFFF"
            android:textSize="13dp" />

         <TextView
             android:id="@+id/persent"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="Chang"
            android:layout_weight="0.5"
            android:textColor="#FFFFFF"
            android:textSize="13dp" />

         <TextView 
             android:id="@+id/ltp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:gravity="center|right"
            android:layout_marginRight="15dp"
            android:text="ltp"
            android:layout_weight="0.5"
            android:textColor="#FFFFFF"
            android:textSize="13dp" />


    </LinearLayout>

</LinearLayout>

Please help.

enter image description here

Upvotes: 2

Views: 1027

Answers (2)

Piyush
Piyush

Reputation: 18923

Change it:

<?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="match_parent"
 android:orientation="vertical" >

 <LinearLayout 
    android:id="@+id/custLayout"
    android:layout_height="50dp"
    android:layout_width="fill_parent">
     <TextView
        android:id="@+id/symbol_list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Symbol"
        android:layout_weight="1"
        android:textColor="#FFFFFF"
        android:textSize="13dp" />

     <TextView
         android:id="@+id/persent"
         android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:text="Chang"
        android:layout_weight="0.5"
        android:textColor="#FFFFFF"
        android:textSize="13dp" />

     <TextView 
         android:id="@+id/ltp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:gravity="center|right"
        android:layout_marginRight="15dp"
        android:text="ltp"
        android:layout_weight="0.5"
        android:textColor="#FFFFFF"
        android:textSize="13dp" />


 </LinearLayout>

</LinearLayout>

Must remember that whenever you use weight you must use 0px for it.. If you are using vertical layout you must use 0px for its height and if you are using horizontal linear layout then you must use 0px for its width.

Upvotes: 3

laalto
laalto

Reputation: 152867

Replace the wrap_content widths with 0px.

Layout weight distributes any remaining space in proportion to weight. The wrap_content sizing takes place first and only then is the remaining space distributed.

Upvotes: 9

Related Questions