java - Change Button custom shape states color programatically -


there lot of answers on site regarding changing button colors, none have managed use in case.

i want able dynamically change button's color, button still needs have visual feedback on press, , needs rounded corners.

the rounded corners part decided upon recently, used :

    statelistdrawable states = new statelistdrawable();     states.addstate(new int[]{android.r.attr.state_pressed},          new colordrawable(hsvdarkencolor(theme.get_buttonsbgcolor())));          states.addstate(new int[]{android.r.attr.state_focused},         new colordrawable(hsvdarkencolor(theme.get_buttonsbgcolor())));          states.addstate(new int[]{},        new colordrawable(color.parsecolor(theme.get_buttonsbgcolor())));      ((button) button).setbackgrounddrawable(states);  //this focused/pressed state of button    private static int hsvdarkencolor(string originalcolor)         {             float[] hsv = new float[3];             int color = color.parsecolor(originalcolor);             color.colortohsv(color, hsv);             hsv[2] *= 0.8f; // value component             return color.hsvtocolor(hsv);         } 

however doesn't preserve rounded corners shape of buttons, makes them squares.

my default buttons' backgrounds list of states each it's own drawable, pressed , unpressed, etc.

in styles.xml:

    <style name="xx.light.scanbutton" parent="android:style/widget.button">     <item name="android:focusable">true</item>     <item name="android:background">@drawable/xx_scan_button</item>     <item name="android:textstyle">normal</item>     <item name="android:textcolor">@color/text_light</item>     </style> 

in drawable/xx_scan_button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- non focused states --> <item     android:state_focused="false"     android:state_selected="false"     android:state_pressed="false"     android:drawable="@drawable/xx_scan_button_normal"     /> <item     android:state_focused="false"     android:state_selected="true"     android:state_pressed="false"     android:drawable="@drawable/xx_scan_button_pressed"     />  <!-- focused states --> <item     android:state_focused="true"     android:state_selected="false"     android:state_pressed="false"     android:drawable="@drawable/xx_scan_button_pressed"     /> <item     android:state_focused="true"     android:state_selected="true"     android:state_pressed="false"     android:drawable="@drawable/xx_scan_button_pressed"     />  <!-- pressed --> <item     android:state_pressed="true"     android:drawable="@drawable/xx_scan_button_pressed"     />  <!-- disabled --> <item     android:state_enabled="false"     android:drawable="@drawable/xx_scan_button_normal"     /> </selector> 

and in drawable/xx_scan_button_normal.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"             android:shape="rectangle" > <corners android:bottomrightradius="5dp"          android:bottomleftradius="0dp"          android:topleftradius="0dp"          android:toprightradius="5dp"/> <solid     android:color="#c74700"     /> <padding     android:left="0dp"     android:top="0dp"     android:right="0dp"     android:bottom="0dp"     /> <size     android:width="270dp"     android:height="60dp"     /> </shape> 

tl;dr: need way extract shape drawable drawable state list button in order change it's color. or if guys have better solution, i'm ears.

you have make separate drawables individual color , change drawable programmatically not color. changing color program overrides drawble.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -