Android Studio Resources

To build an outstanding Android application there are many items that you need to use. Aside from the coding of your application, you need to handle various resources such as bitmaps, menus, colors, layout definitions, animations, etc. static content used by your code.

These resources are managed in various subdirectories under the project's res/ directory.

Organizing Resources in Android Studio

Android Studio Resources

1) drawable/

Image files like .png, .jpg, .gif or bitmaps, shapes, animation drawable derived from XML files. These are storedin res/drawable/ and accessed from the R.drawable class.

2) layout/

XML files that define the layout of the activities. These are stored in res/layout/ and accessed from the R.layout class.

3) values/

XML files containing values like strings, integers, colors, etc.The following table shows the file naming conventions for resources that can be stored inthe values directory:

ResourceFile Name
arraysarrays.xml
color valuescolors.xml
stylesstyles.xml
String valuesstrings.xml

4) mipmap/

Drawable files for various launcher icon densities are stored in res/mipmap.

5) anim/

XML files defining property animations. These are stored in the res/anim/ folder and can be accessed using R.anim class.

6) color/

XML files that define a list of color states. These are stored in res/color/ andcan be accessed using R.color class.

7) menu/

XML files that define application menusincluding options menus, context menus, and submenus. These are stored in res/menu/ and can be accessed using R.menu class.

8) raw/

Any file you save in raw format. To open these raw files, you need to call Resources.openRawResource() with the resource ID (R.raw.filename).

Alternative Resources

Applications you develop must provide alternative resources to support different types of device configurations. To make your application work properly on any configuration you should include alternative drawable resources and string resources. At run time, Android detects the configuration of the device and loads the resources with respect to the device’s configuration for the application.

Follow these steps to assign configuration-specific resources:

  • Create a new sub-directory in res/ with name as <resources_name><config_qualifier>. In this, resources_name will be either of the resources such as values, layouts, mipmap, etc. Qualifiers specify the individual configurations that use these resources. A list of full modifiers for various types of resources can be found in the official documentation.
  • Save each alternate resource in this new directory. The name of the resource filesmust be the same as the basicresource files, but these files would requireconfiguration-specific alternative content. For example, an image file with the same name but a higher resolution screen will have a higher resolution.

Below is an example of specifying a default language layout and an alternative Korean language layout.

MyFirstProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java   
res/
         drawable/  
            icon.png
            background.png
         layout/  
            activity_main.xml
            info.xml
layout-kr/
            main.xml
         values/  
            strings.xml 

Accessing Resources

You need access to the resources defined in your code or layout XML file during application development. The following describes how to access resources in both scenarios:

A. In Code

When an Android application is compiled, an R class is created. This contains the res IDs of all resources present in the res/ directory. You can use R classes to access resources using subdirectories and resource names, or directly using resource IDs.

Example 1

To access res/drawable/icon.jpg and set an ImageView with image resource you will use the following code:

In this, an object of Image View has been created using R.id.img1 to get ImageView definedwith idimg1 in a layout file. Then to set the image resource in Image View, R.drawable.icon is used to get the icon image available in the drawable sub-directory.

ImageViewimageView= (ImageView) findViewById(R.id.img1);
imageView.setImageResource (R.drawable.icon);

Example 2

Let’s take an example where res/values/strings.xml has following string resources:

<resources>
<string name="app_name">My Application</string>
<string name="main_activity">Main Activity</string>
</resources>

Now to set the text on a TextView object with ID name using a resource ID you will use the following code:

TextViewmessageText= (TextView) findViewById(R.id.text1);
messageText.setText(R.string.app_name);

Example 3

Let’s take a layout res/layout/activity_main.xml as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/textView"
android:layout_width="match_content"
android:layout_height="match_content"
android:text="@string/main_activity"
android:textSize="18sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/img1"
android:layout_width="150dp"
android:layout_height="175dp"
android:layout_gravity="top|center_horizontal"
android:layout_margin="10dp"
android:paddingBottom="30dp"
android:src="@mipmap/ic_launcher_profile" />


