Karan
Karan

Reputation: 11671

Android: Show 2 TextViews horizontally in each row of a ListView

I'm creating a custom ArrayAdapter in order to show more than 1 TextView in a ListView (Customizing Android ListView Items with Custom ArrayAdapter). My aim is to show them in a table-like format (with 2 columns), kind of like this:

A     B
----------
C     D
----------
E     F

However, I keep getting them in a vertical format, like this:

A    
B
----------
C     
D
----------
E     
F

Here is my xml for the activity:

<?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" >

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

and here is the list_view.xml

<?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" >

<TextView android:id="@+id/product_name"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>   

<TextView android:id="@+id/status"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>           

 </LinearLayout>

I'm not too familiar with fill_parent and wrap_content, maybe I'm doing something wrong with them?

Upvotes: 3

Views: 1947

Answers (3)

Pratik Dasa
Pratik Dasa

Reputation: 7439

//Change your layout as per below:

<?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" >

<TextView android:id="@+id/product_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>   

<TextView android:id="@+id/status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>           

 </LinearLayout>

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Change this

  android:orientation="vertical"

To

  android:orientation="horizontal"

And use margin's for spacing between the views. Also make the below changes

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

Edit:

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

<TextView android:id="@+id/product_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:text="A"
    android:textSize="16dip"
    android:textStyle="bold"/>   

<TextView android:id="@+id/status"
    android:layout_marginLeft="30dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:text="B"
    android:textSize="16dip"
    android:textStyle="bold"/>           

 </LinearLayout>

snap

enter image description here

Upvotes: 3

Prachi
Prachi

Reputation: 3672

you have put android:orientation="vertical" in your row layout. make it "horizontal"

Upvotes: 2

Related Questions