0

I have an xml file, res/color/btn_black that allows me to apply a gradient look to buttons.

I can use it in a layout.xml successfully by calling:

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

Elsewhere, I am creating buttons dynamically in Java, and I want to apply the same style. When I try this using:

myButton.setBackgroundColor(getResources().getColor(R.color.btn_black));

I get this error:

android.content.res.Resources$NotFoundException: 
  File res/color/btn_black.xml from color state list resource ID #0x7f040001

This seems to be the correct method from other questions I've found answered here, but it isn't working for me. What am I doing wrong?

edit: This is the file btn_black.xml for 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>
Anonymous Man
  • 2,776
  • 5
  • 19
  • 38

2 Answers2

2

Seems like a color in your colors.xml defined in wrong way. Your colors.xml should look like this:

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

As I see, you have defined res/color/btn_black.xml, what is wrong. You need to create colors.xml file in /res/values/ directory.

If you have a gradient xml-file, then you need to put it in /res/drawable/ folder and call myButton.setBackground(getResources().getDrawable(R.drawable.btn_black)) method.

yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • see additional xml above. it's not just a color, it's a style. It works perfectly fine in an xml layout, I just need to know how to apply it in Java. – Anonymous Man Apr 08 '13 at 22:13
  • to make it as a style, define a style for your button in /res/values/styles.xml and a gradient in /res/drawable/my_gradient.xml which you can add to your style with @drawable/my_gradient – yyunikov Apr 08 '13 at 22:18
  • you can check how to make a style for button in this discusion:http://stackoverflow.com/questions/6608170/how-to-define-button-background-at-one-place-and-used-around-the-whole-app – yyunikov Apr 08 '13 at 22:20
  • how to make a selector drawable, you can read in this post:http://stackoverflow.com/questions/4692642/android-customized-button-changing-text-color – yyunikov Apr 08 '13 at 22:24
  • Does it matter which drawable folder I put it in, (hdpi, mdpi, xhdpi etc.)? Does there need to be a copy in each drawable folder? – Anonymous Man Apr 08 '13 at 22:32
  • Every folder depends on different device density. You can check this to read more about it:http://developer.android.com/guide/practices/screens_support.html. If you need only gradient you can just make res/drawable folder and put it there. – yyunikov Apr 09 '13 at 06:20
  • Placed the xml in res/drawable and tried applying it with myButton.setBackground(getResources().getDrawable(R.drawable.btn_black)); this tells me "Call requires API level 16 (current min is 8): android.widget.Button#setBackground" setBackgroundDrawable is deprecated. setBackgroundColor and setBackgroundResource do not work with drawable resources. Any more ideas? – Anonymous Man Apr 09 '13 at 14:46
  • Check this answer: http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android, you can use something like this, and you need to set buildTarget api 16 and min build to 7 or something similar. And please accept this as a best answer if this helped you. – yyunikov Apr 10 '13 at 10:49
  • I just taught myself to draw the button completely in java and gave up on trying to apply the existing xml. Moved on with my life. :) Thanks for your efforts along the way. – Anonymous Man Apr 10 '13 at 20:23
0

i think the best solution is to define a layout for the button with a specific style. Then you can inflate the button with the layout you've created.

See this question for specific help android set style in code

So in your case you can use your xml file with the defined layout and inflate the button created programmatically

Community
  • 1
  • 1
Fabrizio Duroni
  • 747
  • 1
  • 11
  • 24
  • This was partially helpful. I changed my code to Button myButton = new Button(EventsActivity.this, null, R.color.btn_black); It no longer crashes, but the button does not display. Just the text. (Not even the default gray button is visibly now.) – Anonymous Man Apr 08 '13 at 22:14