1

I have an ImageButton and I want to make it so the button background changes color when the button is pressed. I have copied the button_bg.xml file from this question.

button_bg.xml looks like this:

<?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="#ff000000"/> <!-- default -->
</selector>

And line #54 looks like this:

<ImageButton
     android:id="@+id/sendButton"
     android:background="@drawable/button_bg"
     android:src="@drawable/ic_action_send_now"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginRight="4dp"
     android:layout_alignParentRight="true" />

I have tried removing the line:

android:background="@drawable/button_bg"

which stops the application crashing but the buttons don't change color.

Any help would be appreciated

Community
  • 1
  • 1
PriestVallon
  • 1,519
  • 1
  • 22
  • 44
  • possible duplicate of [android.view.InflateException: Binary XML file line #39: Error inflating class](http://stackoverflow.com/questions/17347293/android-view-inflateexception-binary-xml-file-line-39-error-inflating-class) – Codeman Oct 16 '13 at 17:40
  • What version of Android are you testing this on? I suspect the problem is in the `android:src`, rather than the `android:background`. – Codeman Oct 16 '13 at 17:40
  • Try adding Drawable x = getResources().getDrawable(R.drawable.button_bg"); to your Activity#onCreate() method before the setContentView() call. If it works, your drawable is OK. If it fails you might get a better error message in logcat. – Dale Wilson Oct 16 '13 at 20:45
  • Below the InflateException in your stacktrace, there's likely "caused by" exception(s) that describe the specific issue. Please include the complete stacktrace in the question. – laalto Oct 17 '13 at 08:22
  • The very first Caused by is as follows: java.lang.reflect.InvocationTargetException. The next one after it says Caused by: android.content.res.Resources$NotFoundException: File res/drawable/button_bg.xml from drawable resource ID #0x7f020001 – PriestVallon Oct 17 '13 at 13:54

1 Answers1

0

By changning button_bg.xml to the following:

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

and adding the following to strings.xml

<drawable name="clr_normal">#AAAAAA</drawable>
<drawable name="clr_pressed">#777777</drawable>

the problem was solved and the code worked as intended.

PriestVallon
  • 1,519
  • 1
  • 22
  • 44
  • I had been trying to assign to a color attribute when what the correct thing to do was assign it to the background attribute. – PriestVallon Feb 04 '16 at 09:26