17

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>
Sutirth
  • 1,217
  • 4
  • 15
  • 26

6 Answers6

44

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.

Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
  • @Langusten Gustel thanks a lot. Another Samsung spesific weird behaviour, "sigh..." – Lev Feb 23 '17 at 07:50
  • Hello my dear, I think my question is the same. Can you take a look at it? – john Jun 26 '19 at 08:43
  • https://stackoverflow.com/questions/56750943/android-toolbar-color-is-turning-to-blue-when-come-from-fragment – john Jun 26 '19 at 08:43
7

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

card_view:cardBackgroundColor="#fff"
Reza Nazeri
  • 316
  • 3
  • 12
3

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"
BlackPearl
  • 2,532
  • 3
  • 33
  • 49
1

I had the same issue on android 4.1.2 device. I was using an ImageView with shape drawable inside CardView which was the actual culprit.

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

woliveirajr
  • 9,433
  • 1
  • 39
  • 49
1

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

Binz_
  • 21
  • 1
0

Don't use

android:Theme.Dialog

or

android.R.style.Theme_Dialog

, if your CardView is a part of the DialogFragment or Dialog layout.

Michael Katkov
  • 2,256
  • 1
  • 20
  • 17