Android gets the string. xml Value

Source: Internet
Author: User

Why do we need to store the text in the application separately in the string. xml file?

I. For internationalization, when internationalization is required, you only need to provide another string. xml file, change all the man information in it to the corresponding language (such as English), and then runProgramThe Android operating system automatically selects the corresponding string. xml file based on the language environment and country of the user's mobile phone. Then, the mobile phone interface displays English. This is very convenient for internationalization.

2. To reduce the application volume and data redundancy. Assume that "we have been working hard" is used in the application for 1000 times. If you write these words directly at each usage, the program will have 70000 words, these 70000 words account for KB of space. Due to limited mobile phone resources, the CPU processing capability and memory are very limited, and KB is a small space for mobile phone memory, we must remember to "save memory when saving memory" when using mobile apps ". If you define these words in string. in XML, this text is referenced by the resources class in each use, and only occupies 14B. Therefore, it is very effective to reduce the application volume. of course, we may not use so much text during development. However, as mobile application developers, we must develop good programming habits.

There are several differences in getting the values in the string. xml file.

1. in XML files such as androidmanifest. xml and layout:

Android: text = "@ string/resource_name"

2. In activity:

Method 1: This. getstring (R. String. resource_name );

Method 2: getresources (). getstring (R. String. resource_name );

3. In other java files (context or pplication is required)

Method 1: context. getstring (R. String. resource_name );

Method 2: application. getstring (R. String. resource_name );

Use of the string. xml file in Android

1. Obtain the string and value in string. XML in the program.

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<String name = "hello"> Hello world, mainactivity! </String>

<String name = "app_name"> testexample01 </string>

</Resources>

Use in activity:

String appname = (string) This. getresources (). gettext (R. String. app_name );

Or:

String appname = (string) This. getresources (). getstring (R. String. app_name );

2. Define a String Array (arrays. XML)

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<String-array name = "Sports">

<Item> football </item>

<Item> basketball </item>

<Item> tai chi </item>

<Item> ice hockey </item>

</String-array>

</Resources>

---- Getresources (). getstringarray (R. String. Sports );

3. Define the color (colors. XML)

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<Color name = "black"> # ffffff </color>

</Resources>

--- Getresources (). getdrawable (R. String. Black );

--- Getresources (). getcolor (R. String. Black );

4. Define dimensions (dimens. XML)

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<Dimen name = "height"> 80dip </dimen>

</Resources>

--- Getresource (). getdimension (R. String. Height );

5. Define a style (styles. XML)

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<Style name = "sharptext">

<Item name = "Android: textsize"> 18sp </item>

<Item name = "Android: textcolor" >#000000 </item>

</Style>

</Resources>

Assets folder Resource Access

All files in the assets folder are in the original file format. You need to use assetmanager to read files in the form of byte streams.

1. Call getassets () in the activity to obtain the assetmanager reference.

2. Use assetmanager's open (string filename, int accessmode) method to specify the file to be read and the access mode to obtain the input stream inputstream.

3. Use inputstream of the open file to read the file. After reading the file, remember inputstream. Close ().

4. Call assetmanager. Close () to close assetmanager.

Note that files from resources and assets can only be read but cannot be written.

Read from raw files as follows:

Public String getfromraw (){

Try {

Inputstreamreader inputreader = new inputstreamreader (getresources (). openrawresource (R. Raw. test1 ));

Bufferedreader bufreader = new bufferedreader (inputreader );

String line = "";

String result = "";

While (line = bufreader. Readline ())! = NULL)

Result + = line;

Return result;

} Catch (exception e ){

E. printstacktrace ();

}

}

Read from assets directly

Public String getfromassets (string filename ){

Try {

Inputstreamreader inputreader = new inputstreamreader (getresources (). getassets (). Open (filename ));

Bufferedreader bufreader = new bufferedreader (inputreader );

String line = "";

String result = "";

While (line = bufreader. Readline ())! = NULL)

Result + = line;

Return result;

} Catch (exception e ){

E. printstacktrace ();

}

}

Of course, if you want to get the memory stream, you can also directly return the memory stream!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.