0

i have a button on my project and the effect gone on touch event. i want that when someone touch that button it should change.but nothing working for me from the XML. here is my code below. java code.

btnOn.setOnTouchListener(new View.OnTouchListener() {

    //@Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mConnectedThread.write("A");
            Toast.makeText(getBaseContext(), "LED ON NOW", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            mConnectedThread.write("C");
            Toast.makeText(getBaseContext(), "LED SWITCHED OFF", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
});

and here is the XML code below.

<Button
    android:id="@+id/buttonOn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="LED ON/OFF"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="148dp"/>
  • i tried with selector and image button http://stackoverflow.com/questions/7175873/click-effect-on-button-in-android but it's not working. – Imraj Hossain Jun 12 '16 at 17:36

1 Answers1

0

i found a java code but didn't get any working XML code. here is the java code.

public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mConnectedThread.write("A");
                v.getBackground().setColorFilter(0xe0f47521, PorterDuff.Mode.SRC_ATOP);
                v.invalidate();
                Toast.makeText(getBaseContext(), "LED ON NOW", Toast.LENGTH_SHORT).show();
                return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mConnectedThread.write("C");
                Toast.makeText(getBaseContext(), "LED SWITCHED OFF", Toast.LENGTH_SHORT).show();
                v.getBackground().clearColorFilter();
                v.invalidate();
                return true;
            }
            return false;
        }
    });