While writing another app to help me with Android app development I thought it would be great to have the functionality of an app that I had used in the past in the app also.

That app is called Texpand and it allows you to save multiple files of text with associated abbreviations so that you can recall them in other apps on your device.

I always wondered how they achieved that functionality, so as Texpand was closed source, I set out to find out if I could replicate it in my own app.

This led to the creation of the app above that I’ve called Snippet. As well as publishing it on the play store it’s open source so feel free to take a look at the code on my Github account – https://github.com/andyb129/Snippet

Texpand still has its place as it has more functionality, but hopefully this simple implementation will be useful to people as well and as it’s open source I hope it helps other dev’s learn about the AccessibilityService in Android that they could use in their own apps.

How it works

Essentially the app centers around the AccessibilityService class that is available in Android.  You can create your own AccessibilityService and provide the user with information depending on the events that happen as show below (either across all android apps or specific ones).

class SnippetTextAccessibilityService: AccessibilityService() {

    override fun onAccessibilityEvent(accessibilityEvent: AccessibilityEvent?) {

        if (rootInActiveWindow != null) {
            if (accessibilityEvent.eventType == TYPE_VIEW_TEXT_CHANGED) {
                //perform action
            }
        }
    }
}

Once we have isolated the event, in this case TYPE_VIEW_TEXT_CHANGED , then we can use the information in that event to display the overlay to the user that you see in Snippet.

The overlay is the second part of the puzzle in this app and is generated by creating a Service and displaying a layout to the user that they can tap which then pastes the snippet of text.

The text snippets are searched and retrieved from a simple Room database that is mainly based on this excellent example provided by the team at Google (with a few tweaks) –

https://github.com/googlecodelabs/android-room-with-a-view/tree/kotlin 

I hope people find this useful and can learn something from it’s implementation. Thanks for reading & enjoy!