Cloud Stack Ninja

I am not able Set custom icon in the center of MaterialCardView. Here is my xml layout

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cardPaymentContainer"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:layout_margin="@dimen/default_margin"
    android:layout_marginStart="@dimen/button_margin"
    android:layout_marginEnd="@dimen/button_margin"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolBarContainer">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardPaymentCardView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        app:cardCornerRadius="@dimen/card_view_cornder_radius"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>

handle click in activity:

dataBinding =
            DataBindingUtil.setContentView<PaymentActivityBinding>(this, R.layout.payment_activity)
        dataBinding.setHandler(this)
        dataBinding.cardPaymentCardView.setOnClickListener({
            Debug.d(TAG, "cardPaymentCardView: onClick")
            dataBinding.cardPaymentCardView.isChecked = !dataBinding.cardPaymentCardView.isChecked
        })

Here result:

Not clicked:

enter image description here

And clicked:

enter image description here

But I need to put my custom icon to the center of CardView. Smt like this:

enter image description here

I converted SVG to xml (by Android Studio)

I try this, but icon not show and not show in the CENTER of cardView:

 <com.google.android.material.card.MaterialCardView
                android:id="@+id/cardPaymentCardView"
                style="@style/cardViewStyle"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true"
                app:cardCornerRadius="@dimen/card_view_cornder_radius"
                app:checkedIcon="@drawable/ic_credit_card_outline_select"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            </com.google.android.material.card.MaterialCardView>


Read more here: https://stackoverflow.com/questions/58639798/set-custom-icon-in-the-center-of-materialcardview

Content Attribution

This content was originally published by a_subscriber at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: