Android : Cardview Background is turning to black on 4.1.2

Viewed 12127

I am using google cardView support library for my card functionality. It works well for kitkat and version up but however the background of card is set to black and padding/margins are not applied on device 4.1.2.

<android.support.v7.widget.CardView
        android:id="@+id/all_goals_card_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:padding="10dp"
        app:cardCornerRadius="4dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardBackgroundColor="@android:color/white"
        >
 </android.support.v7.widget.CardView>
6 Answers

Okay, I just stumbled across the same issue and I found some devices to have some "special" very-light light-theming defaults cough samsung cough I will answer this slightly old queston.

The thing here is that you are most likely using the wrong context to inflate you layout. I think you are using the application-context to do so. Application-Context does not apply the theme you defined.

This (inflating with the application-context) is legal, but inflation will be done with the default theme for the system on which you are running, not what’s defined in your application.*

For example if you do:

LayoutInflater.from(context).inflate(R.layout.menu_rental_list_item, parent, false);

The context here should be an Activity- or Fragment Context - NOT the application-context.

Please double check that.

*) Ah, you want to read more about contexts? Please continue reading here.

In my case, I put android:theme="@style/Theme.AppCompat.Light.NoActionBar" in the manifest file for related activity

Related