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

5
  • possible duplicate of android.view.InflateException: Binary XML file line #39: Error inflating class Commented Oct 16, 2013 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. Commented Oct 16, 2013 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. Commented Oct 16, 2013 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. Commented Oct 17, 2013 at 8: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 Commented Oct 17, 2013 at 13:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I had been trying to assign to a color attribute when what the correct thing to do was assign it to the background attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.