Android #8 - Managing and Accessing Resources in Android Application
Hello friends, welcome to the another tutorial of Programming Infinity. In this tutorial, we will know, how to manage and access resources in android application. We will know about the resources used in the hello world example here. The more resources will be known in the respective upcoming tutorials, where it is used. So friends, let's begin...
Resources
Resources means, the required components which is used by the android application for proper functioning of the application or for any other specific purpose.e.g :- The code, images, layouts etc. which is used by the application are various types of resources. In the above example, image is a resource, which is used by the app for some purpose, for e.g. showing it to the user. Layout is a resource, which is contains the UI of the android application, which is drawn in the activity at runtime. For any android application, code is an important part, because it contains all the function related to the apps like drawing UI, controlling widgets etc.
Managing Resources
Managing resources means arranging resources according to some common properties, and giving it some unique name, so that we can anytime access it easily. In android application, there are various types of resources such as .java source codes, xml files , images, texts etc., But here we know about the resources which is used for designing the UI of android application. All the other resources except this will be automatically known to us, when we know about the various widgets in the upcoming tutorials. The resources which we will study here is stored under a separate directory named res in the application folder, which is categorized into various sub-directory according to some specific property containing resources related to UI designing and xml values storing files . Now friends, let's know about the various resource sub-directory. We will here know only about the resources used in the hello world example. All the other resources will be known to us in the respective tutorial, where it is used.- layout/ :- It contains XML files that defines UI of the android application of various activities. It is accessed from R.layout class.
- values/ :- It contains simple values related to app, as, texts, colors, dimensions, integer and so on. In the hello world example, text, color, styles and dimensions resources are used for showing text, providing color, adding styles and providing dimensions respectively. These all are simple values, and hence, stored under the values directory. Here, simple means that, the resources which is stored here only contains a unique name and a value, which is accessed in the app through the unique name i.e. we can store values in this in single line of code and access it, whereas other resources such as images, layout files are complex, as it contains a lot of xml declaration for defining a single layout. In the hello world example, colors.xml is a xml resource, which contains colors declaration, dimens.xml is a resource which contains dimensions declaration i.e. width, height, padding etc, strings.xml is a resource, which contains text resource in it, styles.xml is a resource, which contains defining of various styles used in the app. These are respectively accessed with the help of R.color class, R.dimen class, R.string class and R.style class.
R :- The ''R" stands for a auto-generated file which contains unique id of all the apps resources, which is accessed with the help of R class. It transforms our defined id, or name, into dalvik executable resource. So that android will know about the app's resources used.
Accessing Resources
Accessing Resources means using the saved resources in the android application with its unique id or name. In the android application, we use .java source code files to use resources or, we can also use the resources in the xml files i.e. we can access resources in both, .java source code files and in the xml files, such as layouts, menus etc. For accessing resources in each of these, there is a certain way. We will know about it with the help of some examples.
- .java source code files accessing :-
TextView txt = findViewById(R.id.text_view);
txt.settext(R.string.helloworld);
txt.settext("Hello world");
In the above example, first line of code is used to initialize TextView as a component if UI from its unique id. We will know more about it in the next chapter. After initializing, "settext" method of TextView is called wHich is used to set text to the TextView. Here, the text "hello world" is accessed from the string with the help of "R" class. We can also direct add text to TextView as shown above, but it will show error for long texts. In the above way, text is accessed in the code.
- XML files accessing resources :-
<TextView
android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />
android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />
In the XML file, resource is accessed in the above mentioned way. We had to simple write the "@the resource/resource name" as the value of any attribute which requires resource. In the above given syntax, "the resource" means the resource you want to access, and "resource name" means that the name of the resource or id of the resource, that you want to use.
So friends, that's all for this tutorial. Hope u enjoyed it. If u have any question related to this tutorial, then ask me in comment box.
Thanks -
Programming Infinity ( Samridh Sharma )
Since the last few years, there have been many updates available in the ways designers, developers
ReplyDeleteBest technology blogs
Best technology in the world
Best technology to learn
android app development
programming tutorials
mobile application development
latest web development technologies
web design company