log0
log0

Reputation: 2474

How do I format a TextView in a custom ListView in Android

I want to show a TextView, placed in center of the row, be separated in two or more lines. as shown here but I am only able to do

this.

here is my custum_adapter.xml file

 <?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"
android:padding="20dp">



   <TextView
       android:text="magnitude"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/magnitude"
       android:layout_marginLeft="15sp"
       android:textSize="20sp"
       android:layout_alignParentLeft="true"/>
   <TextView
       android:text="place"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:inputType="textMultiLine"
       android:textSize="20sp"
       android:layout_centerHorizontal="true"
       android:id="@+id/place" />

   <TextView
       android:text="date"
       android:inputType="textMultiLine"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/date"
       android:gravity="center_horizontal"
       android:textSize="20sp"
       android:layout_marginRight="15sp"
       android:layout_alignParentRight="true" />
 </RelativeLayout>

I want that my UI should look like the fist image. (I have already seen this these questions 1 , 2, and a few others but none could answer mine)

EDIT: I have asked about formatting the data in a custom ListView when there are more than two items in a row.

Upvotes: 1

Views: 1268

Answers (4)

Nickan
Nickan

Reputation: 466

This Should Work For You :

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="30dp"


        <TextView
           android:text="magnitude"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:gravity="center"
           android:id="@+id/magnitude"
           android:layout_marginLeft="15sp"
           android:textSize="15sp"
          />

        <TextView
           android:text="place"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="0.4"
           android:gravity="center"
           android:inputType="textMultiLine"
           android:textSize="15sp"
           android:id="@+id/place" />

        <TextView
           android:text="date"
           android:inputType="textMultiLine"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="0.4"
           android:id="@+id/date"
           android:gravity="center"
           android:textSize="15sp" />


    </LinearLayout>

</LinearLayout>

Upvotes: 0

Vishal Puri
Vishal Puri

Reputation: 843

Use weights with linearLayout. It'll solve your problem and they will never overlap. copy paste the code below. You can change the value of weights according to your needs.

<?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:padding="20dp"
android:orientation="horizontal">


<TextView
    android:id="@+id/magnitude"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="magnitude"
    android:textSize="20sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".6"
    android:orientation="horizontal">
<TextView
    android:id="@+id/place"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_weight="1"
    android:inputType="textMultiLine"
    android:text="place weifjn eiunf indfg dkjmvd jdfokjdfodf"
    android:textSize="20sp" />

<TextView
    android:id="@+id/date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:inputType="textMultiLine"
    android:text="date"
    android:textSize="20sp" />
</LinearLayout>
</LinearLayout>

Upvotes: 0

Dileep Patel
Dileep Patel

Reputation: 1988

Try to this hope this can help you

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="10dp"
    android:weightSum="1">

    <TextView
        android:id="@+id/magnitude"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:text="magnitude"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/place"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".50"
        android:inputType="textMultiLine"
        android:text="place"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".30"
        android:inputType="textMultiLine"
        android:text="date"
        android:textSize="20sp" />
</LinearLayout></LinearLayout>

Upvotes: 1

earthw0rmjim
earthw0rmjim

Reputation: 19417

Instead of the RelativeLayout you could just use a horizontal LinearLayout with the appropriate layout_weight attributes.

Something like this:

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

    <TextView
        android:id="@+id/magnitude"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="magnitude"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/place"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="place"
        android:gravity="center_horizontal"
        android:inputType="textMultiLine"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/date"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="date"
        android:inputType="textMultiLine"
        android:gravity="center_horizontal"
        android:textSize="20sp"/>
</LinearLayout>

Upvotes: 1

Related Questions