Sutirth
Sutirth

Reputation: 1267

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>

Upvotes: 17

Views: 12880

Answers (6)

Binz_
Binz_

Reputation: 21

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

Upvotes: 1

Vinayak Bhat
Vinayak Bhat

Reputation: 33

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.

Upvotes: 1

Michael Katkov
Michael Katkov

Reputation: 2356

Don't use

android:Theme.Dialog

or

android.R.style.Theme_Dialog

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

Upvotes: 0

BlackPearl
BlackPearl

Reputation: 2785

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"

Upvotes: 3

Langusten Gustel
Langusten Gustel

Reputation: 11002

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.

Upvotes: 44

Reza Nazeri
Reza Nazeri

Reputation: 315

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

card_view:cardBackgroundColor="#fff"

Upvotes: 7

Related Questions