Load localized strings at runtime

Here is a possible solution if you want to load localized text resources on a TextView in Android.


You may need to extend your app flexibility to be able to load localized texts at runtime from a web server for example. That would be to be able to add new localizations (for new countries) without having to release a new update.


One possible solution would be to:
1. Set a sort of naming conventions for your localized resources(text) like naming the key to something like: en_my_text, ro_my_text, de_my_text…
2. Load the text resources from the webserver and save those into a SharedPreferences file using the naming convention from above
3. Create a custom TextView and a custom attribute for the text view that will be used to set in XML the key for your localized text
4. Use the value from the custom attribute to load the translation on the TextView when Android loads your TextView

Simple sample

The xml attribute:


You can save that under res -> values -> attributes.xml

The TextView custom class:

I uploaded the source project here for you.



As an exercise and also improvement, you could create a new attribute called language that could store the language prefix in strings. This way you can remove the string you added in the xml and replace it with just the key for the resource.
After that you could fetch the value of that in your custom TextView and append the language prefix attribute value to the key attribute value to form the final key that you use to load the resource from the SharedPreferences.

This way you have to write only one string resource under the strings.xml file.








Android State list generator

Inspired by a post from stackoverflow, I made a simple XML State List generator :), you simply need to fill the inputs with your corresponding drawable names and then press Generate button. For more details regarding android StateList XML please check the official documentation.
Android state list generator version 2 is here:

 New in android state list generator version 2:
* added all supported android states
* added checkboxes to choose custom states combination
* added description to all states and grouped the states by Android API level


For all API levels:
Pressed: Focused: Selected: Checkable:
This item should be used when the object is pressed (such as when a button is touched/clicked) This item should be used when the object has input focus (such as when the user selects a text input) This item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad) This item should be used when the object is checkable; “false” if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
Checked: Enabled: Window Focused:
This item should be used when the object is checked This item should be used when the object is enabled (capable of receiving touch/click events) This item should be used when the application window has focus (the application is in the foreground)
For API level 11+:
This item should be used when the object is activated as the persistent selection (such as to “highlight” the previously selected list item in a persistent navigation view)
For API level 14+:
This item should be used when the object is being hovered by a cursor


Please input drawable names below (just the drawable names, without extension like .png):


Android PreferenceActivity & SharedPreferences tutorial

I will start with an example. Let’s assume that we want the user to change the date format of your application. For this you must have a class that extends PreferenceActivity, but first you have to create an XML file like this:
1. Make a directory in res called “xml”
2. In the xml directory create a xml FILE called “preferences.xml “put your code for creating the preferences. In our example it looks like this:

3.  In res -> values  directory create a xml called “array.xml”. Here you will put the items which the the user can select. In our example the code looks like this:

4. Create an activity which extends PreferenceActivity. In this class you will put the layout created at step 2, “preferences.xml” and you will get the name of the item that the user selects to show in the main list just below the title of each preference. The code looks like this:

5. And now if you want to get the data format selected by user into a String to use it in other activities you can do this by using SharedPreferences. Here is an example:

How to make a background button selector in XML?

Step 1: Create an XML in the drawable folder and name it selected_item.xml (or any another name you want but to be suggestive). The code in this xml will be:

The color=”#80000000″ is transparent black.
Step 2: Create an XML again, in the drawable folder an name it not_selected.xml. The code in this xml will be:

The color=”#80ffffff” is transparent white.
Step 3 Create an XML again 🙂 in the drawable folder and name it button_selector.xml. The code in this xml will be:

Step 4 In the xml file where you make the button put this: