Praveen P.
Praveen P.

Reputation: 1106

Align element to the right in LinearLayout

I am trying to align the express badge to the right of the layout and I need the title to take as much width as possible. This is the code I have right now.

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:padding="@dimen/margin_large">

    <ImageView
      android:id="@+id/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      tools:src="@drawable/ic_download" />

    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:layout_weight="1"
      android:orientation="vertical"
      android:paddingStart="@dimen/margin_large"
      android:paddingEnd="@dimen/margin_large">
      
      <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
          android:id="@+id/title"
          style="@style/AppTheme.Text.Light.SemiDarker"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          tools:text="Example of title" />

        <com.client.presentation.components.ChipView
          android:id="@+id/badge"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
        
      </LinearLayout>

      <TextView
        android:id="@+id/subtitle"
        style="@style/AppTheme.Text.Body1.Light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Example of subtitle" />
    </LinearLayout>

  </LinearLayout>

enter image description here

Upvotes: 0

Views: 58

Answers (1)

Kamil Jeglikowski
Kamil Jeglikowski

Reputation: 192

The clue is to set weight to 1 in TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:minHeight="?attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:padding="@dimen/margin_large"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        tools:src="@drawable/ic_angry_android" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        android:paddingStart="32dp"
        android:paddingEnd="32dp">

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

            <TextView
                android:id="@+id/title"
                android:layout_weight="1"
                style="@style/AppTheme.Text.Light.SemiDarker"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                tools:text="Example of title" />

            <....

        </LinearLayout>

        <TextView
            android:id="@+id/subtitle"
            style="@style/AppTheme.Text.Body1.Light"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Example of subtitle" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions