0

I have btn_solve_selector.xml file in Drawable folder in order to set Button background programatically.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
      android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff0000ff"/> <!-- default -->
</selector>

With above code, I coded below code in onCreate of MyActivity.

    _button1.setBackgroundResource(R.drawable.btn_solve_selector);

But the app was crashed with followed stacktrace.

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_solvenow_selector.xml from drawable resource ID #0x7f020085
....
at com.MyPackage.MyActivity.setMode(MyActivity.java:343)
....
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
....

It seems like the drawable xml file is failed to read even the file has <item /> inside of it and then fall-back to read android.content.res as a second trial. (I even checked with com.MyPackage.R.drawable.btn_solve_selector instead but failed).

I removed and reinstalled the app but failed. What am I missing?

Youngjae
  • 24,352
  • 18
  • 113
  • 198

2 Answers2

2

Try to use android:drawable instead of android:color in your selector xml file, it works for me.

Try cleaning the project too

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • Sorry to change the answer flag. Sharad gives the solution with pure xml content. (and I upvote yours) – Youngjae Jun 03 '13 at 07:52
  • Best answer, but there's a little more to it: http://stackoverflow.com/a/17701589/507339 - Still not as much XML as Mhaske's answer. – Nilzor Aug 18 '14 at 10:56
  • @AAnkit I'm too facing the same problem, I applied answer given by "Sharad Mhaske" but no luck and as told by you, I use android:drawable instead of android:color, even this too didn't worked, I'm getting same error. Any Ideas on how to resolve this, on android 4.4 upwards, the same code works but in android 2.3 v10, this error get encountered. – codemilan Nov 11 '16 at 08:27
  • I will have to look at your code. Can you create a question, post code there, post link here, I will look at it. – AAnkit Nov 11 '16 at 19:51
1

use this-

    <selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false">
    <shape>
            <gradient
                android:startColor="#E51400"
                android:endColor="#E51400"
                />
              <stroke
                android:width="3dp"
                android:color="#FFFFFF" />
              <corners
                android:radius="10dp" />

                <padding
                android:left="5dp"
                android:top="4dp"
                android:right="5dp"
                android:bottom="4dp" />

        </shape>
</item>

  <item android:state_pressed="true">
    <shape>
            <gradient
                android:startColor="#4AA038"
                android:endColor="#72C437"
               />

             <stroke
                android:width="3dp"
                android:color="#FFFFFF" />

             <corners
                android:radius="10dp" />
               <padding
                android:left="5dp"
                android:top="4dp"
                android:right="5dp"
                android:bottom="4dp" />

        </shape>
</item>



</selector>
Sharad Mhaske
  • 1,103
  • 9
  • 19