ExpandableCardView
An Android library that lets you create in a simple, fast and hassle-free way a CardView in which you can insert your custom layout and just expand and collapse without even writing a single Java/Kotlin line of code.
This component follows the Material Design Guidelines.
Please note that this library is still in early development stage!
Setup
First of all, include the dependency in your app build.gradle:
compile 'com.alespero:expandable-cardview:0.6'
Or get the aar in the Releases section.
Declaring the view
After you have the Library correctly setup, just declare the ExpandableCardView in your xml:
<com.alespero.expandablecardview.ExpandableCardView
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:title="Passengers"
app:icon="@drawable/ic_person"
app:inner_view="@layout/mycustomview"
app:expandOnClick="true"/>
You can specify a custom title and icon on the header on the card by setting the attribute app:title
and app:icon
respectively.
After you created the base xml, just create your custom layout and place it inside your layout
folder.
Now just pass your newly created layout resource to the app:inner_view
attribute. By setting the attribute app:expandOnClick="true"
the card will have a default behaviour (expand/collapse on click)
Done! Now your ExpandableCardView is ready to roll.
Usage
If you want some basic Expandable Card without any custom behaviour, setting the view in the XML is enough. Otherwise just declare your ExpandableCardView in your Activity/Fragment and you're ready to use its methods.
Java
ExpandableCardView card = findViewById(R.id.profile);
//Do stuff here
Kotlin
val card : ExpandableCardView = findViewById(R.id.profile)
//Do stuff here
You can use expand()
and collapse()
to respectively expand and collapse the card, and use isExpanded()
to check if the card is expanded or not.
You can change the title and icon of the card dynamically by using setTitle()
and setIcon()
methods.
You can also set an OnExpandedListener to the card:
Java
card.setOnExpandedListener(new OnExpandedListener() {
@Override
public void onExpandChanged(View v, boolean isExpanded) {
Toast.makeText(applicationContext, isExpanded ? "Expanded!" : "Collapsed!", Toast.LENGTH_SHORT).show();
}
});
Kotlin
card.setOnExpandedListener { view, isExpanded ->
Toast.makeText(applicationContext, if(isExpanded) "Expanded!" else "Collapsed!", Toast.LENGTH_SHORT).show()
}