Home > Back-end >  How to align to right on the same column in android layout
How to align to right on the same column in android layout

Time:05-10

I have 2 text view and i want them to align horizontally at the same column. I tried to android:layout_toRightOf="@id/textName" but it seem like it goes to the bottom instead beside the textName

enter image description here

Expected output: The MovieDate should align beside the MovieName

enter image description here

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

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

            <ImageView
                android:id="@ id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                android:src="@drawable/candy" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@ id/textName"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Name"
                    android:textColor="#000"
                    android:layout_marginRight="100dp"
                    android:textSize="15sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@ id/textdate"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Date"
                    android:textSize="15sp"
                    android:layout_toRightOf="@id/textName"/>


            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

CodePudding user response:

layout_toRightOf is an RelativeLayout param, it is no-op set for child of LinearLayout, so you can safely remove tihs line, it does nothing...

LinearLayout which is a parent of both TextViews should have set android:orientation="horizontal", thats for shure... but I see a lot of problems and potential improvements in your code, so maybe you should also set android:layout_height="wrap_content" for this LinearLayout and android:gravity="center_vertical" for outer one (first child of CardView), also remove android:layout_weight="2", its unnecessary as this layout have match_parent width

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

        <ImageView
            android:id="@ id/imageview"
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:layout_margin="10dp"
            android:src="@drawable/candy" />

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

            <TextView
                android:id="@ id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:layout_marginRight="100dp"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@ id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Movie Date"
                android:textSize="15sp"/>

        </LinearLayout>

    </LinearLayout>

CodePudding user response:

Use LinearLayout and set orientation to horizontal instead . See this

  • Related