Search code examples
androidandroid-layoutlistviewalignment

Align linear layouts to the left & right in listview custom row


Here is the situation : What i'm trying to do

I'm only using LinearLayouts (see code below), and I need the right box to be always stuck to the far right side of the screen. If i use my device in portrait mode, it's fine, but when i turn to landscape or simply go on a larger device, its just ugly.

What can I do? I've tried a 100 things with gravity/layout_gravity/relative layouts and it just does not work.

Note : This will end as a row in a list. The problematic linear layout contains two textview which are pretty small (~30dp wide) and on top of each other.

Here is the code for the above screenshot :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
    android:id="@+id/imgCtrl"
    android:layout_width="68px"
    android:layout_height="68px"
    android:layout_gravity="center"
    android:contentDescription="@string/imgTransport"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/lbDir"
        android:layout_width="180dp"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_gravity="top"
        android:layout_weight="8"
        android:hint="TestValueForDir"
        android:paddingLeft="5dp"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/lbStop"
        android:layout_width="180dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_weight="8"
        android:hint="TestValueForStop"
        android:paddingLeft="5dp"
        android:textSize="16sp" />
</LinearLayout>

<LinearLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/lbTime"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="2"
        android:gravity="right"
        android:paddingLeft="20dp"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/txRating"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_gravity="right"
        android:gravity="center"
        android:textSize="15sp"
        android:textColor="@android:color/white" />

</LinearLayout>


Solution

  • Try this way,hope this will help you to solve your problem.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp" >
    
        <ImageView
            android:id="@+id/imgCtrl"
            android:layout_width="68dp"
            android:layout_height="68dp"
            android:contentDescription="@string/imgTransport"
            android:src="@drawable/ic_launcher" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp" >
    
            <TextView
                android:id="@+id/lbDir"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:hint="TestValueForDir"
                android:paddingLeft="5dp"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/lbStop"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:hint="TestValueForStop"
                android:paddingLeft="5dp"
                android:textSize="16sp" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp"
            android:gravity="center_vertical|right">
    
            <TextView
                android:id="@+id/lbTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:textSize="15sp" />
    
            <TextView
                android:id="@+id/txRating"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:textSize="15sp"
                android:textColor="@android:color/white" />
        </LinearLayout>
    </LinearLayout>