Hello Stack Overflow community,
I am currently working on an Android application where I need to display a custom layout over the accessibility screen. My intention is to provide additional functionality to users who are utilizing accessibility services.
I have tried implementing a solution using a custom layout and the WindowManager, and it works well in most scenarios, except when the accessibility screen is triggered. In that case, the custom layout loses focus, and the accessibility screen takes precedence.
I have ensured that all necessary permissions, including the SYSTEM_ALERT_WINDOW permission, are granted. Despite this, the behavior persists.
private fun showCustomLayout() {
try {
val inflater = LayoutInflater.from(this)
val customLayout = inflater.inflate(R.layout.display_over_other_app_layout, null)
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
@Suppress("DEPRECATION")
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
},
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH or
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR or
WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN or
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
PixelFormat.TRANSLUCENT
)
params.flags = params.flags or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
params.title = "YourCustomWindowTitle"
params.x = 0
params.y = 0
requestAccessibilityPermission()
customLayout.setOnClickListener {
val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
windowManager.removeView(customLayout)
}
val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
windowManager.addView(customLayout, params)
} catch (e: Exception) {
Log.e("YourActivity", "Error in showCustomLayout", e)
}
}
Certainly! Crafting a professional and clear question is key to receiving helpful responses on Stack Overflow. Here's a suggestion for how you might phrase your question:
Title:
Issue with displaying a custom layout over the Android accessibility screen
Body:
Hello Stack Overflow community,
I am currently working on an Android application where I need to display a custom layout over the accessibility screen. My intention is to provide additional functionality to users who are utilizing accessibility services.
I have tried implementing a solution using a custom layout and the WindowManager, and it works well in most scenarios, except when the accessibility screen is triggered. In that case, the custom layout loses focus, and the accessibility screen takes precedence.
I have ensured that all necessary permissions, including the SYSTEM_ALERT_WINDOW permission, are granted. Despite this, the behavior persists.
Here's a simplified version of the code I'm using:
kotlin Copy code // Your code here I have experimented with various combinations of flags, but I have not been able to find a solution that allows the custom layout to remain visible even when the accessibility screen is active.
Has anyone encountered a similar issue or have insights into how I might achieve the desired behavior? Any guidance or alternative approaches would be greatly appreciated.