Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make square CardView (Android)

How to make square CardView.

When I set the layout_weight on CardView it is not set like square on CardView.

Please give me a solution : (without set the fixed height and width)

See picture below: enter image description here

Code:

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:weightSum="3">

                    <android.support.v7.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_margin="4dp"
                        android:layout_weight="2"
                        app:cardBackgroundColor="@color/silver"
                        tools:ignore="NestedWeights">


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

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:weightSum="1">

                        <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="4dp"
                            android:layout_weight="0.5"
                            app:cardBackgroundColor="@color/silver">


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

                        <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="4dp"
                            android:layout_weight="0.5"
                            app:cardBackgroundColor="@color/silver">


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

                    </LinearLayout>

                </LinearLayout>
like image 925
Prince Dholakiya Avatar asked Jul 20 '18 07:07

Prince Dholakiya


People also ask

How do I create a custom card view?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

What is card layout Android?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.

How do I add padding to CardView?

If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true) , or the XML variant cardUseCompatPadding="true" .


1 Answers

You can use a ConstraintLayout instead of multiple LinearLayout with weight.

Add

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

in your build.gradle. Then you can try something like that (with dimensionRatio set to 1):

<?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="match_parent"
android:padding="4dp">

<android.support.v7.widget.CardView
    android:id="@+id/card1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/card2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="4dp"
    app:layout_constraintBottom_toTopOf="@id/card3"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/card1"
    app:layout_constraintTop_toTopOf="@id/card1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
    android:id="@+id/card3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="4dp"
    app:layout_constraintBottom_toBottomOf="@id/card1"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintEnd_toEndOf="@id/card2"
    app:layout_constraintStart_toStartOf="@id/card2"
    app:layout_constraintTop_toBottomOf="@id/card2">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

The result:

enter image description here

like image 124
Pycpik Avatar answered Nov 15 '22 03:11

Pycpik