</RelativeLayout>

This following java code will be usedto load the above layout for an Activity:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

B. In XML

Let’s take the following resource XML res/values/colors.xml file that includes following color resource:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
</resources>

Now to use this resource in the layout file to set the text color you will use “@color/purple_200”:

<?xml version="1.0" encoding="utf-8"?>
<EditTextxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/purple_200"/>

Related Topics

Assets folder in Android

You might have noticed that unlike the Eclipse ADT (App Development Tools), the Android Studio does not have an Assets folder, which is mainly used to store the web resources...

3 minutes read.

Jetpack Architecture Components in Android

A collection of software elements, libraries, tools, and instructions is called Android Jetpack. It is available to assist in creating reliable Android applications. Jetpack, which was introduced by Google in...

8 minutes read.

Difference between android developer and web

From a small business to a big firm, every organization in the world of technology requires a digital market presence and only mobile applications and websites/web applications provide for digital...

4 minutes read.

Android EditText

In Android, EditText is a UI widget utilized to allow the user to input or change the text. While using the EditTextwidget in your Android application, you need to use...

4 minutes read.

Android Studio Resources

To build an outstanding Android application there are many items that you need to use. Aside from the coding of your application, you need to handle various resources such as...

5 minutes read.

Android ListView

A kind of AdapterView that displays a vertical list of scrollable views, each of which is laid out below each other is called ListView. Using adapters, items are inserted into...

4 minutes read.

Android Studio's Layout Editor

The Layout Editor allows you to efficiently create layouts by dragging UI elements from the Palette into the visual design editor instead of manually creating the layout XML file by...

8 minutes read.

Android Project Folder Structure

JetBrains community developed Android Studio, the official IDE (Integrated Development Environment) for Android app development and is freely distributed by Google. After the complete Android Architecture setup, we can construct an...

4 minutes read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

Android Time picker example

How to pick Time from the user in android? Write an android example to pick Time from the user and display the date to the user. Android provides controls for the user...

3 minutes read.

How to reduce the size of APK in Android

What exactly is an APK file? The full form of APK is Android Package Kit. It is the Android operating system's application file format. We use Android Studio to generate APK...

7 minutes read.

How to Update Gradle in Android Studio?

The Android Gradle plugin adds a number of features that are unique to develop Android apps to the Gradle build system, which is the foundation of the Android Studio development...

2 minutes read.

Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date....

3 minutes read.

Button Widget in Android Tutorial

Explanation: Button is a widget under Buttons section which is used to execute specific code. Example: XML file: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">     <Button         android:id="@+id/button"         android:layout_width="wrap_content"    ...

1 minute read.

Top 7 Android App Development Books

Do you want to create Android apps but don't know where to start? One should first be familiar with the basics of Android before we discuss these books. With the...

5 minutes read.

How to insert an email in android application in android version 8

Email Address Insertion It is used to insert email adrress from the user in the android application.The only point of difference between an EditText and an Emailtext is that EmailText consisits...

1 minute read.

How to Generate QRCode in Android?

Many apps use QR codes to show data in a way that is machine-readable. Data is represented using these codes in a secure manner such that only computers, not humans,...

4 minutes read.

Different Ways to Create aar File in Android Studio

A file called an Android archive file (*.aar) can be created using Android Studio and comprises classes and methods that use related files and classes for Android. The Android library...

4 minutes read.

How to insert the mobile number on an android application using EditText

Number Insertion in android application: Sometimes while dealing with numbers, we don't want to insert any string inside an number so, we premiliary define an EditText so we externally specify...

2 minutes read.

How to crop Image from Camera and Gallery in Android?

In this tutorial, we'll learn how to crop photos in the Android Gallery and Camera. There is no crop feature in this project, contrary to what was covered in the...

5 minutes read.