Strings xml file android

how to read value from string.xml in android?

I have written the line:

to get string value, but instead of returning string, it is giving me id of type integer. How can I get its string value? I mentioned the string value in the string.xml file.

18 Answers 18

UPDATE

You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string.

If not in activity but have access to context:

By the way, it is also possible to create string arrays in the strings.xml like so:

And then from your Activity you can get the reference like so:

Only for future references.

You can use either getString(int) or getText(int) to retrieve a string. getText(int) will >retain any rich text styling applied to the string.

In fragments, you can use

If you want to add the string value to a button for example, simple use

The defined text in strings.xml looks like this:

You must reference Context name before using getResources() in Android.

You can use this code:

Basically, you need to pass the resource id as a parameter to the getText() method.

If you are in an activity you can use

If you are not in an Activity use this :

Details

  • Android Studio 3.1.4
  • Kotlin version: 1.2.60
  • single line use
  • minimum code
  • use suggestions from the compiler

Step 1. Application()

Get link to the context of you application

Step 2. Add int extension

Usage

Results

while u write R . you are referring to the R.java class created by eclipse, use getResources().getString() and pass the id of the resource from which you are trying to read inside the getString() method.

Example : String[] yourStringArray = getResources().getStringArray(R.array.Your_array);

You can read directly the value defined into strings.xml:

and set into a variable:

but we can define the string into the view:

I hope this code is beneficial

Update

  • You can use getString(R.string.some_string_id) in both Activity or Fragment .
  • You can use Context.getString(R.string.some_string_id) where you don’t have direct access to getString() method. Like Dialog .
Читайте также:  Объединить звонки андроид как

Problem is where you don’t have Context access, like a method in your Util class.

Assume below method without Context.

Now you will pass Context as a parameter in this method and use getString().

What i do is

What? It is very simple to use anywhere in your app!

So here is a Bonus unique solution by which you can access resources from anywhere like Util class .

Источник

Android strings.xml — things to remember

This article is about such trivial android thing as — strings.xml

Do not reuse

Do not reuse strings for different screens.

1. Imagine you have a loading dialog on Sign In and Sign Up screens. Both have loading dialog and you decided to use the same string — R.string.loading.

Later if you decide to use a different one, you will have to create two new strings and do modification in java code. If you used two strings from the beginning you would have to modify only strings.xml file.

2. You never know which language your application may support. In one language — you will be able to use the same word for different context, but in another — you will have to use different words for different context.

Note that English version of strings.xml uses the same word — “Yes” for both R.string.download_file_yes and R.string.terms_of_use_yes strings.

But Ukrainian version of strings.xml uses two different words — “Гаразд” for R.string.download_file_yes and “Так” for R.string.terms_of_use_yes.

Separate

Separate strings which belong to the same screen via prefix and comment

Creating separate strings.xml file per screen

If you want you can create strings.xml file per screen — settings-strings.xml, profile-strings.xml. But usually, apps have 10–20 screens. So having 10–20 strings.xml files in every language folder, in my opinion, would bring huge mess.

Format

Use Resources#getString(int id, Object… formatArgs) to format strings

Never do string concatenation via a + operator, because in other languages words order may vary.

Источник

How to edit multiline strings in Android strings.xml file?

I have several cases where my string in strings.xml is quite long and has multiple lines done with \n.

Editing however is quite annoying since it is a long line in Eclipse.

Читайте также:  Как почистить андроид если память заполнена

Is there a better way to edit this so it looks like it will be presented later in the textview, ie the line breaks as line breaks and the text in multiline edit mode?

4 Answers 4

1. Use the Source, Luke

XML allows literal newline characters in strings:

You just have to edit the XML code instead of using the nifty Eclipse GUI

2. Use actual text files

Everything inside the assets directory is available as a input stream from the application code.

You can access those file input streams of assets with AssetManager.open() , a AssetManager instance with Resources.getAssets() , and… you know what, here’s the Java-typical enormously verbose code for such a simple task:

Sure, you could put newlines into the XML, but that won’t give you line breaks. In strings.xml, as in all XML files, Newlines in string content are converted to spaces. Therefore, the declaration

will be rendered as

in a TextView. Fortunately, there’s a simple way to have newlines in the source and in the output — use \n for your intended output newlines, and escape the real newlines in the source. The above declaration becomes

which is rendered as

For anyone looking for a working solution that allows the XML String content to have multiple lines for maintainability and render those multiple lines in TextView outputs, simply put a \n at the beginning of the new line. not at the end of the previous line. As already mentioned, one or more new lines in the XML resource content is converted to one single empty space. Leading, trailing and multiple empty spaces are ignored. The idea is to put that empty space at the end of the previous line and put the \n at the beginning of the next line of content. Here is an XML String example:

This is rendered in the Text View as:

You may easily use «» and write any word even from other languages with out error:

Источник

Error in strings.xml file in Android

I have declared a long string in string.xml of an application.

Declared like this

But this gives the following error :

9 Answers 9

post your complete string. Though, my guess is there is an apostrophe (‘) character in your string. replace it with (\’) and it will fix the issue. for example,

Solution

Apostrophes in the strings.xml should be written as

Читайте также:  Text edit для android

Example

In my case I had an error with this string in my strings.xml and I fixed it.

Here you see my app builds properly with that code.

Here is the actual string in my app.

Escaping apostrophes and quotes

If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don’t work:

You have to put \ before an apostrophe. Like this \’ , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.

Use this regex (? for searching an unescaped apostrophe. It finds an apostrophe that not preceded by a backslash.

Источник

get list of the strings present in strings.xml file in Android

I want the list of strings present in the strings.xml file.

Does anyone knows how to get it. One thing I found is it assigns the ids in sequential order inside R.java but how to get the starting id is not clear.

For Example I have 100 Strings in my strings.xml like below and I want to read in at a time not like giving getResources().getString(int id) for individual.

5 Answers 5

You can declare your strings in res\values\strings.xml file like this.

In your activity class, you can access them like the following.

In the above list, whatever sequence you declare, the same way it is assigned to the array in your activity. Suppose Cars will be assigned to categories[0]. Hope this helps.

You will get all codes of strings with its values in «str» variable.

If you want to access all the Strings from the strings.xml file you could use reflection on the R.string class. An example can be found in this answer, you’ll just need to replace drawables with strings.

You could declare an integer array with an entry for each string. I did this for an array of colors once, so I imagine it works for strings as well.

res/values/arrays.xml

Then in your code, you would loop over the array and use each value as the argument for getString().

Источник

Оцените статью