Manikandan K Manikandan K - 1 month ago 16
Android Question

RTL support in addRule() method in Android

I need to support both English and Arabic language in my app. In one of my screens, I need to add rule for a widget programmatically. That is I need to align a view to the right of another view. For that, I have written the below code:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.RIGHT_OF, btnView.getId());
             txtView.setLayoutParams(params);


It works fine in English language, but doesnot work in Arabic language, how to fix it?

Answer

To support arabic language, you need to add another rule.

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                params.addRule(RelativeLayout.RIGHT_OF, btnView.getId());
                 txtView.setLayoutParams(params);
        params.addRule(RelativeLayout.END_OF, btnView.getId());

This will support both English and Arabic language. Hope it will help.

Comments