Alternate resources in Android are used to provide different versions of resources so that an application can work properly on different devices. Android devices have different screen sizes, languages, orientations, and hardware features. Alternate resources allow the app to automatically choose the most suitable resource based on the device configuration.
In Android, resources include files such as layouts, images, strings, and styles that are stored inside the res folder of the project. Instead of writing separate code for each device type, developers can create different versions of these resources. The Android system automatically selects the correct resource at runtime.
One common use of alternate resources is language support. If an application needs to support multiple languages, different string files can be created for each language. For example, a folder named values-en can contain English text, while values-fr can contain French text. When the user’s device language is French, Android automatically loads the French resources.
Another use of alternate resources is screen size support. Android devices come with different screen sizes such as small, normal, large, and extra-large. Developers can create different layout folders such as layout-small, layout-large, or layout-sw600dp. Android will load the layout that best fits the device’s screen size.
Alternate resources are also useful for screen orientation. A device can be used in portrait mode or landscape mode. Developers can create a layout folder for portrait (layout) and another folder called layout-land for landscape orientation. When the user rotates the device, Android automatically switches to the appropriate layout.
Another example of alternate resources is screen density support. Different devices have different pixel densities such as ldpi, mdpi, hdpi, xhdpi, and xxhdpi. Developers can store images in folders like drawable-mdpi, drawable-hdpi, or drawable-xhdpi. Android selects the image that best matches the screen density to ensure good image quality.
Alternate resources help developers create applications that look good and work correctly on many types of Android devices. They reduce the need for complex coding and allow Android to automatically manage different device configurations.
In simple terms, alternate resources allow one Android application to adapt to different devices, languages, screen sizes, and orientations without changing the program code.
Here is a simple example of Alternate Resources in Android showing how Android loads different resources automatically based on language and screen orientation.
Example 1: Language Alternate Resource
Step 1: Default Strings (English)
Create a file: res/values/strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="welcome_msg">Welcome to the App</string>
</resources>
Step 2: French Language Strings
Create a folder values-fr and add: res/values-fr/strings.xml
<resources>
<string name="app_name">Mon Application</string>
<string name="welcome_msg">Bienvenue dans l'application</string>
</resources>
Step 3: Use the String in Layout
res/layout/activity_main.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_msg"
android:textSize="20sp"/>
When the device language is English, the app shows: Welcome to the App
When the device language is French, the app automatically shows: Bienvenue dans l’application
No Java code change is required.
Example 2: Orientation Alternate Resource
Step 1: Portrait Layout
res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Portrait Layout"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 2: Landscape Layout
Create folder: res/layout-land/
File: res/layout-land/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Landscape Layout"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Now when the user rotates the device, Android automatically switches between:
Portrait layout → layout/activity_main.xml
Landscape layout → layout-land/activity_main.xml
Example 3: Screen Density Alternate Resource
Images can be placed in different folders:
res/drawable-mdpi/logo.png
res/drawable-hdpi/logo.png
res/drawable-xhdpi/logo.png
In layout:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"/>
Android automatically selects the correct image based on the device screen density.
