Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Cardview Background is turning to black on 4.1.2

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>
like image 599
Sutirth Avatar asked Mar 10 '16 17:03

Sutirth


3 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.

like image 109
Langusten Gustel Avatar answered Nov 06 '22 02:11

Langusten Gustel


don't use "@android:color/white"

card_view:cardBackgroundColor="#fff"
like image 7
Reza Nazeri Avatar answered Nov 06 '22 03:11

Reza Nazeri


This will solve the issue:

      <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#fff"
        card_view:cardBackgroundColor="#fff"
        android:layout_margin="2dp">

Notice these lines:

xmlns:card_view="http://schemas.android.com/apk/res-auto"

and 

card_view:cardBackgroundColor="#fff"
like image 3
BlackPearl Avatar answered Nov 06 '22 02:11

BlackPearl