1

I'm practicing my Java and I'm trying to make something like simple text game. Now there are two main characters and each one have their own pop up text. I've put the text in string.xml with their own names like "dialog1" "dialog2" "dialog3" and I want to retrieve the strings with R.string.dialog* but I want to make some function to change numbers something like "dialog" + int

How to make something like this??

I made it like if you tap on screeny the its displayed "dialog1" massage when you tap second time the other character should display next part "dialog2". Now I don't want to do it manually r.string.dialog1 then r.string.dialog2 I want to make function of this but I don't know how to pass value "dialog" + int.

PocchiSan
  • 19
  • 4
  • Not sure what you try to do but maybe `getResources().getString()` method can help you with achieving what you need. – noname Aug 15 '19 at 15:22
  • There's probably some slightly more efficient method to do what you're attempting, ultimately, but to do specifically what you're asking, you can use the `Resources#getIdentifier()` method to get an `int` ID for a given string resource name, and then use that ID with `getString()` to get the actual value. There are several examples in the linked duplicates. – Mike M. Aug 15 '19 at 15:33
  • @Jorgesys Yeah, but notice where they say "I don't want to do it manually r.string.dialog1 then r.string.dialog2". That's basically what your answer is doing, just roundabout. I linked duplicates to do what they specifically ask in the question, though if they prefer the array of `R.string`s, there are duplicates for that, too. – Mike M. Aug 15 '19 at 16:02

3 Answers3

2

You can put a parameter in the xml as explained in this post Are parameters in strings.xml possible?

You need to add %1$d wherever you want in the string resource so when you want to get the string you send the parameter.

<string name="dialog1"> This is %1$d</string>

And when you want to get it

getString(R.string.dialog1, "1")

If you want more parameters you change the parameter number

<string name="parameters"> Many parameters %1$d %2$d %3$d</string>

and then

getString(R.string.parameters, "1", "2", "3")
Lenin
  • 500
  • 3
  • 11
0

If you want a method to load the values stored in strings.xml, you have to create an array of ints containing the ids of the resources.

Example:

Create messages into strings.xml:

<resources>
    ...
    ...
    <string name="dialog1">My first message</string>
    <string name="dialog2">My second message</string>
    <string name="dialog3">My third message</string>
</resources>

Then use this method to load the values according the index of the element defined into the array:

int[] messageValues = new int[] {R.string.dialog1,R.string.dialog2,R.string.dialog3};

private String getDialogMessage(Context context, int dialogIndex){
    if(dialogIndex >= messageValues.length) //Check if element exist in array
        return "";

    return  context.getResources().getString(messageValues[dialogIndex]);

}

and these are examples of how to used the method:

        System.out.println(getDialogMessage(getApplicationContext() , 0)); //get first element message.
        System.out.println(getDialogMessage(getApplicationContext() , 1)); //get second element message.
        System.out.println(getDialogMessage(getApplicationContext() , 2)); //get third element message.
        System.out.println(getDialogMessage(getApplicationContext() , 14)); //returns "" , because the element doesnt exist into the array..
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

If you need to format your strings, then you can do so by putting your format arguments in the string resource, as demonstrated by the following example resource.

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. Then, format the string by calling getString(int, Object...). For example:

String text = getString(R.string.welcome_messages, username, mailCount);

Quoted from: Android Documentation