Supporting Different Languages and Layouts in an Android Application

Introduction

Android phones, or to say it more precisely, Android applications, are currently becoming a life support for youngsters, not only youngsters but each and every class of people, whether they be an engineer, doctor, teacher and so on. Applications are developing with the latest and very rapid technologies that brings smiles to everyone's face. Our point of view is not life support rather we will focus on the various languages and multiple screen support in an application.

Not only different language support but also multiple screen resolutions and size support as well. So let us explore these different aspects of Android development in this article.

Supporting multiple languages

The successful development of our application and distribution as well we must develop an application in such a way that our application must support multiple languages, in other words Spanish, French, German, Hindi and so on. This rovides a robust experience to our application and contributes to their popularity in the world.

Some important points that must be remembered during development are:

  • It is good to keep string resources in an external file and outside of code. Android has by default an explicit resource directory in each project.

  • There are several directories, like res/values/string.xml where we integrates our string resources.   
Create local directories and string files

To add support for multiple languages there is a directory called values. If it is not present then create one inside the res directory. Let us consider a default case. For example, values-es/ is the directory containing simple resources for the Locales with the language code "es". The Android runtime maps the appropriate resources at runtime from the device local settings.

For example: values-es/string.xml for French, values-es for Spanish.

Let us look at the code change of string files. 

English the default locale/ values/strings.xml

  1.  <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="title">My Application</string>  
  4.     <string name="hello_world">Hello World!</string>  
  5. </resources>  
For Spanish
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="title">Mi Aplicación</string>  
  4.     <string name="hello_world">Hola Mundo!</string>  
  5. </resources>  
For French
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="title">Mon Application</string>  
  4.     <string name="hello_world">Bonjour le monde !</string>  
  5. </resources>  
Using the String Resources

In the preceding we defined the attributes and now it's time to map the references for the defined attributes in the XML and Java file as well.

Referring the string resource syntax : R.string.<string_name>

For example: Get a string resource from the application's Resources.
  1. String hello = getResources().getString(R.string.hello_world);  
a resource to the method that needs a string.
  1. TextView textView = new TextView(this);  
  2. textView.setText(R.string.hello_world);  
In other XML files we can refer to a string resource with the attribute using the following code:
  1. <TextView  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:text="@string/hello_world" />  
Supporting Multiple Screens

As said in the introduction of this article, depending on the requirements, a user buys phones and tablets rendered with multiple variations in the screen sizes. So how does it manage to provide the same look and feel to these big screens as it does with the lower screen resolution of phones. It is the duty of the programmer or developer to take care of these things and issues like resolution, graphics and screen sizes.
  • There are four generalized sizes that we consider in Android: small, normal, large and xlarge.

  • There are the four generalized densities low (ldpi), medium (mdpi), high (hdpi) and extra high (xhdpi).
The declaration of the layouts and bitmaps for various devices or screens must place these resources in separate directories, the same as we do above for strings. The programmer must also be aware of the orientation of screens, in other words portrait or landscape, that is also considered a variation of screen sizes.

Create Different Layouts

To optimize the user experience on the various screens sizes, whether there is a phone of a small screen size or there is an eight inche tablet PC. So create the layout accordingly for each layout and create a different directory name with each screen size.

For example: MyfirstApp project/res/layout/main.xml and layout_large.xml

Referencing the Layout in an application
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  
  5. }  
So the system loads the appropriate layout file from the appropriate directory based on the screen sizes of the phone and tablet as well. We can provide the special layout for landscapes for large screens and portrait as well and vice versa also. Let us see how as given below.
  • Default (portrait) : MyfirstApp  project/res/layout/main.xml 

  • Landscape : Layout-land/main.xml

  • Large (portrait) : Layout-large/main.xml

  • Large landscapes : layout-large-land/main.xml
As we saw above, it is the developer's duty to provide the width and height, more specifically the layout but from version 3.2 and above new methods for defining these attributes were introduced.

Bitmaps Creation

For supporting various screens of various devices in respect of layout resources we must provide the bitmap of the image like low density, high density and so on. Have a look as given below.
  • xhdpi: 2.0

  • hdpi: 1.5

  • mdpi: 1.0 (baseline)

  • ldpi: 0.75
Logically it means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi and 75x75 for ldpi devices.

It can be described in the drawable resource directory as shown below.
  • For extra large (xhdpi) : MyfirstApp  project/res/ drawable-xhdpi/CsharpCorner.png

  • For large screens (hdpi) :  drawable-hdpi/CsharpCorner.png

  • For medium screens (mdpi) :  drawable-mdpi/CsharpCorner.png

  • For small screens (ldpi) : drawable-ldpi/CsharpCorner.png       
Specifying the Minimum and Target API Levels

In Android Studio it is a little bit different. Instead of the manifest file you must go directly to the Gradle file where you will find the relevant data. You can modify it as needed and/or wanted. However In any other IDE such as Eclipse you will find these settings in AndroidManifest.xml and select the minimum and target APIs.
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >  
  2.     <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="21" />  
  3.         ...  
  4. </manifest>  
Check Your System Version at Runtime
  1. private void setUpActionBar() {  
  2.     // Make sure we're running onAPI level 11 or higher  
  3.     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
  4.     ActionBar actionBar = getActionBar();  
  5.     actionBar.setDisplayHomeAsUpEnabled(true);  
  6.     }  
  7. }    
Summary

This article explained the multiple language and multiple screen support with their basic structure and how they are defined, referenced and much more. Mainly for the popularity and user convenience an application must support various languages and the same for multiple screens.

Up Next
    Ebook Download
    View all
    Learn
    View all