CardView grey on API 19 and 21
Asked Answered
X

3

5

I have no idea why my cards appear grey on my API 21 and 19 emulator. I have built RecyclerViews with CardViews before and they were white. I don't change the background color anywhere. It appears normal on my API 26 emulator.

enter image description here

This my layout for the cards:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Name"
        android:textColor="@android:color/black"
        android:textSize="20sp" />


    <ImageView
        android:id="@+id/image_view_upload"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/text_view_name" />

</RelativeLayout>

This is my adapter class:

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

private Context mContext;
private List<Upload> mUploads;


public ImageAdapter(Context context, List<Upload> uploads) {
    mContext = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Upload upload = mUploads.get(position);
    holder.textViewName.setText(upload.getName());
    Picasso.with(mContext)
            .load(upload.getImageUrl())
            .fit()
            .centerCrop()
            .into(holder.imageView);

}

@Override
public int getItemCount() {
    return mUploads.size();
}

class ImageViewHolder extends RecyclerView.ViewHolder {
    TextView textViewName;
    ImageView imageView;

    ImageViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);
    }
}

}

I load these images from the Firebase storage. As you can see, I didn't change the background color anywhere.

Any idea?

Xanthin answered 1/1, 2018 at 12:55 Comment(13)
have you checked it on real device too ? sometimes emulator doesn't show the correct results.Darrendarrey
take a look at this link : #43781509Darrendarrey
I did take a look at this, but he changes the background color - I don'tXanthin
if you are talking about the background of textview which is in cardview then I believe you have to take a look at your styles.xml file.Darrendarrey
No, I mean the cards themselves. They are white on API level 26, but not lowerXanthin
if this issue is below lollipop version then this is due to the fact that carview was announced in lollipop and certain features are not supported at less levels you have to give background to support pre-lollipop devices.Darrendarrey
It works for my other projects so that can't be itXanthin
are you testing your application on samsung emulators ?Darrendarrey
No, on Nexus 5. The same emulators on which it works for other projects.. very weirdXanthin
I think you are passing the wrong type of context here. Is this the application context or of activity or fragments? try changing that and if it still doesn't work then it is wired but I believe this is due to difference API levels.Darrendarrey
Hey, you were right. It was because I passed getApplicationContext to the adapter. I found it like this in a tutorial. If i pass the Activity context, it's white. Now I only have to figure out if I should change the context or the cards background.Xanthin
Great now I should add that as an answer.Darrendarrey
Understandable. Go ahead, I will accept itXanthin
D
8

I believe you are passing the wrong context to your adapter to inflate your layout. Try passing activity or fragment context instead of passing applicationConetxt. ApplicationContext does not apply the theme you defined.

OR

if you don't wanna do that. you need to change background color of your carview like this:

<android.support.v7.widget.CardView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:card="http://schemas.android.com/apk/res-auto"
 card:cardBackgroundColor="@color/colorPrimary"
 android:layout_margin="8dp">
</android.support.v7.widget.CardView>
Darrendarrey answered 1/1, 2018 at 13:40 Comment(2)
Thank you, it was because I passed getApplicationContext to the adapterXanthin
Glad I could be of help. Happy Coding :)Darrendarrey
N
1

I am not sure why it is showing grey on api 21.

But if you want white background then you can set the background color like this.

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#FFFFFF">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Name"
        android:textColor="@android:color/black"
        android:textSize="20sp" />


    <ImageView
        android:id="@+id/image_view_upload"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/text_view_name" />

</RelativeLayout>

Also please note that cardview is supported from api 21 only.

Nutting answered 1/1, 2018 at 13:24 Comment(1)
I would rather understand why it is displayed grey but not for my other projects which use the same cardsXanthin
D
0

As You can See in This Link

android support(cardView, RecyclerView)library in older versions with target kitkat

Your CardView is Only compatible till API level 26,which is why i asked you to post your Gradle dependencies

You can Try changing the minimum API level and see for any changes

Denna answered 1/1, 2018 at 13:10 Comment(3)
this is a comment not an answer.Darrendarrey
implementation 'com.android.support:cardview-v7:26.1.0' implementation 'com.android.support:recyclerview-v7:26.1.0'Xanthin
I have updated the answer as I cannot comment till I reach 50 reputationDenna

© 2022 - 2024 — McMap. All rights reserved.