Develop an Android App to support English and Arabic "layout alignment"

Android 4.2 introduced native support for right to left layout.

  • In AndroidManifest set the android:supportsRtl="true"
  • In layout properties use the start/end properties instead of left/right.

Here is the video with detailed explanation: https://youtu.be/plW1qSGDSzs


Why can't this be done with two layouts (you never said why this is not desireable)? As described by the Android Developers documentation

A large part of localizing an application is providing alternative text for different languages. In some cases you will also provide alternative graphics, sounds, layouts, and other locale-specific resources.

An application can specify many res// directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination.

Ref: http://developer.android.com/guide/topics/resources/localization.html

So in your case create res/layout-ar then copy your existing layout into this folder and then simply swap them round. Simple, follows best practices and is easy to do. This also makes any further localisation changes easier going forward without having to write more code.

If you were to write code you are going to need to find the default language of the device and then swap the Views based on this. You can get the language by:

Locale.getDefault().getDisplayLanguage();

See this question for more detail: Get the current language in device

On a final personal note: I think the former is a much better separation of concerns, as the code provides logic and the XML layouts actually control the layout (with Android selecting the right resources automagically for you without having to write any more code).