Android: Cardview background turns to black on 4.1.2

I use google cardView support library for my functionality. It works great for kitkat and up version, but, however, the map background is set to black, and padding / margin is 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> 
+7
android android-layout user-interface
source share
5 answers

Well, I just stumbled upon the same problem, and I found that some devices have some β€œspecial” very light scenes by default, coughing the female. I will answer this slightly old question.

The fact is that you are most likely using the wrong context to inflate your layout. I think you are using application-context for this. The Context application does not apply the theme that you defined.

This (inflating using a context application) is legal, but inflation will be performed with the default theme for the system you are running on, and not what is defined in your application. *

For example, if you do:

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

context here should be an Activity- or Fragment context - NOT a context application.

Please double check this.

*) Ah, you want to know more about contexts? Read on here .

+26
source share

do not use "@android: color / white"

 card_view:cardBackgroundColor="#fff" 
+7
source share

This will solve the problem:

  <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"> 

Pay attention to the following lines:

 xmlns:card_view="http://schemas.android.com/apk/res-auto" and card_view:cardBackgroundColor="#fff" 
+1
source share

I had the same issue on an Android 4.1.2 device. I used ImageView with the ability to draw inside CardView, which was the actual culprit.

Please check the answer in this link which helped me fix this problem.

+1
source share

Do not use

Android: Theme.Dialog

or

android.R.style.Theme_Dialog

if your CardView is part of a DialogFragment or Dialog scheme.

0
source share

All Articles