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

Android Emulator

A tool for creating Android virtual devices (including software and virtual hardware) on your personal desktop is known as Emulator. We can use these virtual devices as the target device...

4 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.

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.

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 Styles and Themes

Android styles allow you to design the appearance (for example, colors, size, and fonts) of Android UI components from XML resource files. With this method, you only need to set...

5 minutes read.

Event Handling in Android

Events are a convenient way to gather data about user interactions with interactive elements of your application. Like a button click or a screen touch. A queue of events on...

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.

Android Navigation Drawer

A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed...

5 minutes read.

Android GridView

Theview group that displays items in a two-dimensional scrolling grid is called GridView. The data isadded into this grid layout from anArray List or database. The adapter class isutilized to...

4 minutes read.

Android Activity

What is an Activity? Android Activity is a screen in the user interface of an Android application. In this way, Android activity is very similar to the desktop application window. An...

4 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.

Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of...

4 minutes read.

How to show image on an android application

ImageView Widget in Android What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets   Explanation: Image View widget is used...

2 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.

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.

Android Tutorial

This Android tutorial will provide you with all the concepts that will be beneficial for both beginners and advanced-level students or candidates. You can learn this tutorial to begin your...

6 minutes read.

ImageView Widget in Android

What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets Explanation: Image View widget is used to insert the image...

2 minutes read.

Android RecyclerView

RecyclerView is a ViewGroup brought to AndroidStudio as successor to ListView and GridView. These are all improvements and are included in the latest v7 support package. It was written to...

8 minutes read.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

8 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.