黃義鈞
黃義鈞

Reputation: 13

How to set the cardview height programmatically?

I am new in android development and I have searched this question, but not getting any solution.I want to ask how to make the cardview with recyclerview divide into three pieces in one screen?

example

this is my XML

<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:layout_margin="1dp"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:id="@+id/card"
    android:layout_height="250dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="1dp"
    app:cardMaxElevation="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.github.siyamed.shapeimageview.RoundedImageView
            android:id="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher_background" />

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

and here is the code I try in Activity

View rootLayout = findViewById(R.id.root_layout);
    double height =rootLayout.getLayoutParams().height/3.0;
    CardView card= (CardView)findViewById(R.id.card);
    CardView.LayoutParams layoutParams= (CardView.LayoutParams) card.getLayoutParams();
    layoutParams.height=(int)height;
    layoutParams.width=MATCH_PARENT;

Thanks.

Upvotes: 1

Views: 8203

Answers (2)

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

try getting height of window and divide it by 3,

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card= (CardView)findViewById(R.id.card);
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) card.getLayoutParams();
layoutParams.height= height;
layoutParams.width=MATCH_PARENT;
card.setLayoutParams(layoutParams);

Upvotes: 2

Abdulrahman Abdulkarim
Abdulrahman Abdulkarim

Reputation: 132

Just as @V-rund suggested Get the Screen/Window height and divide it by 3 using,

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card = (CardView)findViewById(R.id.card);

but instead of using CardView.LayoutParams use the LayoutParams for the parent layout of your CardView which in your case will be a LinearLayout.

LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) 
card.getLayoutParams();
layoutParams.height = height;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
card.setLayoutParams(layoutParams);

I hope this helps solve your problem.

Upvotes: 0

Related Questions