14

I am developing an Android app to support both En/Ar. But I faced a problem that if the user changes from En to Ar the alignment of the user interface must turns from "left to right" to "right to left".

Example: (TextView)(EditText) this is in En

But in Ar it should be: (EditText)(TextView).

Is there a way to do this without creating two different layouts or two different versions?

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Nouf
  • 143
  • 1
  • 1
  • 6

4 Answers4

12

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).

Community
  • 1
  • 1
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
  • +1 for different layouts per locale. Also, if you are concerned about trying to maintain multiple versions of the same layout, consider researching Layout Including: http://developer.android.com/resources/articles/layout-tricks-merge.html – Jake Wilson Mar 13 '12 at 20:49
  • @Jakobud speaks total sense (slightly irritated that I forgot about that) - well played sir! – Graham Smith Mar 13 '12 at 20:58
  • 1
    Maintaining an entire project of layouts in duplicate is a nightmare! In even one of my small app's I'm using 20-30 layout files. For every bug or functional update using duplicate layout doubles the amount of work I have to do - this may be the best option but it's certainly not a good one. – Graeme May 28 '13 at 09:10
  • @Graeme So you downvoted the answer because you don't like the way Android deals with this? You could use includes like Jakobud describes. We have some major apps out there that are quite large and we don't suffer from what you describe. – Graham Smith May 29 '13 at 02:34
  • Downvoted because suggesting to duplicate everything is a bad idea -especially with suggestions of it being "easy". – Graeme May 29 '13 at 08:10
  • The question is How to achieve this without having two layout xml ? we all know to achieve with two layouts, as we may require to develop a app for multiple languages, tablets and mobile, we end up with many layouts assume we are developing app for 2 languages and for mobile and tablet we end up with 4 layouts for single view. – flexdroid Jul 03 '14 at 12:07
  • Refer the answer provided by @Tomasz Hadam This is working perfectly in my case. Thank you so much, appreciate... – Smeet May 13 '22 at 11:47
2

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

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • This should be an accepted as answer. Perfect. Using this approach we can save creating multiple layouts for different different layouts. – Smeet May 13 '22 at 11:46
0

I've tried to take some of the horror out of this by creating a set of custom LabeledView components (for Form components such as CheckBox, EditText and Spinners). These views (such as LabledEditText) inflates a region-specific layout and implements the bare minimum of calls in order to act as a facade (EditText in this case).

res/layout/component_labeled_view.xml - [TextView][EditText]
res/layout-ar/component_labeled_view.xml - [EditText][TextView]

The View class itself contains:

public LabeledEditText(Context context, AttributeSet attrs) {
    super(context, attrs);  
    LayoutInflater inflator = LayoutInflater.from(context);

    mContainer      = inflator.inflate(R.layout.component_labeled_edittext, null);      
    mValueView      = (EditText) mContainer.findViewById(R.id.editText);
    mLabelTextView  = (TextView) mContainer.findViewById(R.id.textView);

    align(attrs);
}

public Editable getText() { 
    return ((EditText)mValueView).getText();
}

public void setText(String text) {  
    ((EditText)mValueView).setText(text);
}   

public void addTextChangedListener(TextWatcher watcher) {   
    ((EditText)mValueView).addTextChangedListener(watcher);
}

This should at least cut down any duplication as you should now be able to reference the LabeledEditText view rather than an TextView and EditText combo.

In my implementation which I haven't fully shown I've gone a step further and created an abstract LabeledView class which uses some custom attributes allowing me to customise both label and value in specific situations (see the align(attrs); line of code). Depending on how big your app is you might want to try something similar?

Graeme
  • 25,714
  • 24
  • 124
  • 186
-4

Why not just make the TextView above the EditText?

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • Where possible it might be necessary to design your layout to be left/right independent in order to avoid having to duplicate - this seems like a pretty reasonable answer. – Graeme May 28 '13 at 09:11