Issue
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
You can't create custom configuration qualifiers. The current supported qualifiers are listed here.
I will suggest the following workaround (Example):
Create a special layout for WAI for each existing layout, with the same name, but with the suffix "_wai"
example_layout.xml example_layout_wai.xml
Create a method to resolve the appropriate layout based on system needs. Say we have a method isWAI(), resolve method will look something like:
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
BaseActivity
class for all your classes (or use a utility static function), that will override thesetContentView
method. There you will add a logic to select the layout.@Override public void setContentView(int layoutResID) { int newLayoutResID = resolveLayoutResourceID(layoutResID) super.setContentView(newLayoutResID); }
Answered By - Eyal Biran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.