0

i have a list item and i would like to style 2 (or more) text views in one line.

------------------------------------------------
|                                              |    
| TEXT_VIEW_1: FOO    TEXT_VIEW_2: BAR         |   
|                                              | 
|                                              |
------------------------------------------------

How to do it most simple and effective?

Thanks for any advice.

redrom
  • 11,502
  • 31
  • 157
  • 264
  • It's all explained in this thread. You just need to modify XML that textviews are side by side and you are done: https://stackoverflow.com/questions/15832335/android-custom-row-item-for-listview – Boris Knez Mar 18 '14 at 10:27

3 Answers3

2

Create your list item layout as below...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="FOO" />

     <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BAR" />

</LinearLayout>

If you want to have more than 2 TextView then just add android:layout_weight="1" attribute to them. If all TextView contains android:layout_weight="1" then this attribute will distribute layout width equally to all Textviews.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • by setting weight may effect with the content of the textview. consider one as long text and the other a small it may affect the view. – Ashwin S Ashok Mar 18 '14 at 10:34
  • 1
    If `android:layout_weight="1"` is set to all `TextView` then long will not cause any problem. – Hamid Shatu Mar 18 '14 at 10:36
  • it will effect try to fill the first textview with large text and the second with small. and one thing ,its a listview so we need to show the list in an aligned manner. – Ashwin S Ashok Mar 18 '14 at 10:38
  • At first, the parent `LinearLayout` will collect all the `weight` then it will divide its `width` with the sum of `weight` and then divided result will set to all `TextViews` as `layout_width`. – Hamid Shatu Mar 18 '14 at 10:45
0

Use orientation of LinearLayout as Horizontal:

and provide weights:

<?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="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text2"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text3"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
Lokesh
  • 5,180
  • 4
  • 27
  • 42
0

use below code for list item.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightsum="2" >

<TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="FOO" />

 <TextView
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="BAR" />

</LinearLayout> 

And make both textview singleLine true.

Cheerag
  • 415
  • 3
  • 10