Android I don't understand Long detection in my ImageView subclass

hi
i am new to touch screen programming please help me!

I have:

public class PhotoEditDrawView extends ImageView {

      

and I have:

@Override
public boolean onTouchEvent(MotionEvent event) {

      

In the constructor, I have:

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

      

onLongClick

never starts. What am I doing wrong?
Everything onTouchEvent

works well.

What I want to do is start the operation with @android:style/Theme.Dialog

when pressed for 1-2 seconds.

+2


source to share


2 answers


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 go back to onTouchEvent and onLongClick so events keep firing.

+5


source


I had exactly the same problem with the ImageView subclass, the onTouch event fired fine, but I could not get the long press to register with the OnLongClickListener. In the end, I just called System.currentTimeMillis () on the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP events to calculate the time difference myself. Not perfect, but he worked on the problem and it works.



+2


source







All Articles