Child views inside MaterialCardView shrinking and vanishing

Viewed 2016

android.support.design.card.MaterialCardView has been used in native axml layout and have child view as below , my goal is keep switching border i.e stroke when some event occurs ,ex: button click and revert it , but the every change in strokeWidth the child views shrinka bit gradually and finally width and height become 0 making inner view completely vanish.

MaterialCardView layout code:


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.card.MaterialCardView  
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_margin="10dp"
              app:cardCornerRadius="10dp"
              app:strokeColor="#123456"
              app:strokeWidth="3dp"
              android:id="@+id/material_card"
              android:padding="5dp"
              android:layout_gravity="center_horizontal">
              <LinearLayout android:layout_width="fill_parent"
                              android:layout_height="240dp"
                              android:orientation="vertical"
                            android:padding="8dp"
                            android:id="@+id/linearLayoutCard">
                <Button android:id="@+id/button"
                        android:text="Click me"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"/>
                <TextView android:id="@+id/textview"
                          android:text ="Border in"
                          android:layout_below="@id/button"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"/>
              </LinearLayout>
</android.support.design.card.MaterialCardView>

PageRenderer code


  public void OnClick(Android.Views.View v)
        {
            // throw new NotImplementedException();
            if (_yes)
            {
                material_card.StrokeColor = Android.Graphics.Color.ParseColor("#FFFFFF");
                material_card.StrokeWidth = 0;
                //material_card.LayoutParameters.LayoutAnimationParameters
                linearLayoutCard?.SetMinimumHeight(material_card.Height+20);
                linearLayoutCard?.SetMinimumWidth(material_card.Width+20);
                linearLayoutCard.SetPadding(0, 0, 0, 0);
                textview.Text = "Border out";
                _yes = false;
               // return true;
            }
            else
            {
                material_card.StrokeColor = Android.Graphics.Color.ParseColor("#123456");
                material_card.StrokeWidth = 20;
                linearLayoutCard?.SetMinimumHeight(material_card.Height);
                linearLayoutCard?.SetMinimumWidth(material_card.Width);
                linearLayoutCard.SetPadding(0, 0, 0, 0);
                textview.Text = "Border in";
                _yes = true;
                //return true;
            }
        }

After few clicks:

Issue_image

I tried to use other layout RelativeLayout which dint work and also tried to increase the LinearLayout height and width and set padding to 0 to min as in the above , that also not working,while debugging I noticed Padding gets triggered for MaterialCardView and that probably causing it shrink it down. Also tried setting padding of cardview to 0 but still no luck.

material_card.SetPadding(0, 0, 0, 0);
linearLayoutCard.LayoutParameters.Width = material_card.Width;
linearLayoutCard.LayoutParameters.Height =material_card.Height;

Why inner views are not set with higher width since its mapped to match_parent /fill_parent ?

Full Code at Github : github.com/pmahend1/TestMaterialCardView

1 Answers

There seems to be an issue with this class that is causing its content padding into keeping increasing after changing the StrokeWidth values.

Try resetting the ContentPadding every time you set the StrokeWith to 0 with this line of code:

material_card.SetContentPadding(0, 0, 0, 0);

Hope this helps.-

Related