Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a custom color for button background while using selectableItemBackground attribute

I am trying to use

android:background="?android:attr/selectableItemBackground"

to get my button to do appropriate effects for each android version like rippling, etc.

But this produces a button with a grey color when I need a different color.

How can I override this default color ?

like image 873
Mohamed Abdalkader Avatar asked Dec 11 '14 03:12

Mohamed Abdalkader


2 Answers

If you read the source code of Button.java then you will see that it is a subclass of TextView.java. I have made a simple workaround for the problem in question.

<LinearLayout android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:clickable="true"               android:background="#1f5050">         <TextView android:layout_width="some_dp"                  android:layout_height="some_dp"                  android:id="@+id/button"                  android:background="?android:selectableItemBackground" />  </LinearLayout> 

In code:

button.setOnClickLisetener(new Onclicklistener() {     // do your stuff here } 

It would be much better if someone can extend the TextView class and make a custom Button with the feature in question.

Note: my minsdk is 14. also, the lollipop ripple effect works just fine

like image 178
denvercoder9 Avatar answered Sep 17 '22 15:09

denvercoder9


Just move your desired color in outer/parent level, e.g. "@color/Red" is button color:

    <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:background="@color/Red"         android:layout_weight="1">          <Button             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:background="?attr/selectableItemBackground"             android:gravity="center"             android:text="Hello"             android:textColor="@color/White"/>     </LinearLayout> 
like image 24
林果皞 Avatar answered Sep 16 '22 15:09

林果皞