I have a button defined as shown below. When I want to disable it I use my_btn.setEnabled(false)
, but I would also like to grey it out. How can I do that?
Thanks
<Button android:id="@+id/buy_btn" style="@style/srp_button" />
style/srp_button
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">14sp</item>
<item name="android:typeface">serif</item>
<item name="android:paddingLeft">30dp</item>
<item name="android:paddingRight">30dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
drawable/btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/pink" />
<corners android:radius="6dp" />
</shape>
u can make a grey picture and put in into your drawable and add this into your code.
easy,simple,but because of using picture it gets a little more space.
You should create a XML file for the disabled button (drawable/btn_disable.xml)
And create a selector for the button (drawable/btn_selector.xml)
Add the selector to your button
The most easy solution is to set color filter to the background image of a button as I saw here
You can do as follow:
Hope I helped someone...
Set Clickable as false and change the backgroung color as:
You have to provide 3 or 4 states in your
btn_defaut.xml
as a selector.You will provide effect and background for the states accordingly.
Here is a detailed discussion: Standard Android Button with a different color
All given answers work fine, but I remember learning that using setAlpha can be a bad idea performance wise (more info here). So creating a StateListDrawable is a better idea to manage disabled state of buttons. Here's how:
Create a XML btn_blue.xml in res/drawable folder:
Create a button style in res/values/styles.xml
Then apply this style to your button:
Now when you call
btnBlue.setEnabled(true)
ORbtnBlue.setEnabled(false)
the state colors will automatically switch.