I have a CardView in a LinearLayout. The Layout reserves 1/5 of the screen for the CardView and 4/5 of the screen for other stuff.
I would like the cardview to be square.
I set the CardView's height to "match_parent". But how do I set it's width to match it's height?
Am I going to have to create a custom View and use OnMeasure(), or is there a simpler way?
Here is my view's XML:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >
<!-- Other stuff goes here-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
app:cardBackgroundColor="#81C784"
app:cardCornerRadius="12dp"
app:cardElevation="3dp"
app:contentPadding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/card_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:textSize="15sp"
android:text="TEST"/>
<TextView
android:id="@+id/card_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:textSize="12sp"
android:text="123"/>
<ImageView
android:id="@+id/card_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="3dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
Here is a screenshot of what, roughly, I would like the cardview to look like: