Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic TextViews wrapping to next row/line in Linear layout

I am trying to build a UI, where in I have a linear layout which has been defined in the XML.

As per the user's input, I need to add some TextViews to this linear layout. I am able to do this.

My actual problem is, when I have more text views, they are stacked next to each other and some of text views text are hidden or stretched vertically as shown in the image below.

please see this image (Current)

I would like to use the whole width of the linear layout and if the text view can not fit in this row, it should be put in a new row or below the first text view.. I would like the display to be as below.

I wish, I can do this way

Following is my Linear layout configuration in XML:

    <RelativeLayout
        android:id="@+id/RL1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingBottom="5dip"
        android:paddingTop="5dip" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="85dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="left"
            android:text="Lable 1"
            android:textColor="#999999" />

        <LinearLayout
            android:id="@+id/LL1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/button1"
            android:layout_toRightOf="@+id/text1"
            android:gravity="left|center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:paddingTop="3dip" >
        </LinearLayout>

        <Button
            android:id="@+id/button1"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="2dip"
            android:background="@drawable/plus"
            android:clickable="true"
            android:focusable="true"
            android:paddingTop="5dip" />
    </RelativeLayout>

Can any one please help me in how to realign the same in java code.

like image 560
Vinay Avatar asked Feb 28 '12 19:02

Vinay


1 Answers

I would Like to suggest you about how you can provide equal sizes to all your views in Linear Layout,you can do that by using weight property in the XML file i.e., android:weight for that particular View and when you use this property you should give width=0dp or 0dip.I think this will solve your problem easily.

Please I suggest you first you take full Knowledge of how to use this property in the following Link:-Weight property with an example

like image 116
Hardik Vora Avatar answered Nov 06 '22 20:11

Hardik Vora