Tarida George
Tarida George

Reputation: 1303

Implement View.OnTouchListener on custom control

I created a view that extend RelativeLayout, and I what that, this view to be able to handle touch events. So what I thought about was to implement the OnTouchListener in my custom control, like this:

class MyCustomControl extends RelativeLayout implements View.OnTouchListener {
    public MyCustomControl (Context context) {
         super(context);
    }

    public MyCustomControl (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomControl (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    other_methods;

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        //event handle here.
    }
}

It doesn't work but if I do something like this, it does:

MyCustomControl control = (MyCustomControl) LayoutInflater.from(context).inflate(R.layout.my_control, null);
control.setOnTouchListener(control);

What should I do in order to have the touchListener built-in(already defined and activated) in my custom control ?

Upvotes: 2

Views: 1278

Answers (1)

Olga Zb
Olga Zb

Reputation: 46

I know, it's late, but I solved my case by adding

this.setOnTouchListener(this);

to constructor

Upvotes: 3

Related Questions