2

hi
Im new to the Touchscreen programming please give me some help!

I have the:

public class PhotoEditDrawView extends ImageView {

and i have the:

@Override
public boolean onTouchEvent(MotionEvent event) {

In the constructor i have the :

setOnLongClickListener(new OnLongClickListener() {
@Override
    public boolean onLongClick(View v) {
        Toast.makeText(ctx, "hello hello ", Toast.LENGTH_SHORT).show();
        return true;
    }
});

The onLongClick is never fired. What am i doing wrong?
Everything in the onTouchEvent is working good.

What i want to do is start an Activity with @android:style/Theme.Dialogwhen pressing 1-2 second.

Erik
  • 5,039
  • 10
  • 63
  • 119

2 Answers2

5

take a look at this little snippet, it works!

public class MyImageView extends ImageView {

private Context mContext;

public MyImageView(Context context) {
super(context);
setBackgroundColor(Color.RED);
mContext = context;
setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
    Toast.makeText(mContext, "hello hello ", Toast.LENGTH_SHORT).show();
    return true;
    }

});
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}

}

make sure you´re returning true in onTouchEvent and onLongClick, so that the events keep firing.

stk
  • 6,311
  • 11
  • 42
  • 58
  • Is there any way to make time longer before onLongClick fires? Its about a second now. I would like 2 second maybe – Erik May 27 '11 at 20:18
  • Im moving text (canvas.drawtext) on the screen and wanted to let user edit font size and color when longpressing. It's about 500ms delay before it fires. My activity is poping up to easy. – Erik May 27 '11 at 20:26
  • 1
    don´t know any way to modify this on a standard onlongclick listener...I guess you´ll have to implement the listener yourself like daveD said. I have also done this before, it´s not too hard (you don´t need a thread to do this). – stk May 27 '11 at 20:35
2

I have had exactly the same problem with an ImageView subclass, the onTouch event fired OK but I couldn't get a Long Press to register with the OnLongClickListener. In the end I just called System.currentTimeMillis() in the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP events to calculated the time difference myself. Not perfect, but it worked around the problem & it works.

daveD
  • 869
  • 1
  • 7
  • 24
  • Thanks. I like to show the activity popup when the finger is still pressed down. Is this possible because the ACTION_UP is fired when finger is lifted right? – Erik May 27 '11 at 19:55
  • 1
    I think you will need a Thread to detect this then, set the time in ACTION_DOWN, clear it in ACTION_UP & have your thread see if the time has not been cleared after X milliseconds. – daveD May 27 '11 at 20:00
  • thanks I have to start an activity from that thread doe. Dunno guess i could do that passing in ctx – Erik May 27 '11 at 20:18
  • im trying this code [link](http://stackoverflow.com/questions/1831490/android-onlongclicklistener-not-firing-on-mapview) from the first Answer written by @ratana. It looks like what you described but the thread is always firring. will work on that a bit – Erik May 27 '11 at 21:35