java - How to apply a res/color/xml style to a button created dynamically -


i have xml file, res/color/btn_black allows me apply gradient buttons.

i can use in layout.xml calling:

<button     android:background="@color/btn_black" /> 

elsewhere, creating buttons dynamically in java, , want apply same style. when try using:

mybutton.setbackgroundcolor(getresources().getcolor(r.color.btn_black)); 

i error:

android.content.res.resources$notfoundexception:    file res/color/btn_black.xml color state list resource id #0x7f040001 

this seems correct method other questions i've found answered here, isn't working me. doing wrong?

edit: file btn_black.xml reference

<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item android:state_pressed="true" >     <shape>         <solid             android:color="#343434" />         <stroke             android:width="1dp"             android:color="#171717" />         <corners             android:radius="3dp" />         <padding             android:left="10dp"             android:top="10dp"             android:right="10dp"             android:bottom="10dp" />     </shape>   </item>   <item>     <shape>         <gradient             android:startcolor="#343434"             android:endcolor="#171717"             android:angle="270" />         <stroke             android:width="1dp"             android:color="#171717" />         <corners             android:radius="4dp" />         <padding             android:left="10dp"             android:top="10dp"             android:right="10dp"             android:bottom="10dp" />     </shape>   </item> </selector> 

seems color in colors.xml defined in wrong way. colors.xml should this:

<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="btn_black">#000000</color> </resources> 

as see, have defined res/color/btn_black.xml, wrong. need create colors.xml file in /res/values/ directory.

if have gradient xml-file, need put in /res/drawable/ folder , call mybutton.setbackground(getresources().getdrawable(r.drawable.btn_black)) method.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -