How to Stop Tap Events From Passing Through an Android Fragment

• 1 min read

By default, the layout view in Android (LinearLayout, RelativeLayout, etc) don’t consume click events. I discovered this trying to show a new fragment above another. Taps were registering on the non-visible fragment below. To fix this, add an attribute to the view to tell it to consume click events with android:clickable="true".

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"

    android:clickable="true">

</LinearLayout>

I hope this tip saves someone else the time I lost :)

If you enjoyed this tutorial, please consider sponsoring my work on GitHub 🤗

Now look what you've done 🌋
Stop clicking and run for your life! 😱
Uh oh, I don't think the system can't handle it! 🔥
Stop it, you're too kind 😄
Thanks for the love! ❤️
Thanks, glad you enjoyed it! Care to share?
Hacker News Reddit

×

Recommended Posts ✍🏻

See All »
• 3 min read
✨ HTML Share Buttons
Read Post »
• 3 min read
🚅 Next Stop, Yaak
Read Post »
• 4 min read
💻 Wait for User to Stop Typing, in JavaScript
Read Post »