0

I'm developing a multilingual app and need to show a different layout for a specific region

My res structure is as follows

res/
    layout/
        questions.xml
    layout-ar-rLY/
        questions.xml

When the locale is English (en) the default layout is shown. When the locale is Libyan the Libyan (ar_LY) layout is shown. However, when the locale is Arabic (ar) the Libyan layout is still shown. I need it to show the default layout.

I know that I can simply create a layout-ar directory and copy the questions.xml layout there, however, I am wondering if there is a more elegant way to achieve the right result, so that I don't have to maintain both files.

res/
    layout/
        questions.xml
    layout-ar/
        questions.xml
    layout-ar-rLY/
        questions.xml

The best solution I have come up with so far is for the default and the Arabic layouts both to include a shared sub-layout. Is there a better way to achieve the same result?

QuantumTiger
  • 970
  • 1
  • 10
  • 22
  • Sounds like you're looking for *alias resources*. It's briefly explained [here in the Android docs](http://developer.android.com/intl/es/guide/topics/resources/providing-resources.html#AliasResources), but for a slightly better example have a look at [this related SO answer](http://stackoverflow.com/a/14103951/1029225). – MH. Nov 24 '15 at 13:51

1 Answers1

0

Thanks to MH's comment above I had a look at the Android documentation on the official way to do this using Aliases which suggest this.

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include layout="@layout/main_ltr"/>
</merge>

Unfortunately the method given in the documentation doesn't actually work (as detailed in this StackOverflow) answer. Using a merge tag as a root element gives an InflateException error.

I'm sure that the second suggested fix contained in the StackOverflow thread should work; however, the concept of placing Layouts into the resources seems to me to be just as flawed as creating multiple copies (ie from a maintenance perspective you have to remember they are there and not in the layouts folders where you'd expect them to be) so in the end I compromised on the following:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
  <include layout="@layout/questions_default"/>
</FrameLayout>

Which is a bit more verbose than just a merge tag, but at least it works.

Community
  • 1
  • 1
QuantumTiger
  • 970
  • 1
  • 10
  • 22