Skip to content → Skip to footer

Code examples for button labeling on Android accessibility

When it comes to designing accessible Android apps, button labelling plays a crucial role. Properly labelled buttons ensure that users, including those with disabilities, can easily understand and interact with the functionality of your app. In this tutorial, we’ll provide you with some code examples that demonstrate best practices for button labelling on the Android platform, enabling you to create a more inclusive and accessible user experience.

XML Approach

In Android, button labelling can be done through XML layout files. Here’s an example of labelling a button using the android:text attribute:

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    />
  

In the above example, the button is given the label “Submit” through the android:text attribute. Remember to use clear and descriptive labels that convey the purpose or action associated with the button.

String Resource Approach

To promote localization and maintain consistency in button labelling, it’s recommended to use string resources. Here’s an example of defining a button label in a string resource file (res/values/strings.xml):

<string name="submit_button_label">Submit</string>
  

You can then reference the string resource within the button’s android:text attribute:

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/submit_button_label"
    />
  

Using string resources allows for easy translation and adaptation to different languages, ensuring that your app is accessible to a global audience.

Programmatically Set Button Label

In some cases, button labels may need to be dynamically set based on runtime conditions. Here’s an example of setting the button label programmatically in your Java or Kotlin code:

Button submitButton = findViewById(R.id.submitButton);
submitButton.setText(R.string.submit_button_label);
  

In this example, the button label is set using the string resource R.string.submit_button_label. This approach allows for flexibility in changing button labels based on different scenarios or user interactions.

Iconography with Button Label

Combining iconography with button labels can enhance accessibility, especially for users with cognitive or language-related disabilities. Here’s an example of adding an icon alongside the button label using the android:drawableLeft attribute:

<Button
    android:id="@+id/searchButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_search"
    android:text="Search"
    />
  

In the above example, the ic_search drawable is set as the left drawable of the button using android:drawableLeft. The label “Search” is then provided as the button text. This combination of iconography and text aids users in understanding the button’s functionality.

Conclusion

Button labelling is a crucial aspect of creating accessible Android apps. By following best practices such as using clear and descriptive labels, utilizing string resources for localization, programmatically setting button labels, and incorporating iconography, you can enhance the accessibility of your app. Remember to test your app with users, including those with disabilities, to gather feedback and ensure a seamless user experience. By prioritizing accessibility in your code and design, you can create apps that are inclusive and cater to a diverse range of users.

About Author

Salih Kunduz

Co-founder of Accessible Android, he has been using Android since 2013. He closely follows social media and global accessibility developments and has worked with many Android developers to make their apps and services accessible.

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
    Roads Audio: Voice Threads https://accessibleandroid.com/app/roads-audio-voice-threads/
  • Untitled
    Infinix Zero 40: A Review from a Visually Impaired User’s Perspective https://accessibleandroid.com/infinix-zero-40-a-review-from-a-visually-impaired-users-perspective/
  • Untitled
    BookFusion Voice: Natural TTS https://accessibleandroid.com/app/bookfusion-voice-natural-tts/
  • Untitled
    Samsung Galaxy Tab S11 Review https://accessibleandroid.com/samsung-galaxy-tab-s11-review/