Resource Management

In this tutorial, we will see how to managing resources like strings,colors,dimens,styles…etc. If you want to be an android developer seriously, you must be good at resource management. When you do that properly, you can change the whole theme of application with little resource changes. On the other hand, if you don’t care about enough, you may face with too much cost of change.

  1. How to Use Resources Properly
  2. Why We Need to Use Resource Files
  3. Device Density Matters For Some Resources
  4. Localization to Make Application Multilingual

1. How to Use Resources Properly

The key solution is simple. Do not hard code any resource. Define the resource in relevant file and reference it wherever you want.

Lets start with strings.

Open your strings.xml file located in res/values folder. I assume that your strings.xml is like below.

Put a string item

Open a layout file, and write below

Result is below. See string referencing with android:text="@string/a_text"

Screen Shot 2015-10-15 at 14.30.38

Lets continue with colors

Check your res/values folder and if a colors.xml file is not there, define one.

Screen Shot 2015-10-15 at 14.36.37    Screen Shot 2015-10-15 at 14.37.20

Open your colors.xml and put a color item.

Open previous layout file and set button background with this color you defined

See referencing from colors.xml with android:background="@color/a_color". Result is below.

Screen Shot 2015-10-15 at 14.54.18

How about dimensions? Dimension resource file carries dimension values (with a unit of measure). You can keep your margin-padding amounts or font sizes or something like that in dimens.xml.

Open your dimens.xml located in res/values folder and put two dimen items with names ‘a_padding_for_content’ and ‘a_fontsize_for_content_text’.

Open your layout file and write below code

So our result is below

Screen Shot 2015-10-15 at 15.08.15

Pay attention,root RelativeLayout use android_padding="@dimen/a_padding_for_content" and textview’s all properties are referenced from resource files located in res/values folder.

2. Why We Need to Use Resource Files

Lets assume that we have three different pages.




Of course you need to define three different layout xml file for each page. Each layout xml have common features like bar height, content padding and background color of content’s container.

Assume that you hard code these features for each layout like below




When you want to change one of these you will open all files which have that feature and change it one by one and believe me, if you work on a big project , tons of changing will wait for you, not just three.

So define your resource in relevant resource file, do not duplicate. Do you want to change later ? Find it easily and deal with it just one time. 

3. Device Density Matters For Some Resources

Before, we have talked about density term. Every device has a density and density types can be grouped as mdpi-hdpi-xhdpi-xxhdpi-xxxhdpi.

I will explain this with an example. Switch your project hiearachy to ‘Project’. Follow app->src->main->res. You can see drawable folder under res folder. Drawable folder contains your images,xml-drawables which are used by application.

Screen Shot 2015-10-15 at 16.28.36             Screen Shot 2015-10-16 at 10.31.05

 Assume that you put an image and use that image in application. If the image size is not enough for high density device, it looks pixel blurry. So we need other versions of the image in right sizes. Different sized image for each density type.

If you have drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxhdpi,drawable-xxxhdpi folders ignore this step. If you have not, click right on res folder and create drawable folders for density types like below. Below pics show to create just drawable-mdpi so you need create others too.

Screen Shot 2015-10-16 at 10.41.28Screen Shot 2015-10-16 at 10.42.22

So when you create all you res folder looks like below

Screen Shot 2015-10-16 at 10.50.51

Pick an image for xxxhdpi (highest denstiy) like below (Image resolution depends on using for how big area)


Resize it for other densities. You can learn ratio relations with below chart. So this orange android image is selected for xxxhdpi and its size is 256×256, xxhdpi must be 192×192… and so on. You can find resizer tools for android on web.


When you complete all density, put images in right folder like below

Screen Shot 2015-10-16 at 11.31.14

Now when you use this image anywhere in application, android will select the best version depends on device’s density and provide the best experience to user.

Note (from

If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density

The “default” resources are those that are not tagged with a configuration qualifier. For example, the resources in drawable/ are the default drawable resources.

You don’t have to resize all images for all densities. I mean you may put an image drawable/ folder and want system scales that image up or down.

 4. Localization To Make Application Multilingual

You may want your application multilingual. For example, you created an application and you want the application language is german when device’s language is german but for any other device language, you want it as english.

Lets create a folder under res folder for specific language, in this case for german.

Screen Shot 2015-10-16 at 10.41.28

Name will be values-de. You can search ‘Android Language / Locale List’ on web to reach other language codes.

Screen Shot 2015-10-16 at 15.02.56

Create a strings.xml file in values-de folder

Screen Shot 2015-10-16 at 15.05.49

Screen Shot 2015-10-16 at 15.07.46

So we have two strings.xml files right now. One located in values-de for a specific language which is german. The other located in values folder for all language (default) except german.

Open two strings.xml files and add a string item named ‘application_language’ for each like below.

Screen Shot 2015-10-16 at 15.27.13

When you run application on a device which uses german, system select strings from values-de/strings.xml. If a string can not be found in values-de/strings.xml then system take it from default strings.xml automatically. All devices different from german use default strings.xml.

Thats all. See you next tutorial.

Bir Cevap Yazın