Close
Topic : Introduction to Android Values resources:



Value Resources-Android : infobrother

Values Resources:

Values Resources Provides us a text strings, Integers and color for our application with Optional styling and formatting. In Above Picture, we have one file Named String.xml located in Values Directory.

A string Resources provides text strings for Our application with Optional text styling and formatting, there are three type of resources that can provide our application with strings.


  • » String: An XML resource that provides a single string.
    » String Array: An XML resource that provides an Array of strings.
    » Quantity Strings: An XML resource that carries different strings for Pluralization.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <Button
            android:text="@string/Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button1" />
</LinearLayout>


In Above Example, The string for the Button was loaded from the resources by providing the resource ID for the String As shown below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">HelloWorld</string>
  <string name="Button">Click ME:</string>
</resources>


NOTE: Consider the following tutorial "Values resources" to learn more about Values resources, and how we use it in our application.


  • There are two ways to Access a resource:
    1 » Using Code: Using a Static integer from a Sub-class of our resource class, such as:

    Resource.string.Button;
    

    Where String is the resource type and Button is the resource name.

    2 » Using XML: Using a Special XML syntax that also corresponds to the resource ID define in our Resource Class, such as:

    @string/Button
    

    Where string is the resource type and Button is the resource name.



NOTE:We Need to set Resource name according to Normal XML rules, and the name must be valid Android Resource ID. No spaces or dashes allowed.


In this tutorial, we Learn about Values resources. Here we just introduced Values resources. In our upcoming tutorials, we will learn more and will use it while writing our applications.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement