Tuesday, July 5, 2016

Resources

“Resources“ is a public class in Android which is used to store static content such as strings, colors, styles, bitmap and more.
NOTE: Static content are the files that don't change based on user input.
This is how “Resources” is organized in Android Studio.


Organisation of Resources, Learn Android Development, 2TechUp, Vishal Malik


Now I will explain some of directories in “Resources” Class one by one but first I want to tell you that when Android application is compiled, a R class gets generated, which contains resource IDs for all the resource available in your res/ directory and we will use this R class to access or file in resource using sub-directory.

res/drawable

  • Image file like .png, .jpg, .gif, or XML file that are compiled into bitmaps, Layer List, State List, Level List etc. are stored on this file.
  • You can get your drawable by using getDrawable(int) method in JAVA Class and access it from the R.drawable.filename class so to get 2techup.xml file from Drawable we can use getDrawable(R.drawable.2techup);

res/layout

  • It is used to store XML file that define user interface layout.
  • You can access your layout in JAVA Class from the R.layout.filename class

res/values

It is used to store colors, strings, styles etc.

Some of files you can create in res/values directory

colors.xml
  • We can use this for storing color values.
  • To get color from colors.xml file we can use getResources().getColor(R.color.idname); method.
This is how you can store color’s in this file

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<item name="green" type="color">#FF99CC00</item>
   
<item name="blue" type="color">#FF33B5E5</item>
   
<item name="purple" type="color">#FFAA66CC</item>
 
    
<integer-array name="androidcolors">
       
<item>@color/blue</item>
       
<item>@color/purple</item>
   
</integer-array>

</resources>

strings.xml
  • This can be used for storing string or string array.
  • To get String from strings.xml file we can use getString(R.array.string_name); method.
  • To get String array we can use getResources().getStringArray(R.array.string_array_name); method
This is how you can store Strings or String Array in strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<string name="text_1">text_string_1</string>
   
<string name="text_2">text_string_2</string>
 
    
<string-array name="text_array">
       
<item>text_1</item>
       
<item>text_2</item>
   
</stringr-array>

</resources>