Android get string array from string

Getting a string array from strings.xml in xamarin Android

I have a string array in strings.xml that looks like this:

And I want to access it in one of my activities.

I’m currently doing the following:

But it’s not working.

2 Answers 2

I would do it like this :

String[] pages = getResources().getStringArray(R.array.helpPages);

The question asked about Xamarin.Android but the accepted answer syntax is for Java , so here is C# syntax (Xamarin way):

You can access to each item by using for or foreach . for ex.:

Not the answer you’re looking for? Browse other questions tagged android arrays xml string xamarin.android or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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.

Читайте также:  Андроид 11 что нового для самсунг а51

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 Essentials: Working with Strings and String Arrays

One poor coding practice that really gets our blood boiling is when developers include raw string content inline in code. Sure, there are occasionally good reasons to do this, especially when writing debug code, but generally speaking, if your Android application relies on string content that is displayed on the screen, this data belongs in the Android resources, not in the code itself.

There are a number of benefits to including string content as a resource, such as:

  • It centralizes the strings used by the application in a single location that is easily managed (by the developer or a non-developer).
  • Strings can be defined as a resource once, and used throughout the code. Therefore, it will have consistent spelling, case and punctuation.
  • Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).
  • Strings don’t clutter up your application code, leaving it clear and easy to maintain.

So let’s talk about how to add strings and more importantly, string array resources to your applications effectively.

Step 1: Define String Resources for Individual Strings

String resources belong in the /res/values/strings.xml file. Define a string resource entry for each string used by your application. Name your string resources appropriately. For example, the following XML excerpt defines a number of game character race types as individual strings:

Note the “race_” prefix of each string resource. This helps remind team members that these strings may be related somehow. For example, they might be used together an array or displayed as options in a Spinner control.

Step 2: Load Your String Resources Programmatically

You can easily load a string to use in your code using the getString() method of the Resources class. The following code can be dropped into your Activity class to load a String resource defined in your application:

The handy thing about loading resources this way is that if you’ve provided alternative resources in different languages, the appropriate language version of the string will be loaded based upon the device settings at runtime. No special code is needed for this to work.

Step 3: Define String Array Resources Using String Resource References

Now let’s say you wanted to create an array of those character race strings. Sure, you could load them all up individually in the Java code and make a String array in memory, but you would need to know each of the resource’s full id names and reference it in the code. Instead, your best bet is to create a string array resource that references the individual string resources you’ve already created, tying them together.

Читайте также:  Очистка рабочего стола андроид

String array resources are best stored in the /res/values/arrays.xml file. Define a string array with an item for each string used by your application. Name your string array resources appropriately. For example, the following XML excerpt defines an array that contains each of the game character race types defined in the strings.xml resource file in Step 1:

Step 4: Load Your String Array Resources Programmatically

You can easily load a string array to use in your code using the getStringArray() method of the Resources class. The following code can be dropped into your Activity class to load an array resource defined in your application:

Step 5: Referencing Array Resources in Simple Spinner Controls

One useful reason to go through all this work is to populate a control that requires a data adapter, the simplest of which is an array. If you are unfamiliar with using data adapters to populate Spinner controls and the like, check out the Android Developer website’s Hello Spinner tutorial. However, the method described in the linked tutorial is not as easily internationalized as what we are describing in this quick tut, since it defines the raw string contents in the string array resource, instead of as string references.

Using string array resources allows you to skip the steps of hooking up your data adapter to your control. For example, a simple Spinner control, or dropdown, can be populated with a string array resource without bothering with pesky adapters. You simply add a Spinner control to your layout file and set its android:entries attribute to the string array you created and you’re done. For example:

This defines a Spinner control that will show each of the character race strings as options, as shown in Figure 1.

In your Activity class, you can now implement an OnItemSelectedListener to capture when the user selects a specific character race, like this:

Here we simply react whenever an item in the Spinner is selected. We look up the data selected using the getItemAtPosition() method, which, in the case of a string array resource, is the string data itself.

Conclusion

There’s absolutely no reason to define strings and arrays of strings inline in code. Strings and string arrays belong in your Android application resources, not cluttering up your elegant Java. There are a number of benefits to storing string data in resource files, including better organization and easier internationalization. String arrays are even more powerful when they are comprised of string resource references instead of the raw string data. String array resources can be glued to many UI controls that generally rely upon data adapters without a lot of fuss, making your code even more readable and maintainable.

As always, we look forward to your feedback.

Источник

Referencing a string in a string array resource with xml

I have preferences where you can enable/disable what items will show up on the menu. There are 17 items. I made a string array in values/arrays.xml with titles for each of these 17 items.

Читайте также:  Зигард медникс программирование под android

I have preferences.xml which has the layout for my preferences file, and I would like to reference a single item from the string array to use as the title.

How can I do this?

In the Android developer reference, I see how I can reference a single string with XML, but not how I can reference a string from an array resource in XML.

5 Answers 5

In short: I don’t think you can, but there seems to be a workaround:.

If you take a look into the Android Resource here:

You see than under the array section (string array, at least), the «RESOURCE REFERENCE» (as you get from an XML) does not specify a way to address the individual items. You can even try in your XML to use «@array/yourarrayhere». I know that in design time you will get the first item. But that is of no practical use if you want to use, let’s say. the second, of course.

HOWEVER, there is a trick you can do. See here:

You can «cheat» (not really) the array definition by addressing independent strings INSIDE the definition of the array. For example, in your strings.xml:

By using this, you can use «@string/earth» and «@string/moon» normally in your «android:text» and «android:title» XML fields, and yet you won’t lose the ability to use the array definition for whatever purposes you intended in the first place.

Seems to work here on my Eclipse. Why don’t you try and tell us if it works? 🙂

Источник

Android getResources().getStringArray() variables

I have an xml file, where there are strings and string-arrays. I have a class where I click on a listitem, and in the other class I list other items. In the xml there are string which names are the clicked items modified name, and the values are the string-arrays name. I have found a solution how to add a variable to the getResources(9.getStringArray() but it doesn’t work. The program starts, but when I click any of the listitem my activity just stops working. My class file:

The TextView is there to check the variables. The albumSearch gives «modifiedartistpicked_code», it’s good. The getRes gives the value of from the xml («something_array»), the setRes gives it’s actual id number (which the getStringArray requires). The getRes2 gives the same as getRes it’s just there to check that it works fine. When I comment out the next two lines, the String albums[], and setListAdapter then the program works but it doesn’t list the items.

I hope I was able to write what I would like to do, and what is the problem 🙂

Update:

@Marc Bernstein: Because I don’t know it’s code1_array. First I made many if and when the picked item was x, then I read the R.array.x_array, etc.

But I have got what was the problem, there were capital letters int he name of the strings in xml. Thats why I hate xml, the problem is always there 🙂 This xml was just an example, the original is much greater that’s why no one was able to help. Next time I will be much more cautious.

And also I made it now more simplier because you have right it was too complicated.

Источник

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