Andrew
Andrew

Reputation: 1833

Fixed Columns in an Android Textview

I am trying to build a listview in android but have four columns that are all equal. I am using android:layout_width="0dp" and android:layout_weight="1" which does give me equal columns, until one of the fields has text greater than it's column's allotted width.

I have been trying to find a solution that would fix the weight of the column so that the extra text would ideally move to the next row.

This is my layout for the listview:

<?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:weightSum="4">

    <TextView
    android:id="@+id/txvService"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:singleLine="false"/>

    <TextView
    android:id="@+id/txvScheduleDate"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:singleLine="false" />

    <TextView
    android:id="@+id/txvQty"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:singleLine="false" />

    <TextView
    android:id="@+id/txvBalance"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:singleLine="false" />

</LinearLayout>  

Any ideas would be appreciated.

Upvotes: 1

Views: 695

Answers (2)

advdev1234
advdev1234

Reputation: 30

If you add

android:weightSum="4"
android:orientation="horizontal"

to the LinearLayout opening tag just under its height attribute, it will cause the Linear Layout to allocate room for a total weight of 4 horizontally across the space allotted to it. From here, each element can have a weight of 1, which will cause each to hold 1/4 of the space. You can also use the weight/weightSum attributes to place elements with varied widths across the linear layout, ie: two elements with weights 1 and 3 would arrange the first with 1/4 of the horizontal space and the second with 3/4.

Upvotes: 1

Joseph
Joseph

Reputation: 6031

When using a LinearLayout you need to add an orientation attribute if you more than one child. Use android:weightSum to proportionately divide the screen space. In your case it will be android:weightSum=4

Upvotes: 1

Related Questions