ironmantis7x
ironmantis7x

Reputation: 827

changing the background of a card (Android cardView)

I am having a tough time changing the background color of a card in Android (cardView).

I did this in the XML:

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_margin="5dp"
        android:layout_gravity="bottom"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp"
        card_view:cardBackgroundColor="#2600e622">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

But it did not work. I keeps defaulting to the white back ground color.

I am not sure where to go from here. Any ideas?

Also - I want to position the card in the center of the screen and the card ends up being in the center but at the TOP of the screen. Can I get some insight on how to position the card in the center of the screen as well?

Thanks.

Upvotes: 1

Views: 2523

Answers (4)

maresan
maresan

Reputation: 31

if you have in your colors.xml this color for example:

<color name="ambar400">#FFCA28</color>

in your class use:

card_name.setCardBackgroundColor(getResources().getColor(R.color.ambar400));

Upvotes: 1

adc
adc

Reputation: 76

Set the color to your relative layout background if he is the root layout of your cardview:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:colorBackground="#2600e622">

Upvotes: 0

user6021276
user6021276

Reputation:

I used this code to set programmatically:

card.setCardBackgroundColor(Color.WHITE);

Or in XML you can use this code:

card_view:cardBackgroundColor="@android:color/white"

Upvotes: 1

Borislav Kamenov
Borislav Kamenov

Reputation: 711

This should works.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="5dp"
    android:layout_gravity="center"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding="10dp"
    card_view:cardBackgroundColor="#00490b">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        // add more views here

    </RelativeLayout>
</android.support.v7.widget.CardView>

Upvotes: 4

Related Questions