Customizing the ActionBar Title and Content Description in Android Apps

In one of the recent Android apps on which I was working, our team had the app audited for Accessibility. (Side note: If you’ve never given a second thought to Accessibility in your app, it’s a great learning experience, albeit a humbling one as well.) One of the items that came out of the audit was to use more descriptive contentDescription attributes for elements like buttons and form fields, as well as for ActionBar items. In particular, for non-home Activities that had up navigation, there was a request to customize the contentDescription attribute on the up button, in an effort to make it more descriptive and meaningful to the user. While this did not prove to be a difficult task thanks to some helpful code on setting the contentDescription found on StackOverflow, I ran into a unique issue trying to set it while also customizing the ActionBar title. In short, in order to properly set a custom contentDescription and ActionBar title, you must set the ActionBar title first, and then set the contentDescription for its up button. Otherwise, Android will disregard the just-set contentDescription value and use the default value gleaned from the ActionBar title.

Sample Code

How about some sample code? Here’s some helpful methods you can use in your Android apps to set the ActionBar title and the contentDescription for its up button. But remember, set the ActionBar title first.

public void setActionBarTitle(CharSequence value) {
  getActionBar().setTitle(value);
}
public void setHomeButtonContentDescription(CharSequence value) {
  ((View) getWindow().getDecorView().findViewById(android.R.id.home).getParent().getParent()).setContentDescription(value);
}

Until next time, happy (and accessible) coding!

There are no comments yet, add one below.

Leave a Comment