Christopher Francisco
Christopher Francisco

Reputation: 16278

Programmatically generated views

I'm adding a view at the top of the current view programmatically by inflating it from an XML. The problem is that after the view is added and showed, I'm still able to press button from the underlying view. How can I prevent this?

Upvotes: 0

Views: 52

Answers (2)

Phil
Phil

Reputation: 36289

Your new view needs to handle the touch event instead. Just create a new, empty touch listener for this purpose:

myTopView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;//true to specify that the event was consumed by the top view
    }
});

Upvotes: 1

Andy Res
Andy Res

Reputation: 16043

Try to set an empty click listener for the view at the top.

For example:

myTopView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
          // empty, just to consume the event.
      }
});

It will do nothing except to consume the event and not let pass it to underlying view.

Upvotes: 1

Related Questions