android I dont understand Long press detection in my subclass ImageView

Erik picture Erik · May 27, 2011 · Viewed 10.7k times · Source

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.

Answer

stk picture stk · May 27, 2011

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.