Issue
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?
Solution
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>
Answered By - Sharad Mhaske
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.