Skip to content

Instantly share code, notes, and snippets.

@smartherd
Created December 4, 2020 12:06
Show Gist options
  • Save smartherd/07b4c3846578bf76d6da047441e05d17 to your computer and use it in GitHub Desktop.
Save smartherd/07b4c3846578bf76d6da047441e05d17 to your computer and use it in GitHub Desktop.
Checkable MaterialCardView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#e6e6e6"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:text="Long press to check"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="#666666" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:visibility="visible">
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardTajMahal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="6dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/txvTajMahal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Taj Mahal"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="#666666" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:text="It's an ivory-white marble mausoluem on the south bank of the Yamuna river."
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#999999" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardStatueOfUnity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="6dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/txvStatueOfUnity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Statue of Unity"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="#666666" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:text="It's a colossal statue of Indian independence activist Vallabhbhai Patel."
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#999999" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardLotusTemple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="6dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/txvLotusTemple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Lotus Temple"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="#666666" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:text="Notable for its flowerlike shape, located in Delhi, India, is a Baha'i House of worship that was dedicated in December 1986."
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="#999999" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<Button
android:id="@+id/btnWhatsSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show what's selected"
android:textAllCaps="false"
android:layout_margin="10dp"
android:layout_gravity="center"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.sriyank.mdccomponents
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cardTajMahal.setOnLongClickListener {
cardTajMahal.isChecked = !cardTajMahal.isChecked
true
}
cardStatueOfUnity.setOnLongClickListener {
cardStatueOfUnity.isChecked = !cardStatueOfUnity.isChecked
true
}
cardLotusTemple.setOnLongClickListener {
cardLotusTemple.isChecked = !cardLotusTemple.isChecked
true
}
btnWhatsSelected.setOnClickListener {
var msg = ""
if (cardTajMahal.isChecked)
msg += "'Taj Mahal' "
if (cardStatueOfUnity.isChecked)
msg += "'Statue of Unity' "
if (cardLotusTemple.isChecked)
msg += "'Lotus Temple' "
Snackbar.make(it, "$msg Selected", Snackbar.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment