Android is ignoring /res/values-large/strings.xml - android

I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?

values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.

Related

Hiding views declaratively based on screen size in Android

In android xml:ish
Is there any way to change a visibility attribute based on the layout size/orientation in the xml directly?
I have a button in a fragment that should be visible for small screens sizes only. On larger sizes, let's say layout-large, I want it to be hidden.
Sure, I can write code for this without any problem but for academic reasons I would like to know it it's possible to do something like this.
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="<magic expression here>" />
Thanks
// Johan
This answer is based off the explanation provided here by Flávio Faria.
The visible, gone, etc can be values mapped to their corresponding enum values in a string resource - which means you can create a visibilty.xml with string resources for each layout you want, and Android will automatically resolve the one you want for each screen type.
I'd recommend the following:
/res/values/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Enum values pulled from Android source -->
<string name="visibility_visible">0</string>
<string name="visibility_invisible">1</string>
<string name="visibility_gone">2</string>
<string name="product_info_footer_button_visibility">#string/visibility_visible</string>
</resources>
/res/values-large/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="product_info_footer_button_visibility">#string/visibility_invisible</string>
</resources>
And then you can reference the visibility as follows for your button:
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#string/product_info_footer_button_visibility" />
Warning: This depends on the device having the same enum values for visibility (0/1/2) as defined in the AOSP source. Device manufacturers and custom ROM creators can change these values, in which case this code will likely not work as desired.
The android:visibility attribute is an int (like many attributes) so you can do the following :
Define a resource file named visibility.xml in values-port and values-land resource directories. The content of this file is like this :
values-port/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">0</integer> <!-- 0 is the value for android:visible -->
</resources>
values-land/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">1</integer> <!-- 1 is the value for android:invisible -->
</resources>
and in your layout.xml :
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#integer/buttonvisibility" />
It works : btn_check_availability is visible in portrait and invisible in landscape.
Note : this example use layout orientation as discriminator, but you can of course do it with any resource qualifier (like dimension, density, ...)
There is no magic expressions available in XML. If only.
There are two approaches to this problem:
a/ use the drawable folder system. Drawable folders can be copied and named to be DPI aware following the conventions dictated here: Supporting Multiple Screens.
b/ Do it programmatically. On runtime check for screen DPI and show/hide view accordingly.
Have you looked at using includes and multiple layouts organized into the appropriate size/orientation layout folders? Some layouts could either simply not have the button or have it hidden by default.
Re-using Layouts with include
Providing Alternative Resources

Different screensize and language in android

I want to support different screen sizes and languages at the same time. So I got some folders for my layout like
layout (German)
layout-en (English)
layout-fr (French)
etc...
and I got layout-large for big screens. So when I open another language in the large screen I got the layout from the small screen. Is there a way to manage this? layout-large-fr or layout-fr-large did not work...
And of course I defined translated strings.
Try this one:
Create on layout file for each language. Create on layout.xml in res/values-xx with this content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="main_layout" type="layout">#layout/activity_main_xx</item>
</resources>
The result should looks like this:

Load different colors based on conditions

I am programming an application contains layouts with some views. I load these colors from values/colors.xml. Now, I want to define multiple themes for my application, e.g. Blue and Green (some sort of blue and green colors). My question is how can I define two colors.xml file and load it based on some conditions or choosing by user. What I want is some thing like strings.xml that we can load strings based on locale defined.
Thanks in advance.
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="white">#FFFFFF</string>
</resources>
values-fr/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="white">#EEEEEE</string>
</resources>
button.setBackgroundColor(Integer.parseInt(getString(R.string.white)));

How to add localization setting in my app

Is there a way to add localization setting in my app? I'm going to add a preference/setting in my app and there is a localization option. So the user can change the language I have provided from the values string.
I was googling around but found nothing. Wondering you guys can help me, and give example or link to the tutorial.
You don't need to provide a setting for that, Android will do that for you. You just have to provide the translations for the languages you want to support.
You can do this for French for example by creating a folder named res/values-fr in your resources folder and putting your translations in there.
so in the res/values folder you would have a strings.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Map</string>
</resources>
and in the res/values-fr you woud have another string.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Carte</string>
</resources>
Your app will use the translation that best matches the language that the user has selected on their device.

Android XML include another xml (non layout)

I have a settings.xml file that I use to set various global variables within my app. I want to be able to create sub xml files that I include in the settings.xml file because depending on the client, I change settings in this file so I want to make it easier on myself.
I know that there is a way to include layout files this way but I can't find any documentation that shows how to do this with plain XML on Android.
Any help would be much appreciated.
I think all you have to do is make an additional file which includes the settings you want.
Ie, I have strings.xml in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="main_title">Hello there</string>
</resources>
And I can create additionalstrings.xml in the values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="additional">How are you?</string>
</resources>
I can refer to either R.string.main_title or R.string.additional, without having to say the file it came from.

Categories

Resources