Android : Custom Button Doesn't work with OnTouchListener

on Sunday, March 8, 2015


I have a problem with OnTouchListener. I created a custom button this button properly work with onClick event. But it doesnt workwith onTouch Event


This is my custom button i want only two options just button and button_pressed.



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


This is my Ontouch button code. When I pressed the button (when my finger on the button) Audio is playing and looping. When I unpress the button audio is stopping. This code correctly work but this code doesnt work with my custom button. So when my finger on this button, when I unpressed, or when hold the button shortyl, in every case this button show me only button2.png(above the custom button code)



pl6.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:

n6=MediaPlayer.create(MainActivity.this, R.raw.audio4);
n6.start();
n6.setLooping(true);
return true;

case MotionEvent.ACTION_UP:
n6.stop();
n6.release();

break;
}

return false;

}


});

0 comments:

Post a Comment