Search code examples
androidonlongclicklistener

android I dont understand Long press detection in my subclass ImageView


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.


Solution

  • 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.