Friday, 13 January 2017

Android resources

Android Resources are managed in a folder named as res under the application package.In this folder we can manage our external or internal resources that are used by android system whenever these are required and resources can be accessed by class represented by R keyword.Resources can be layout files,strings,images,color,drawable files (used for designing of views or to store images ),icon images etc.This can be easily understood by the following image -

Some of the resources folders are not available default but can be created whenever is used like animation,raw etc.Android Resources folder are explained below -

1
anim/
XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class.
2
color/
XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class.
3
drawable/
Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/ and accessed from the R.drawable class.
4
layout/
XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class.
5
menu/
XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class.
6
raw/
Arbitrary files to save in their raw form. You need to call Resources.openRawResource() with the resource ID, which is R.raw.filename to open such raw files.
7
values/
XML files that contain simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory −
  • arrays.xml for resource arrays, and accessed from the R.array class.
  • integers.xml for resource integers, and accessed from the R.integer class.
  • bools.xml for resource boolean, and accessed from the R.bool class.
  • colors.xml for color values, and accessed from the R.color class.
  • dimens.xml for dimension values, and accessed from the R.dimen class.
  • strings.xml for string values, and accessed from the R.string class.
  • styles.xml for styles, and accessed from the R.style class.
8
xml/
Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time.
 You should always provide the alternative resources to support all devices configurations.There are five type of different sizes in android for which we can provide the resources by default , for other than these size we have to create the folder with specific dimensions.Here are five type of drawable folder and icon size as shown below-
 1.mdpi (48*48 )
 2. hdpi (72*72)
 3.xhdpi(96*96)
 4.xxhdpi(144*144)
 5.xxxhdpi (192*192)

We have to copy our images in to drawable folder to access them otherwise it will not be available to us.All strings to be defined in strings folder under the values,all the xml file for layout of activities and fragment must be defined under layout folder,all the menu that are being used in our application must be defined under the menu folder, all the launcher icons must be copied to mipmap folder and strings,colors,arrays etc must be created under values folder in respective xml files.

By default there is no folder in drawable to copy images to support different above screen sizes we have to create these folder under drawable folder like drawable-hdpi ,drawable-mdpi and same for the other screen sizes.If we need to define the images for the specific screen size then we have to name the folder as drawable-sw720dp then we need to copy all the images in to this folder, after copying the files in to this folder by providing the name of image android automatically fetch the image from the resources folder as shown below-


How to access the Android Resources

When we create the project in android studio then automatically a file is created where all the id's of resources ,views and view groups are stored this file is named as R.java. In this file all id's are stored in an hexadecimal format it is advised that you should never change anything in to this file otherwise you may not be able to generate the id again and it will give an error while compiling the code.In the image below it is shown that where you can locate the R.java file in android studio.


In the image below it is shown that how an id is stored in R.java file.



In below image it is explained that how you can provide the id to views you define in your android layout file.Now by using this id you can find your view in activity class.


Now in the image below it is shown that how you can find the views in an android activity by writing an view id.

In above image this is shown that we have some views that are of button type and whenever we call the the method findViewById(viewId) (it take one parameter that is id of view we need to find from the resource file) will return an object then we need to type cast the object with the appropriate class whether it is textview,imageview or anything else.

In below images it is shown that how you can create strings,color,styles etc. under the values folder.


Android Colors Resource-


Android Dimen Resource-


Android Strings Resource-


 Android Styles Resource-


In below image it is shown that where you need to copy your launcher icon for you android application and it is also shown that image for different sizes is also copied with the same name because with same name android system will automatically load the image with respective device screen density. This is the same case with drawable images you have to provide the same name for all images for every type of drawable i.e. mdpi,hdpi etc. difference will be only in image size then android system will automatically find the respective image from drawable folder and will show in the view.



No comments:

Post a Comment