'Android layout resources for Accessibility vision impairment

Android applications currently support different layout resources based on orientation, screen size, day and night etc.

However, I would like to provide layouts targeted at users with vision impairments, for instance use layouts with YELLOW background and BLACK text.

Have I missed something that Android already supports?

Can I implement custom res/layout-WAI or res/layout-DDA folders?



Solution 1:[1]

Color (YELLOW background and BLACK text), fonts and font size is a topic for Styles and Themes.

Unless visually imapaired people need own layouts (arrangement, ordering of gui elements) you can implement a setting with a style chooser that can be applied to every layout

Solution 2:[2]

Instead of providing two different layouts, you can parametrize views in a single layout. Thus, views of your layout would take parameters (e.g. background color, text color) from the context theme they are inflated in.

So, this is what we want to achieve:

<android.support.constraint.ConstraintLayout
    android:background="?attr/bgColor"
    ... >

    <TextView
        android:textColor="?attr/textColor"
        ... />

</android.support.constraint.ConstraintLayout>

?attr/someAttribute would be taken from the theme of the current context.

Create attributes at attrs.xml in values/:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyAttrs">
        <attr name="bgColor" format="reference|color"/>
        <attr name="textColor" format="color"/>
    </declare-styleable>

</resources>

In styles.xml declaring two themes extending from a common theme:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
    </style>

    <style name="AppTheme.Default">
        <item name="bgColor">@color/red</item>
        <item name="textColor">@color/blue</item>
    </style>

    <style name="AppTheme.Accessibility">
        <item name="bgColor">@color/orange</item>
        <item name="textColor">@color/yellow</item>
    </style>

</resources>

Then, in your activity perform the assertion and set a correct theme:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    setTheme(isAccessibility ? R.style.AppTheme_Accessibility : R.style.AppTheme_Default);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ...
}

Or, if you have to do it on runtime, you may use ContextThemeWrapper to inflate specific view with appropriate theme.

Context wrapper = new ContextThemeWrapper(MyFragment.this.getContext(), R.style.AppTheme_Accessibility);
// inflating with a `wrapper`, not with the activity's theme
View themedView = View.inflate(wrapper, R.layout.some_layout, parent);

This is much better approach then providing two separate layouts, because it refrains you from maintaining two layouts when a change happens in UI.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 k3b
Solution 2