I'm creating a layout very similar to instagram
I want to exibit users media in a recycleview with 3 columns
this is my card:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="1dp">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/mediaThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/link_blue"
android:scaleType="centerCrop" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/mediaThumbnail"
android:layout_alignBottom="@id/mediaThumbnail"
android:layout_margin="2dp"
android:drawableLeft="@drawable/ic_like"
android:text="10001"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="12sp" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/mediaThumbnail"
android:layout_alignBottom="@id/mediaThumbnail"
android:layout_margin="2dp"
android:drawableLeft="@drawable/ic_comments"
android:text="10001"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="17dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="1dp"
android:background="#33000000" />
and this is how i init the recyclerview:
RecyclerView recyclerView = findViewById(R.id.gridView);
recyclerView.setAdapter(new MediaAdapter(new ArrayList<>()));
recyclerView.setLayoutManager(new GridLayoutManager(this,3));
recyclerView.setItemAnimator(new DefaultItemAnimator());
my point is: different devices will have different screen width so i can't force any value in the card android:layout_width="match_parent" i will let the layoutmanager do that job
but then i dont know what value it ended being so any value i try to guess for android:layout_height="100dp" will not be square.
how to adjust the height to get the runtime value of width?

The best way to achieve this result - wrap your layout with
ConstraintLayout. Please look at the code below.