4

I have a button_animation.xml in my res/drawable folder for showing different button states (default, pressed, focused). I reference button_animation.xml in a button in my layout file. It works perfectly, except for when I set an onTouchListener on the button that is actually being pressed. Below is my code.

button_animation.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

layout.xml

     <Button
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/button_animation" />

Code that causes the animation to break

Button button = (Button) findViewById(R.id.button1);
button.setOnTouchListener(this);

Am I not able to show the button state as the documentation suggests and handle onClick at the same time for any particular view?

Documentation: http://developer.android.com/guide/topics/ui/controls/button.html

Thanks,

Jason

Jason
  • 49
  • 8

3 Answers3

8

This answer is pretty old, but just in case anyone comes searching for how to do this, here's how you can do it. @Shadesblade was close, but not quite there.

public boolean onTouch(View button, MotionEvent theMotion) {

    switch (theMotion.getAction()) {

      case MotionEvent.ACTION_DOWN: 
          button.setPressed(true);
          break;
      case MotionEvent.ACTION_UP: 
          button.setPressed(false);
          break;
    }
    return true;
}

This way you can use an xml selector drawable and still toggle the state with an ontouchlistener.

Also make sure that the view "button" is clickable (Button class is clickable by default but if you are using some other view/viewgroup you made need to delcare this in the xml).

Eshaan
  • 673
  • 1
  • 7
  • 11
  • Thank you so much! I was trying things like View.requestFocus() and View.setSelected(true), but this was what I needed. – TheWanderer Sep 02 '17 at 19:14
2

You should be using button.setOnClickListener(this) instead of button.setOnTouchListener(this) and the class should implement OnClickListener.

If you still need to handle onTouch (down and up), you can handle the background setting yourself.

public boolean onTouch( View button, MotionEvent theMotion ) {

   switch ( theMotion.getAction() ) {

      case MotionEvent.ACTION_DOWN: 
          //Set button background here
          break;
      case MotionEvent.ACTION_UP: 
          //set button to default background
          break;
   }
    return true;
}
Shadesblade
  • 782
  • 5
  • 5
  • For this app, when the user holds down a button, a tone will play until the button is released. How can I achieve this with OnClickListener? – Jason Aug 11 '13 at 23:10
  • I've changed the answer – Shadesblade Aug 11 '13 at 23:20
  • That's exactly what I came up with for onTouchListener but was not sure if it was the most efficient way to go. I'll stick with that approach for now, thanks! – Jason Aug 11 '13 at 23:27
2

Just return false.

public boolean onTouch(View view, MotionEvent event) {           
    if (event.getAction() == MotionEvent.ACTION_DOWN) {                

    } else if (event.getAction() == MotionEvent.ACTION_UP) {                

    }

    return false;
}
Aduilio
  • 21
  • 1