19

The issue I am having is basically what is found in this answer but I will still explain below. I am testing on a device with android 5.0.1 and another with 4.2.2. On my device with 5.0.1 I get no errors and everything behaves as expected. On my 4.2.2 device I get the following error:

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
        at android.support.v7.widget.CardViewEclairMr1.getShadowBackground(CardViewEclairMr1.java:159)
        at android.support.v7.widget.CardViewEclairMr1.getMinWidth(CardViewEclairMr1.java:150)
        at android.support.v7.widget.CardView.onMeasure(CardView.java:181)
        at android.view.View.measure(View.java:15696)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:15696)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15696)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15696)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261)
        at android.view.View.measure(View.java:15696)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2225)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1290)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1527)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1190)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4860)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766)
        at android.view.Choreographer.doCallbacks(Choreographer.java:575)
        at android.view.Choreographer.doFrame(Choreographer.java:542)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:5751)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
        at dalvik.system.NativeStart.main(Native Method)

with the error occurring at this line: card1.setBackgroundColor(getResources().getColor(R.color.White));

Now as suggested by the linked answer I have tried using setCardBackgroundColor however this causes the error to be produced on both my devices. Any suggestions?

The CardView I am attempting to use is a android.support.v7.widget.CardView

This is the layout producing the error:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#bbbbb5"
android:orientation="vertical"
android:weightSum="100">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="400dp"
    android:layout_gravity="center_vertical|top"
    android:layout_marginBottom="50dp"
    android:layout_marginTop="-65dp"
    android:layout_toRightOf="@+id/prem_BACK"
    android:layout_weight="0"
    android:clickable="true"
    android:paddingBottom="0dp"
    android:src="@drawable/plane1" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView"
    android:layout_alignEnd="@+id/imageView"
    android:layout_alignRight="@+id/imageView"
    android:layout_marginBottom="40dp"
    android:layout_marginRight="5dp"
    android:src="@drawable/ic_navigate_next_white_48dp" />

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view1"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="280dp"
    android:clickable="true"
    android:onClick="card1ClickMethod"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/choice_text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:onClick="card1ClickMethod"
        android:text="Loret ipsum sample"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view2"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="340dp"
    android:clickable="true"
    android:onClick="card2ClickMethod"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/choice_text2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:gravity="center"
        android:onClick="card2ClickMethod"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view3"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="400dp"
    android:clickable="true"
    android:onClick="card3ClickMethod"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/choice_text3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:gravity="center"
        android:onClick="card3ClickMethod"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view4"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="460dp"
    android:clickable="true"
    android:onClick="card4ClickMethod"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/choice_text4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:gravity="center"
        android:onClick="card4ClickMethod"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>


<TextView
    android:id="@+id/resulttext3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/progressBar1"
    android:layout_alignParentStart="true"
    android:layout_gravity="center_vertical|left"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:textSize="15dp" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/fab"
    android:layout_gravity="right|center_vertical"
    android:layout_marginTop="20dp" />

<ImageView
    android:id="@+id/prem_BACK"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:src="@drawable/background4"
    android:visibility="gone" />

After reading through the issue described here I noticed that it has since been closed. As far as I am aware I am using the most updated version of the platform tools and sdk tools and I still have this issue.

After some further testing, I have discovered that the emulator I am using does not have GooglePlayServices. I do not currently have access to a physical device with less than Android 6.0 to test on. Is it a possibility that not having GooglePlayServices could cause this issue?

Community
  • 1
  • 1
ez4nick
  • 9,756
  • 12
  • 37
  • 69

7 Answers7

2

This is working for me for cardview inside recycleview

 ((CardView)viewholder.itemView).setCardBackgroundColor(Color.WHITE);
hashPJ
  • 76
  • 7
2

You could also keep both versions of android happy by giving them what they want rather than trying to force and cast something:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
       // below lillipop
       view.setCardBackgroundColor(Color.WHITE);
       // working for lower questions as per another answer posted here
   } else {
        // lollipop and above
        card1.setBackgroundColor(getResources().getColor(R.color.White));
        // known to be working for lillipop as per your question
}

Basically call different and working methods for each of the android versions.

To correctly identify android versions above and below lollipop use LILLIPOP_MR1 (Soruce)

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
1

You can use:

card1.setBackgroundColor(Color.rgb(255, 255, 255));

instead of:

card1.setBackgroundColor(getResources().getColor(R.color.White));
1

The easiest way to do what you want is using color filter, I guess. I tried this on 4.2.2 and it works fine.

cardView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
dtx12
  • 4,438
  • 1
  • 16
  • 12
1

By using setBackgroundColor(int color) you are actually calling a method on View, switching the background. The CardView expects a RoundRectDrawableWithShadow, which you did just overwrite. Hence the exception.

CardView view = (CardView) findViewById(R.id.card);
view.setCardBackgroundColor(Color.RED);

Be sure to call cardView.setCardBackgroundColor(int) and it works. It works on 4.2.2, and by looking through the cardView source code I'm confident about other apis as well. If this also leads to a crash, it must be a different reason.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • Have you tested this on a physical device or on an emulator? I have only been testing on an emulator and *not* a physical device as I do not have access to one. On my emulator this does not solve the issue. – ez4nick Jan 05 '16 at 16:57
  • @ez4nick tested on a Samsung Galaxy S2 with Api 19...and I find it rather peculiar that this would not work in an emulator, especially since there is no `ColorDrawable` that could cause the crash using that method. Are you sure you don't set the color on multiple posititions? – David Medenjak Jan 05 '16 at 17:01
1

Probably this resolved by using PublicRoundRectDrawableWithShadow.
Create PublicRoundRectDrawableWithShadow.

package android.support.v7.widget;

import android.content.res.Resources;

public class PublicRoundRectDrawableWithShadow extends RoundRectDrawableWithShadow {
    public PublicRoundRectDrawableWithShadow(Resources resources, CardView cardView, int backgroundColor) {
        super(resources, backgroundColor, cardView.getRadius(), cardView.getCardElevation(), cardView.getMaxCardElevation());
    }
}

And setBackground() like this.

    final CardView cardView = (CardView) findViewById(R.id.card_view);
    if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
        cardView.setBackgroundColor(Color.RED);
    } else {
        cardView.setBackgroundDrawable(new PublicRoundRectDrawableWithShadow(getResources(), cardView, Color.RED));
    }
takahirom
  • 1,934
  • 2
  • 12
  • 21
0

Please try below specific line to set background color of card view object:

cardView.setCardBackgroundColor(Color.WHITE);
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Rahul Vats
  • 277
  • 3
  • 17