Reputation: 389
I wanna expand my CardView on click with a little animation, and show rest of the TextView.
This is my .xml file
<LinearLayout android:id="@+id/yolo"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
android:id="@+id/card_view1"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="10dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:longClickable="true"
android:orientation="vertical"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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">
<TextView
android:id="@+id/info_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF4444"
android:maxLines="1"
android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
android:textAlignment="center"
android:textSize="24sp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card_view2"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="10dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:longClickable="true"
android:orientation="vertical"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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">
<TextView
android:id="@+id/info_text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
android:textAlignment="center"
android:textColor="#FF4444"
android:textSize="24sp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card_view3"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="10dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:longClickable="true"
android:orientation="vertical"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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">
<TextView
android:id="@+id/info_text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
android:textAlignment="center"
android:textColor="#FF4444"
android:textSize="24sp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="10dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:longClickable="true"
android:orientation="vertical"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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">
<TextView
android:id="@+id/info_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
android:textAlignment="center"
android:textColor="#FF4444"
android:textSize="24sp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
And this is how it looks. I want to make it expand so you can see other information in TextView with a little animation of expanding.
Upvotes: 3
Views: 6307
Reputation: 16587
I do the same thing with following structure :
first of all create AnimationHelper class like following :
public class AnimationHelper {
public static void expand(final View v) {
v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LinearLayout.LayoutParams.WRAP_CONTENT
: (int) (targetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}
Then in your xml put Layout which you want to expand like following (in my case it's view_popup_menu_expand
):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_background"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/card_background">
<TextView
android:id="@+id/btn_expand"
style="@style/IconFont.Large"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="right"
android:text="@string/icon_chevron_up"
android:textColor="@color/color_primary_dark" />
</android.support.v7.widget.CardView>
<include layout="@layout/view_popup_menu_expand" />
</LinearLayout>
Then set a listener to your Card and pass your view_popup_menu_exapand
to AnimationHelper.expand()
You can find complete implementation Here
Upvotes: 2
Reputation: 22945
You can do it this way by using ExpandableHeightListView for view expand and collapse on click
check https://github.com/PaoloRotolo/ExpandableHeightListView
check this code
<com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView
android:id="@+id/expandable_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
</com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView>
Upvotes: 1