Skip to content → Skip to footer

How to Add Custom Accessibility Actions to Android Apps with Java and Kotlin

Greetings fellow developers,

I’m Aaryan Kumar Andarpa, the Lead Developer at Tech-Freedom, and I’m here to champion the cause of accessibility in Android app development. Today, I want to shed light on a critical aspect of accessibility: custom accessibility actions. These actions are not merely an optional feature but a fundamental element that ensures all users, regardless of their abilities, can interact with our apps seamlessly.

Why Add Custom Accessibility Actions?

Before we delve into the technical details, let’s understand why custom accessibility actions matter. As developers, it’s our responsibility to create apps that are inclusive and accessible to everyone. By adding custom accessibility actions, we empower users with disabilities to navigate our apps more efficiently. Whether it’s a simple button click or a complex interaction, custom accessibility actions ensure that no user is left behind.

Realizing Accessibility Actions in Real-Time Views

Now, let’s get practical. We’ll walk through how to add custom accessibility actions using both Java and Kotlin, leveraging the power of ViewBinding for seamless integration.

Example in Java with ViewBinding

In our Tech-Freedom app, let’s consider a scenario where we have a button, and we want to add a custom accessibility action to it. First, let’s set up ViewBinding in our activity:

Java:
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        
        // Now, let's add a custom accessibility action to our button
        setAccessibilityAction(binding.button, "Custom Action", view -> {
            // Perform your custom action here
            // For example, navigate to a specific screen
            startActivity(new Intent(MainActivity.this, DetailsActivity.class));
        });
    }
    
    private void setAccessibilityAction(View actionView, String label, ViewCompat.AccessibilityActionCompat action) {
        ViewCompat.addAccessibilityAction(actionView, label, action);
    }
}

In this example, we use ViewBinding to access our button, making our code more readable and concise. We then add a custom accessibility action to the button, allowing users to perform a specific action when interacting with it.

Converting to Kotlin

Now, let’s rewrite the same functionality in Kotlin, embracing its concise syntax and expressive nature:

Kotlin
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        // Adding a custom accessibility action to our button
        setAccessibilityAction(binding.button, "Custom Action") { view ->
            // Perform your custom action here
            // For example, navigate to a specific screen
            startActivity(Intent(this@MainActivity, DetailsActivity::class.java))
        }
    }
    
    private fun setAccessibilityAction(actionView: View, label: String, action: ViewCompat.AccessibilityActionCompat) {
        ViewCompat.addAccessibilityAction(actionView, label, action)
    }
}

In Kotlin, we leverage the power of extension functions and lambda expressions to achieve the same functionality in a more concise and expressive manner.

Raising Awareness: Why It Matters

As developers, it’s our responsibility to create apps that are accessible to everyone. By adding custom accessibility actions, we ensure that users with disabilities can navigate our apps with ease. Let’s spread the word and encourage our fellow developers to prioritize accessibility in their app development journey.

In conclusion, custom accessibility actions are not just a feature; they’re a statement of inclusivity and empathy. Let’s join hands and build a digital world where everyone has equal access to technology. Together, we can make a difference, one accessibility action at a time.

Happy coding!

Get to know accessibility actions, the useful yet overlooked screen reader feature

About Author

Aaryan Kumar Andarpa

Aaryan is the Lead Developer at Tech-Freedom.

Published in Developer Guides

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Donate to Us

To uphold the standards of a robust and fully accessible project, we graciously request your support. Even a modest contribution can have a profound impact, enabling Accessible Android to continue its growth and development.

Donations can be made via PayPal.

For alternative methods, please do not hesitate to contact us.

We deeply appreciate your generosity and commitment to our cause.

Subscribe to Blind Android Users mailing list

RSS Accessible Android on Mastodon

  • Untitled
    What’s New in TalkBack 16.0 https://accessibleandroid.com/whats-new-in-talkback-16-0/
  • Untitled
    Gemini Live Camera Stream and Screen Sharing Impressions: Is Gemini Ready to Be the AI Assistant for the Blind? https://accessibleandroid.com/gemini-live-camera-stream-and-screen-sharing-impressions-is-gemini-ready-to-be-the-ai-assistant-for-the-blind/
  • Untitled
    New app added to the Accessible Android apps directory: Jacket.fm — Social Audio https://accessibleandroid.com/app/jacket-fm-social-audio/
  • Untitled
    New bug posted to the bug tracker: TalkBack navigates between keyboard items instead of moving focus out of the Gboard window when swiping https://accessibleandroid.com/bugs/talkback-navigates-between-keyboard-items-instead-of-moving-focus-out-of-the-gboard-window-when-swiping/