How to make cards in RecyclerView be square in differents DPI

90 Views Asked by At

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.

this is the final result: enter image description here

how to adjust the height to get the runtime value of width?

2

There are 2 best solutions below

0
Alex On

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

<androidx.constraintlayout.widget.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">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <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" />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
0
Rene Ferrari On

You could do it like this:

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = view.getWidth();
        view.setLayoutParams(params);
        view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

There is (except wrapping your View with a ConstraintLayout) no real way to achieve this via xml. The code that I have written does the following: when onGlobalLayout is invoked your Views size has been set up. Now when accessing its LayoutParams it will have the correct values. Then I just say the height should be equal to the width. The last line removes the listener since having it attached would come with a performance cost.