How to change CardView's width programmatically in android studio? CardView is written in XML

Viewed 160

This is my Main Activity code. I want to change CardView's width from my activity when the button pressed, but it is not working I tried other attributes as well, for example I can change CardView's color it works fine. But CardView's width and height properties not working.

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private CardView cardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=findViewById(R.id.button);
        cardView=findViewById(R.id.cardView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 cardView.setMinimumWidth(20);//HERE IS THE PROBLEM
            }
        });
    }
}

Below is my XML file which CardView written.

<?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">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_margin="180dp"
        app:cardBackgroundColor="@color/purple_500"
        app:cardCornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="150dp"
        android:padding="8dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>

I tried vise versa in xml file CardView's width initialy 20dp when the button pressed 10dp , but did not work

(This is out of this question but when I use cardView.setRadius(); it works when innitially in xml CardView width greater than the value you want to setup for example it works when CardView's radius initially 20dp you want to be 10dp, but it will not work vice versa)

I want to know is there anything which I am missing? Like to add some implementation...

1 Answers

Card width is setting by using LayoutParams. Add in your button onClick

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) cardView.getLayoutParams();
lp.width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
cardView.setLayoutParams(lp);
Related