IDN Media Fauzi Mubarok Nov 20, 2024

Building a Floating View in Android: A Practical Approach - Part 1

Article Summary

Fauzi Mubarok from IDN Engineering breaks down how to build those addictive floating reward bubbles you see in TikTok and Shopee. Turns out, creating smooth overlay views in Android requires some clever system-level tricks.

This practical guide walks through building interactive floating views that overlay the entire Android screen, independent of activities or fragments. The tutorial covers custom layouts with Lottie animations, touch handling, and smooth movement animations that stick to screen edges.

Key Takeaways

Critical Insight

Building floating views requires managing system overlays, precise touch event handling, and responsive animations that adapt to screen orientation changes.

Part 2 promises to reveal magnetic effects and how to properly attach, detach, and remove these floating views from activities.

About This Article

Problem

Fauzi Mubarok needed to tell the difference between a user clicking and dragging on floating views. The challenge was detecting how long the touch lasted and how far it moved to avoid accidentally dismissing the view while repositioning it.

Solution

He built an isOnClickEvent() function that uses TOUCH_TIME_THRESHOLD and CLICK_TOLERANCE constants. These check that the touch was quick and that movement in both X and Y coordinates stayed within acceptable limits.

Impact

This touch detection logic lets users click reliably while still being able to drag smoothly. They can reposition floating views without accidentally triggering reward claims or dismissals.