17
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】Layoutを重ねたときに、下のLayoutにタッチイベントを発生させない方法

Posted at

FrameLayoutやRelativeLayoutの上に別のLayoutを重ねたときに、下のLayoutにタッチイベントを発生させない方法のメモ。

.java
RelativeLayout parent = new RelativeLayout(getContext());
RelativeLayout child = new RelativeLayout(getContext());

parent.addView(child);

例えばこんな感じでRelativeLayoutを重ねると、childをタッチした後にparentのタッチイベントが発生してしまう。
そのため、タッチした座標にparentのButtonなどがあれば反応してしまう。

parentにタッチイベントを発生させたくない時は、以下のようにchildのTouchListenerの戻り値をtrueにしてあげればよい。

.java
child.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return true;
    }
});

17
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?