0

Newbie question. I am using an ImageView in my activity layout and would like similar behavior to a button. I notice that ImageView already generates onClick. What it doesn't do is provide any visual feedback at touch and un-touch, which is what I'd like to add.

As well, I am going to need long-press handling; don't yet know if ImageView does that.

I've dug in a bit and it I see that View implements Runnable; something is calling performClick when the user taps on the image, which then calls ImageView.PerformClick, which finally calls onClick for all the listeners (which is my activity). By the time ImageView handles PerformClick, it's too late to change the image appearance; how do I do it at onShowPress time?

One more note: I've looked at (and tried) adding a gestureDetector to ImageView, but then it appears that I need to reimplement the onClick processing - at any rate, the activity stopped receiving onClick.

There may be a completely different way to do what I want and I'm open to hear about it.

Thanks.

Edit: Test code for monitoring touch actions: package com.example.mockup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

public class ImageGestureButton extends ImageButton
   implements View.OnTouchListener, View.OnClickListener, View.OnDragListener,
   View.OnLongClickListener
{
  public ImageGestureButton (Context context, AttributeSet attrs)
  {
    super(context, attrs);
    setOnTouchListener (this);
    setOnClickListener (this);
    setOnLongClickListener (this);
    setOnDragListener (this);
    setLongClickable (true);
  }

  public boolean onTouch (View v, MotionEvent e)
  {
    System.out.println ("onTouch " + e.getAction());
    return false;
  }

  public void onClick (View v)
  {
    System.out.println ("onClick");
  }

  public boolean onDrag (View v, DragEvent e)
  {
    System.out.println ("onDrag");
    return false;
  }

  public boolean onLongClick (View v)
  {
    System.out.println ("onLongClick");
    return false;
  }

}
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

2 Answers2

1

I would use an ImageButton. Then use setOnClickListener() to wire up the method you want to be called when you click on it. In addition, you can use setOnLongClickListener() for the long press behavior.

Finally, you can also set different backgrounds, colors, etc. depending on the state (focused, selected, etc.). Take a look at the documentation.

As for the test code, your onLongClick() is returning false, which indicates that the long click wasn't consumed. Try changing the return value to true and that should stop onClick() from also getting fired. Also, you'll need to call the startDrag() method in order to fire the onDrag() event. See the android.view documentation for more info on the different event listeners and the startDrag() method.

Jeff
  • 820
  • 9
  • 18
  • Actually, that was one of the first things I tried. The standard button look doesn't work for what I'm trying to do - I want to provide my own regular and "pressed" images. As you say, there's a way to override the default button image behavior. I'll look. But, I have been trying ImageView. – Peri Hartman Aug 04 '12 at 01:18
  • Nonetheless, setOnClickListener() and setOnLongClickListener() also work for ImageView because they are methods of View, a parent class of ImageView. Also, you can assign the src of your ImageView to a state list drawable as seen in [this SO question](http://stackoverflow.com/questions/4185930/how-to-highlight-imageview-when-focused-or-clicked) in order to get different states. [ImageButton](http://developer.android.com/reference/android/widget/ImageButton.html) is a child of [ImageView](http://developer.android.com/reference/android/widget/ImageView.html) so they're very similar. – Jeff Aug 04 '12 at 03:15
  • Progress! So, I added setOnClickListener, setOnLongClickListener, and also setOnDragListener and setOnTouchListener and called setLongClickable. Using debug output, I can see that these handlers are sortof working. What I get for onTouch is reasonable: a "down", possibly some "moves", and an "up". Regardless of the kind of touch, I always see an onClick. I never see onDrag. If I hold for a few seconds, I do see onLongClick (plus an onClick). So, now what do I do to get onClick to only occur for short clicks and why am I not seeing onDrag? (I added some code to my original post) Thanks. – Peri Hartman Aug 06 '12 at 16:28
  • Your onLongClick() is returning false, which indicates that the long click wasn't consumed. Try changing the return value to true and that should stop onClick() from also getting fired. Also, you'll need to call the startDrag() method in order to fire the onDrag() event. See the [android.view documentation](http://developer.android.com/reference/android/view/package-summary.html) for more info on the different event listeners and the startDrag() method. – Jeff Aug 06 '12 at 18:24
  • Excellent! Thanks for your help. Yes, returning true from onLongClick made the difference - I didn't expect onClick to be affected by that. Admottedly, I did not yet read the docs for drag & drop. – Peri Hartman Aug 06 '12 at 19:08
  • Here's a tutorial on [drag and drop](http://www.vogella.com/articles/AndroidDragAndDrop/article.html) that you can take a look at as well. – Jeff Aug 06 '12 at 23:10
  • And if this was indeed the answer to your question, I would go ahead and accept it. – Jeff Aug 06 '12 at 23:11
  • Hi skiman. It took me a few days to respond as I was out of town. Anyway, it more-or-less solved the problem. In addition, I discovered that onClick, onLongClick, etc. should be handled by the activity, not the button. I was under the impression that these were low level handlers and that there was another mechanism to notify the activity. Anyway, I moved them to the activity, figured out that I need to call findViewById to set the listeners. Now, it's working! Next step will be learning D&D. Thanks. – Peri Hartman Aug 10 '12 at 00:24
0

Sounds like what you'd like to do is use and extend this:

http://developer.android.com/reference/android/widget/ToggleButton.html

It has two states AND it also because its a view, has OnClick and OnLongClick available.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45