Abby
Abby

Reputation: 1821

android change image of ImageButton on pressed with selector xml

I have an ImageButton in Android, and need to change the image when the button is pressed so it's clear to the user that the button is being pressed.

What I've tried is to use an xml with a selector in the drawable folder my images are in. My code is as follows:

xml

<ImageButton
    android:id="@+id/upButton"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/up_button"
    android:background="#00000000"/>

up_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/up_pressed"
        android:state_pressed="true"/>
    <item android:drawable="@drawable/up"/>

</selector>

java onTouchListener

The text of textView is changed when the button is pressed, and changed back when the button is released.

upButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    textView.setText("Up button pressed.");
                    break;
                case MotionEvent.ACTION_UP:
                    textView.setText("Up button released.");
                    break;
            }
            return true;
        }
    });

When searching for similar questions on this site, I found Android button on pressed, but this wasn't very helpful as it didn't have an answer. I've also found a lot of other similar questions and tried those answers, but none of it worked. I started with what is in the Android documentation, and when going through other questions tried variations of it. https://developer.android.com/guide/topics/ui/controls/button.html

Upvotes: 3

Views: 1137

Answers (1)

Hussein El Feky
Hussein El Feky

Reputation: 6707

If you use onTouch() on a button, its onClick() functionality will not work. So you can let it work by adding certain methods for the button you touch like this:

upButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    textView.setText("Up button pressed.");
                    upButton.setPressed(true);
                    //Use this if you want to perform onClick() method.
                    //upButton.performClick();
                    break;
                case MotionEvent.ACTION_UP:
                    textView.setText("Up button released.");
                    upButton.setPressed(false);
                    break;
            }
            return true;
        }
    });

Upvotes: 2

Related Questions