Android: programmatically changing to a solid xml shape makes text disappear -


i have button in layout background , textcolor defined selectors. when unpressed button has background color , textcolor , when pressed color , text color - e.g. white background black text -> black background white text.

at point in code need replace background/text color third set of colors , go original selector defined in xml. however, after going text no longer appears , instead solid colored button.

<button android:id="@+id/somebutton" android:layout_width="80" android:layout_height="80" android:textcolor="@color/sometext_selector" android:background="@drawable/somebackground_selector"             android:gravity="center"             android:text="@string/sometext"             android:textsize="15sp"             /> 

at point fine, when this:

somebutton.setbackgrounddrawable(resourcescompat.getdrawable(getresources(), r.drawable.backgroundred, null));  somebutton.settextcolor(getresources().getcolor(android.r.color.holo_red_light)); 

and afterwards this:

somebutton.setbackgrounddrawable(resourcescompat.getdrawable(getresources(), r.drawable.originalbackgroundselectordefinedinxml, null));  somebutton.settextcolor(getresources().getcolor(r.color.originaltextselectordefinedinxml)); 

is when problems begin.

here xmls selectors - apologies pseudocode, imagine appropriate hex values:

first - background:

<item android:drawable="@drawable/color_purple_full" android:state_selected="true"/> <item android:drawable="@drawable/color_purple_full" android:state_pressed="true"/> <item android:drawable="@drawable/color_purple_full" android:state_focused="true"/> <item android:drawable="@drawable/color_purple_outline"/> 

color_purple_outline:

android:shape="oval"> <stroke android:width="2dp" android:color="#somepurplehexa" /> <size android:width="80dp" android:height="80dp"/> </shape> 

color_purple_full:

android:shape="oval"> <solid android:color="#somepurplehexa"/> <size android:width="80dp" android:height="80dp" /> </shape> 

for text:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:color="@android:color/black" android:state_selected="true"/> <item android:color="@android:color/black" android:state_pressed="true"/> <item android:color="@android:color/black" android:state_focused="true"/> <item android:color="@color/green"/> 

what i'm seeing correct background in unpressed state in pressed state i'm getting solid color no text on @ all. i'd grateful ideas why happening , why in original xml works fine stops working after changing programmatically?

solved:

turns out problem here:

somebutton.settextcolor(getresources().getcolor(r.color.originaltextselectordefinedinxml)); 

should have been

somebutton.settextcolor(getresources().getcolorstatelist(r.color.originaltextselectordefinedinxml)); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -