Lesson Plan: Unit - 02
Subject: P15A2AAD - Android Application Development
Topic of Study: Working String and String Array Resources
Grade/Level: Master of Computer Applications
Objective: To understand the mechanism of String and String Array resources types for Android OS.
Time Allotment: 55 Minutes
- Working with String resources
- String resources might show text labels on form views and for help text.
- The application name is also stored as a string resource, by default.
- String resources are defined in XML under the /res/values project directory.
- Exa.
- You can edit the strings.xml file using the Resources tab, or you can edit the XML directly by clicking the file.
- Select the strings.xml tab.
- You can write a tag in .xml file
- How to do that.
- Step 1: Open any strings.xml file
- Step 2: type a tag
Hi - Step 3: Press CTRL + S
- Step 4: Repeat the Step 2 and 3 as per need.
- You can use open editor method.
How to do that.
- Step 1: Open any strings.xml file
- Step 2: Click on Open Editor.
- Step 3: Click on + sign to add new string resources.
- Step 4: Type sHello0 in key box and Hi in default value box.
- Step 5: Press OK
- Step 3: Press CTRL + S
- Step 4: Repeat the Step 3,4 and 5 as per need.
- After you save the file, the resources are automatically added to your R.java class file.
- R.java is a special file of AAPT which provide each resources with unique resources id and that id written in Hex code format, automatically.
- So, you can access each resources using R class.
- Write a code to access string in other .xml file.
- Exa. @string/sHello0
- Exa. @string/sHello1
- Exa. @string/sHello2
- Exa. @string/sHello3
- How to do that.
- Step 1: Open any Layout from layout folder.
- Step 2: Select Container > TextView and press enter key.
- Step 3: Select the TextView and Click on text properties.
- Step 4: type "@string/sHello0 or sHello1 or sHello2 or sHello3" and run your project.
- Write a code to access string in .java file…
- Exa. R.string.sHello0
- How to do that.
- Step 1: Open current MainActivity.java file from Java folder.
- Step 2: Declare required variables. Exa. String sName;
- Step 3: Write a code to access string array resource.
sName = getResources().getString(R.string.sHello0);
OR
sName = getResources().getString(R.string.sHello1);
OR
sName = getResources().getString(R.string.sHello2);
OR
sName = getResources().getString(R.string.sHello3);
- Step 4: Write a code to display string in Logcat window.
Log.e(tag:"sName",sName);
- Step 5: Run a program and See the output at Logcat window.
- Step 6: To see the output of Logcat, click on Edit Filter Configuration option from drop down at right hand side.
- Step 7: Type "sName" in Log Tag input box and press OK button.
- Step 8: See the output in Logcat Window.
- Working with String Array resources
- You can specify lists of strings in resource files.
- This can be a good way to store menu options and drop-down list values.
- String arrays are defined in XML under the /res/values project directory
- String arrays are appropriately tagged with the string-array tag and a number of item tags as child, one for each string in the array.
- String arrays store in /res/values/arrays.xml.
- To create a arrays file in Android Studio.
- Step 1: Right on Values > New >Value Resource file.
- Step 2: Type "arrays" and Click OK
- Step 3: Type following code in to file.
- Before.
- After:
- Write a code to access string array in other .xml file.
- Exa. @string/aFlavors
- How to do that.
- Step 1: Open any Layout from layout folder.
- Step 2: Select Container > Spinner and press enter key.
- Step 3: Select the Spinner and Click on entries properties.
- Step 4: type "@string/aFlavors" and run your project.
- Write a code to access string array in .java file…
- Exa.
- How to do that.
- Step 1: Open current MainActivity.java file from Java folder.
- Step 2: Declare required variables.
- Step 3: Write a code to access string array resource.
- Step 4: Write a code to display arrays item in Logcat window.
- Step 5: Run a program and See the output at Logcat window.
- Step 6: To see the output of logcat, click on Edit Filter Configuration option from drop down at right hand side.
- Step 7: Type "array" in Log Tag input box and press OK button.
- See the output.