Mobile Development 12 min read

Android Guide Management, One‑Line Pressed State, and Expanding Click Area Techniques

The article introduces three practical Android techniques—a singleton‑based ordered guide manager to avoid conflicting tutorials, a one‑line Kotlin extension that adds a pressed‑state alpha effect to any view, and a concise TouchDelegate utility that expands a view’s clickable area without layout changes.

Baidu Geek Talk
Baidu Geek Talk
Baidu Geek Talk
Android Guide Management, One‑Line Pressed State, and Expanding Click Area Techniques

This article from Baidu’s front‑line engineers presents three practical Android development tricks: an ordered guide‑management system, a one‑line Kotlin extension to add a pressed‑state effect to any View, and a one‑line method to enlarge a View’s clickable area.

1. Ordered Guide Management

When multiple feature guides may appear simultaneously, conflicts can degrade user experience. The solution is to define guide types with an enum and register each guide in a singleton GuideManager that tracks registration, display state, and whether the guide has already been shown.

enum class GuideType {
GuideTypeA,
...,
GuideTypeN
}
object GuideManager {
private val guideMap = mutableMapOf<Int, GuideModel>()
fun registerGuide(
guideType: GuideType,
show: () -> Unit,
isShowing: () -> Boolean,
hasShown: () -> Boolean,
setHasShown: () -> Unit) {
guideMap[guideType.ordinal] = GuideModel(show, isShowing, hasShown, setHasShown)
}
...
}
object GuideManager {
...
fun show(guideType: GuideType) {
val guideModel = guideMap[guideType.ordinal] ?: return
if (guideModel.isShowing.invoke() || guideModel.hasShown.invoke()) {
return
}
guideMap.forEach {
if (it.value.isShowing().invoke()) {
return
}
}
guideModel.run {
show().invoke()
setHasShown().invoke()
}
}
}
object GuideManager {
...
fun release() {
guideMap.clear()
}
}

The manager can be extended with additional interception rules, such as disabling all guides under certain business conditions.

2. One‑Line Pressed State for a View

Typical pressed‑state effects use a selector XML, which requires extra drawable resources. A more lightweight approach is to adjust the View’s alpha via a Kotlin extension:

@JvmOverloads
fun View.addPressedState(pressedAlpha: Float = 0.2f) = run {
setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> v.alpha = pressedAlpha
MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> v.alpha = 1.0f
}
// return false so the event continues to propagate
false
}
}

This single line of Kotlin adds a pressed‑state effect without extra resources, though it only works on Android 6.0+.

3. One‑Line Expansion of a View’s Click Area

Expanding a small button’s touch region can be done via three strategies: layout padding, custom event handling, or Android’s TouchDelegate . The recommended solution uses TouchDelegate and can be wrapped in a utility method:

public static void expandTouchArea(View ancestor, View child, int left, int top, int right, int bottom) {
if (child != null && ancestor != null) {
MyTouchDelegate touchDelegate;
if (ancestor.getTouchDelegate() instanceof MyTouchDelegate) {
touchDelegate = (MyTouchDelegate) ancestor.getTouchDelegate();
touchDelegate.addExpandChild(child, left, top, right, bottom);
} else {
touchDelegate = new MyTouchDelegate(child, left, top, right, bottom);
ancestor.setTouchDelegate(touchDelegate);
}
}
}

A custom MyTouchDelegate can maintain a map of child Views and their expanded bounds, allowing multiple children to share a single delegate. For Android 8.0 and earlier, a known bug requires resetting the internal mDelegateTargeted flag when the touch leaves the delegate area; overriding onTouchEvent as shown fixes the issue.

public class MyTouchDelegate extends TouchDelegate {
private Map
mDelegateViewExpandMap = new HashMap<>();
@Override
public boolean onTouchEvent(MotionEvent event) {
// iterate over map, calculate target child, forward event
...
}
public void addExpandChild(View delegateView, int left, int top, int right, int bottom) {
ExpandBounds expandBounds = new ExpandBounds(new Rect(), left, top, right, bottom);
mDelegateViewExpandMap.put(delegateView, expandBounds);
}
}

By combining these snippets, developers can manage feature guides cleanly, add pressed‑state feedback with a single line of Kotlin, and enlarge touch areas without altering layouts.

AndroidKotlinTouchDelegateClickAreaGuideManagerPressedState
Baidu Geek Talk
Written by

Baidu Geek Talk

Follow us to discover more Baidu tech insights.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.