Understanding XML Files in Android

Nidhi Kumari
3 min readAug 4, 2020

--

“Understanding XML Files in Android”- After reading the blog title, most of you must have understood what I am going to talk about in this blog. The XML files, and more specifically how can we modify these files to enhance the accessibility features of our applications. So, let’s start with it.

What is XML?

XML files store data in plain text format wherXML stands for eXtensible Markup Language. Some other features of XML files are:

  • XML is a markup language much like HTML
  • XML was designed to store and transport data
  • XML was designed to be self-descriptive

How do we write XML Files?

Using Android’s XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements.

Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you’ve defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout. For example, here’s an XML layout that uses a vertical LinearLayout to hold a TextView and a Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>

After you’ve declared your layout in XML, save the file with the .xml extension, in your Android project's res/layout/ directory, so it will properly compile.

Targeting accessibility in Android Applications using XML

As, I have mentioned that here I will be talking about the accessibility in android, let’s start with it.

1.Content Labeling:

Screen Readers like Talk Back use a content description to pronounce the features. So, one needs to add contentDescription to make their code more understandable by Screen Readers.

For components that do not add too much value to accessibility, or exist purely for decoration, you can also set the contentDescription="@null" which won’t hurt the navigation experience for switch access users.

2. Grouping Content:

Sometimes the auditory feedback TalkBack gives for visual elements in an app may not reflect their logical and spatial structure. Even though elements may be ordered in a sensible way visually, they may be spoken out of order.

3. Touch Target Size:

As a rule of thumb, all touchable components should be at least 48dp in height by 48dp in width so users with dexterity issues can navigate the application easier. You can achieve this in several ways:

  • Global android:minWidth and android:minWidth styling for all separate touchable components using your application theme. Or, apply individual styles to your views in xml.
  • Apply padding to your components to make the touch area larger.
  • Implement a TouchDelegate to expand the touch area to be larger than the bounds of the view.

4. Custom Views:

Sometimes adding contentDescription attributes and grouping related UI elements in the same container — have not required you to define the announcements that an AccessibilityService makes. This is mostly because default Android UI components have accessibility “baked in” — that is to say, there is useful metadata in the code for buttons, switches, checkboxes, etc. that tells a service like TalkBack how to speak these components out loud.

When you create your own custom views instead of using the default ones in the framework, the accessibility problems quickly become more difficult and the solutions become non-trivial.

This is my take on android accessibility and XML files. Thanks for reading and hope you liked it. Do let me know in the comments if you have any suggestions and you can also reach out to me here.

Resources:

https://codelabs.developers.google.com/codelabs/basic-android-accessibility/

--

--

Nidhi Kumari

GSoC'20 & GSoC’19 @DIAL_community | Intern @Ushahidi | Open Source enthusiast | JS lover ❤️ |