Plain string resources can be declared in res/values/strings.xml
If you create an Android application [for example call it HelloAndroid] and just before adding anything, browse to res/values/strings.xml it will be like this:
This is a pretty example of Plain text resources. The resource with name=app_name is the name of the application that appears when you deploy the application to the phone, its referenced in the AndroidManifest.xml file in the application tab. You can change it as you want.
Hello World, HelloAndroid!
HelloAndroid
Now lets add two entries in the file to see how can we use plain text resources within the application
The first string will be the text of a text view defined in res/layout/main.xml fileThis is referenced from the res/layout/main.xml This is referenced from the code
See that to reference the first string we use the @string/[Resource Name] convention.
The second resource will be referenced from the code file of the activity like this
TextView txtHeader2=(TextView)findViewById(R.id.txtHeader2);if you open R.java file of your project you will find that Android has generated a class called string with members referencing to your string resources:
txtHeader2.setText(getString(R.string.plainResource2));
public static final class string {Also notice the way you access the string resources in Android, you dont open the strings.xml file and parse it t extract the values you want to reference- instead you access them through the R.string class defined in R.Java and Android does the rest for you.
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int plainResource1=0x7f040002;
public static final int plainResource2=0x7f040003;
}
Also another interesting feature is that you can define your own resources file, go to res/values/ directory, right click>New>File and call the file for example CustomStrings
You will see something like this:
I will add a resource with the name CustomString and value this is a custom string and reference them from the layout like this
Or from the code like this:
TextView txtHeader3=(TextView)findViewById(R.id.txtHeader3);
txtHeader3.setText(getString(R.string.CustomString));
String format Resources:
the dalvik vm offers string formats whih provide placeholders representing data to be replaced at runtime by variables
an example of a string format resource is:
the %1$s is the place holder that would be replaced by a variable string.This is resource for %1$s
and from your code you can use it like this:
//String format resourceand the text view will have a text equal to This is resource for Giving an example of string format resource.
TextView txtHeader4=(TextView)findViewById(R.id.txtHeader4);
String strFormat=getString(R.string.StringFormat);
String header=String.format(strFormat, "Giving an example of string format resource");
txtHeader4.setText(header);
Styled text Resources:
You can use string resources styled with these three HTML tags:
<b>,<u> and <i>
You can define the a string as follows:
This is an <u>example</u> of <i>Styled</i> <b>Resources</b>
And use it from the code as this:
TextView txtHeader5=(TextView)findViewById(R.id.txtHeader5);
txtHeader5.setText(R.string.StyledResource);
notice that we use setText() method by calling the string resource directly.
if we use the getString() method it will display the string without styling
we can use the HTML styled text also by using spanned.
Spanned textSpan = android.text.Html.fromHtml(htmlTaggedString);Summary
//Set it in a text view
textView.setText(textSpan);
Android provides three types of string resources: Plain strings, String formats which provide place holders to be replaced by variables in runtime and Styled text resources which provide styling with three standard HTML tags
Download a demo application here
0 comments:
Post a Comment