asf
asf

Reputation: 355

Android: Align 3 TextViews in custom ListView

I have 3 texviews in my custom ListView. Each TextView in each row has their varying length (on the screen, max length of 1th - 3, another two - 14).

How can I align them in one line?

screen

My layout:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/number_calls_tv"
        android:singleLine="true"
        android:maxLines="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/time_calls_tv"
        android:singleLine="true"
        android:maxLines="1"
        android:layout_marginLeft="42dp"
        android:layout_marginStart="42dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/number_calls_tv"
        android:layout_toEndOf="@+id/number_calls_tv" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/break_calls_tv"
        android:singleLine="true"
        android:maxLines="1"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/time_calls_tv"
        android:layout_toEndOf="@+id/time_calls_tv"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp" />
</RelativeLayout>

Upvotes: 0

Views: 559

Answers (5)

AbhayBohra
AbhayBohra

Reputation: 2117

You can also try this without using linear layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/number_calls_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:singleLine="true"
        android:text="New Text" />

    <TextView
        android:id="@+id/time_calls_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@id/number_calls_tv"
        android:maxLines="1"
        android:singleLine="true"
        android:text="New Text" />

    <TextView
        android:id="@+id/break_calls_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:maxLines="1"
        android:singleLine="true"
        android:text="New Text" />
</RelativeLayout>

Upvotes: 0

Avinash Joshi
Avinash Joshi

Reputation: 1308

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

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/number_calls_tv"
    android:singleLine="true"
    android:maxLines="1" 
android:layout_weight="1"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/time_calls_tv"
    android:singleLine="true"
    android:maxLines="1" 
android:layout_weight="1"  />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/break_calls_tv"
    android:singleLine="true"
    android:maxLines="1"   
android:layout_weight="1"/>

you can play with the weights to get the exact sizes desired

Upvotes: 0

Saritha G
Saritha G

Reputation: 2608

Use LinearLayout instead of RelativeLayout.

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:layout_weight="1"
        android:id="@+id/number_calls_tv"
        android:singleLine="true"
        android:maxLines="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/time_calls_tv"
        android:singleLine="true"
        android:layout_weight="1"
        android:maxLines="1"
        android:layout_marginLeft="42dp"
        android:layout_marginStart="42dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/number_calls_tv"
        android:layout_toEndOf="@+id/number_calls_tv" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:layout_weight="1"
        android:id="@+id/break_calls_tv"
        android:singleLine="true"
        android:maxLines="1"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/time_calls_tv"
        android:layout_toEndOf="@+id/time_calls_tv"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp" />

   </LinearLayout>

Upvotes: 0

Nowshad
Nowshad

Reputation: 294

please look at this.......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="15dp"
    android:layout_marginLeft="15dp"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="left"
        android:gravity="center"
        android:layout_weight="2"
        android:id="@+id/leftTv" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="center"
        android:gravity="center"
        android:layout_weight="2"
        android:id="@+id/centerTv" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="right"
        android:gravity="center"
        android:layout_weight="2"
        android:id="@+id/rightTv" />
 </LinearLayout>

</RelativeLayout>

Upvotes: 0

Oleksandr
Oleksandr

Reputation: 902

Read more about layout weight.

You can use this sample code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

Upvotes: 4

Related Questions