Android layout resources for better vision

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

However, I would like to provide layouts for visually impaired users, such as using layouts with a YELLOW background and BLACK text.

Am I missing what Android already supports?

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

+3


source to share


3 answers


You cannot create custom qualifiers. The currently supported qualifiers are listed here .

I suggest the following workaround ( Example ):



  • Create a custom WAI layout for each existing layout with the same name but suffix "_wai"

    example_layout.xml
    example_layout_wai.xml
    
          

  • Create a method to resolve the appropriate layout based on system needs. Let's say we have an isWAI () method, the solution method would look something like this:

    public int resolveLayoutResourceID(int layoutResID) {
        int newLayoutResID = layoutResID;
        if (isWAI()) {
            String layoutResName = getResources().getResourceEntryName(layoutResID);
            String newLayoutResName = layoutResName + "_wai";
            newLayoutResID = getResources().getIdentifier(newLayoutResName, "layout", getPackageName());
        }
        return newLayoutResID;
    }
    
          

  • Create a class BaseActivity

    for all your classes (or use a static utility function) that override the method setContentView

    . There you will add logic to select the layout.

    @Override
    public void setContentView(int layoutResID) {
        int newLayoutResID = resolveLayoutResourceID(layoutResID)
        super.setContentView(newLayoutResID);
    }
    
          

+1


source


Color (YELLOW background and BLACK text), fonts and font size - Theme for Styles and Themes .



If visually imapaired people don't need their own layouts (layout, ordering of gui elements), you can implement a style- select setting that can be applied to each layout

+1


source


Instead of providing two different layouts, you can parameterize views in the same layout. This way your layout views will take parameters (e.g. background color, text color) from the contextual theme they are pumped into.

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

will be taken from the topic of the current context.

Create attributes in 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 the styles.xml

announcement of two topics, ranging from the general topic:

<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, do the assertion and set the correct topic:

@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 need to do it at runtime, you can use ContextThemeWrapper

bloat for a specific kind with a matching theme.

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

      

This is a much better approach that provides two separate layouts, as it prevents you from maintaining two layouts when a change occurs in the UI.

+1


source







All Articles