HyperX
HyperX

Reputation: 1211

CardView weight with ImageView

I am trying to create a CardView layout where image takes 2/3 of layout and other 1/3 should be textview. But somehow i am getting weird results where weight is never used it always seems that image is taking its space no matter what i set for weight

Here is my current layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:orientation="horizontal"
    android:padding="3dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/testing"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher_background"
                app:layout_constraintBottom_toTopOf="@+id/info_text"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_weight="2" />

            <TextView
                android:id="@+id/info_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Testing Text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/testing"
                app:layout_constraintVertical_weight="2" />

        </android.support.constraint.ConstraintLayout>

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


</android.support.constraint.ConstraintLayout>

So i always want that weight is used instead of any fixed size.

Upvotes: 0

Views: 154

Answers (2)

Kinjal
Kinjal

Reputation: 142

Try this it might help you

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        android:layout_weight="1"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <ImageView
                android:id="@+id/testing"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher_background" />

            <TextView
                android:id="@+id/info_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="Testing Text" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

Upvotes: 0

Jaymin
Jaymin

Reputation: 2912

Set android:layout_height="0dp" and set the ratio like app:layout_constraintDimensionRatio="3:3". It will fix your height issue.

Here is the solution:

<ImageView
   android:id="@+id/testing"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:adjustViewBounds="true"
   android:contentDescription="@null"
   android:scaleType="centerCrop"
   android:src="@drawable/ic_launcher_background"
   app:layout_constraintBottom_toTopOf="@+id/info_text"
   app:layout_constraintDimensionRatio="1:1"
   app:layout_constraintHorizontal_bias="0.5"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintVertical_weight="2" />

Upvotes: 1

Related Questions