Hello in this tutorial, I will show you how to create an overlay view that will appear on top of other apps using Accessibility Service.
Hello in this tutorial, I will show you how to create an overlay view that will appear on top of other apps using Accessibility Service.
Creating the Service
Begin by creating a class that extends AccessibilityService. Then override the onAccessibilityEvent() and onInterrupt() required methods to remove the error.
Now go to the manifest file and add the service to the app using the <service> element.
For the last part, add an intent filter for android.accessibilityservice.AccessibilityService inside the service element. Without this filter, your accessibility service will be ignored and will not work.
Build the Overlay
Since we want to create an overlay that will appear on top of other apps, we need to get a hold of the display of the device and add views to it using WindowManager.
Go into the Accessibility Service class and override the onServiceConnected() method. This will be called when the user turns on the service from the settings.
Next, declare a WindowManager object and inside onServiceConnected, get the display by using getSystemService() and passing in WINDOW_SERVICE.
Adding Views to the Window
Add a TextView inside and set the text, then change the color to white.
For the LinearLayout, set the width and height to wrap_content, change the background to white, and the add some paddings to it.
Go back to the Accessibility Service class to add the layout. Grab the WindowManager object from earlier and call addView().
Go back to the Accessibility Service class to add the layout. Inside onServiceConnected, grab the WindowManager object from earlier and call addView().
It takes 2 arguments, (1) the first is the Layout we want to add so use the LayoutInflater to get the layout and apply it to the method.
WindowManager.LayoutParams appOverlayLayoutParams = new WindowManager.LayoutParams();
For the overlay to show, we must set the type to TYPE_ACCESSIBILITY_OVERLAY.
Now apply the layout params in addView().
If we run the app, and go to the accessibility settings, we can turn on the service.
Changing the position
appOverlayLayoutParams.gravity = Gravity.TOP;
If you need to place it on a specific spot, use TOP and LEFT and then set the x and y position.