Pressed state of android selector not working

I made android and arduino apps that control my standing table. I'm uploading this project to github ( https://github.com/neosarchizo/MyStandingDesk )

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/down_button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/down_button" />
</selector>

      

I wanted to use a selector for buttons in MainActivity. If I click the button, then the button image should be changed. But it didn't work. So I change the button image programmatically by checking the MotionEvent.

btnDown.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        btnDown.setImageResource(R.drawable.down_button_pressed);
                        sendCommand("a");
                        break;
                    case MotionEvent.ACTION_UP:
                        btnDown.setImageResource(R.drawable.down_button);
                        sendCommand("s");
                        break;
                }
                return true;
            }
        });

      

Also I wrote the following codes for selector in layout XML file.

<ImageButton
    android:id="@+id/btnDown"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_margin="30dip"
    android:layout_weight="1"
    android:scaleType="fitCenter"
    android:background="@android:color/transparent"
    android:src="@drawable/selector_down_button" />

      

+3


source to share


2 answers


Change this

android:src="@drawable/selector_down_button"

      

to



android:background="@drawable/selector_down_button"

      

You probably want to enable the Press state when you receive an ACTION_DOWN event and disable it when you receive an ACTION_UP event.

  btnDown.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            btnDown.setImageResource(R.drawable.down_button_pressed);
                            sendCommand("a");
                            view.setPressed(true);
                            break;
                        case MotionEvent.ACTION_UP:
                            btnDown.setImageResource(R.drawable.down_button);
                            sendCommand("s");
                            view.setPressed(false);
                            break;
                    }
                    return true;
                }
            });

      

+3


source


Just try this

 android:background="@drawable/selector_down_button"

      



Set the selector as background

+2


source







All Articles