Swamy Veera
Swamy Veera

Reputation: 237

how to display image to right side of cardview in android

I am trying to display images inside CardViews like in the example below. But my ImageView is displayed on the left side. How do I change it to the right alignment? Here is my XML code.

XML Layout:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_margin="8dp"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp">

<ImageView
    android:id="@+id/img_thumbnail"
    android:layout_width="235dp"
    android:layout_height="130dp"
    android:scaleType="fitXY"

    android:src="@drawable/imag_bg" />

<TextView
    android:id="@+id/tv_nature"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:text="TextView"
    android:textColor="#FFFFFF"
    android:layout_alignParentLeft="false"
    android:layout_alignParentStart="true" />

Example of what I want:

here my sample image i need like this

Upvotes: 1

Views: 5175

Answers (2)

Jefferson Tavares
Jefferson Tavares

Reputation: 991

This is probably what you're looking for... Add in the line android:layout_alignParentRight="true"

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/your_image_path 
            android:layout_alignParentRight="true"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:layout_alignParentLeft="true"
            android:align_toLeftOf="@+id/image_view" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

Upvotes: 2

Mukesh Rana
Mukesh Rana

Reputation: 4091

Just use the following attribute for your ImageView.It will display the image to the right.

android:layout_gravity="right"

Upvotes: 3

Related Questions