Balkyto
Balkyto

Reputation: 1510

Align android text and images in listview to be the same

I need some help with alignment of custom list view with image and text.

I'm trying with this:

<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="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center"
        android:paddingTop="20dp"
        android:layout_weight="2"
        android:text="TextView" />
</LinearLayout>

And I thought that weight is keeping the ratio of element, not the content of the element, but apparently it takes care about things inside the text view, so my list looks messy.

How to align it to be the same, no matter how long the text is in text view?

My Crazy alignment

So the green line should be straight vertical line, no matter of text on right hand side.

Tnx

Upvotes: 0

Views: 3349

Answers (4)

Benito Bertoli
Benito Bertoli

Reputation: 25793

Remove android:layout_weight.

This will align you images since you define the width.

If you want to align the text too, remove android:gravity or set it to left.

You can add android:marginRight to the imageView for better readability.

Upvotes: 0

Jens
Jens

Reputation: 17077

Basically, your layout should be this:

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center"
    android:paddingTop="20dp"
    android:layout_weight="2"
    android:drawableLeft="@drawable/ic_launcher"
    android:text="TextView" />

The android:drawableLeft="@drawable/ic_launcher" defines the leftmost compound drawable, and it will align nicely.

example

Upvotes: 3

Samer
Samer

Reputation: 536

Try this instead:

<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="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="2"
        android:text="TextView" />
</LinearLayout>

Upvotes: 0

njzk2
njzk2

Reputation: 39386

use

android:layout_height="0dp"

in your textView. it will give fixed size to all your elements. here, the size shared by the weight param is variable as it depends on the amount of text.

Upvotes: 0

Related Questions