user1513171
user1513171

Reputation: 1974

Android: CardView Button can't set background color

I'm using this tutorial http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168

to try and create a raised button using a CardView and a TextView. It kind of works, but there's an issue.

enter image description here

The goal is for the whole button to have the teal color, which is my colorPrimary. However you can see only my TextView has that color and that's because I manually set it.

Here's my layout:

 <android.support.v7.widget.CardView
                    android:id="@+id/card_view"
                    android:layout_width="wrap_content"
                    android:layout_height="36dp"
                    android:layout_marginTop="16dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    card_view:cardBackgroundColor="@color/colorPrimary"
                    android:background="@color/colorPrimary"
                    card_view:cardCornerRadius="2dp"
                    card_view:cardElevation="1dp">

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

                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:gravity="center"
                            android:singleLine="true"
                            android:text="@string/login"
                            android:background="@color/colorPrimary"
                            android:textColor="@color/colorTextIcons"/>
                    </RelativeLayout>
                </android.support.v7.widget.CardView>

I'm not sure why these lines aren't working:

card_view:cardBackgroundColor="@color/colorPrimary"
                    android:background="@color/colorPrimary"

Upvotes: 0

Views: 1313

Answers (2)

Sharjeel
Sharjeel

Reputation: 15798

You have to change the color of your button using colorButtonNormal attribute instead of changing background attribute of the button to make it work. You can do this using style like this:

<Button  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:theme="@style/YourColorButton"/>

In your styles.xml

<style name="YourColorButton" parent="Theme.AppCompat.Light">  
    <item name="colorButtonNormal">@color/yourButtonColorHere</item>
</style>  

Make sure you are using latest version of appcompat support library.

Upvotes: 0

Walt
Walt

Reputation: 76

Try to put the background atribute to the RelativeLayout tag rather than to CardView tag:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorPrimary" >

So your code after the corrections should be:

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_marginTop="16dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="1dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/colorPrimary">

       <TextView
           android:id="@+id/textView"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:gravity="center"
           android:singleLine="true"
           android:text="@string/login"
           android:textColor="@color/colorTextIcons"/>
    </RelativeLayout>

You may also want to move the 'foreground' atribute accordingly

Upvotes: 1

Related Questions