3

Please see my previous question here for the full code.

In case I want to internationalize my app, I need to create string objects for some statements in my project. The one bothering me most is this code block.

    ...
    else
        Toast.makeText(MainActivity.this, 
            "Good job! The answer was " + comp + ".\n" +
            "You made " + guesses + " guesses.\n" +
            "Restart the app to try again.", 
            Toast.LENGTH_LONG).show();
    }
};

The relative part to this in the strings.xml looks like:

<string name="correct">Good job! The answer was + comp\n
               You made + guesses + guesses.\n
               Restart the app to try again.</string>

I want the comp and guesses variables to show their respective values, but I don't know how.

I plan to do this to the code block

    ...
    else
        Toast.makeText(MainActivity.this, 
            R.string.correct, 
            Toast.LENGTH_LONG).show();
    }
};

Thank you.

2
  • This has been answered elsewhere. For e.g. see this: stackoverflow.com/a/3656409/415412 Commented Jan 9, 2014 at 17:54
  • Thanks Suchintya. Didn't realize that, but it's an awesome resource. Commented Jan 9, 2014 at 17:57

5 Answers 5

14

In your xml file string.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="correct">Good job! The answer was %1$s\n
               You made %2$s guesses.\n
               Restart the app to try again.</string>
</resources>

If you want to put an other value in the text, you increment %2$s -> %3$s , etc ...

Then you can use it like in java like that :

Toast.makeText(MainActivity.this, getString(R.string.correct, comp, guesses), 
            Toast.LENGTH_LONG).show();
Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely the best way to handle this, and I did not know about that getString method - I've been using String.format(Locale, String, Object...). Nice!
1

Store a format string in the resource and use String.format to replace the comp and guesses placeholders. Use the appropriate places holders for each and include the variables as arguments to format(). You can find details of what placeholders to use under the Formatter class.

The resource would then be something like:

<string name="correct">Good job! The answer was %s \n
           You made %d guesses.\n
           Restart the app to try again.</string>

And the code would be:

...
else
    String correctFormat = getString(R.string.correct);
    Toast.makeText(MainActivity.this, 
        String.format(correctFormat, comp, guesses),
        Toast.LENGTH_LONG).show();
}

};

Comments

0

you would use

context.getResources().getString(r.id.correct);

then create a values folder for each language you want

Comments

0

As @Andros said,

In your string.xml

<string name="correct">Good job! The answer was %1$s\n
           You made %2$s guesses.\n
           Restart the app to try again.</string>

In your code,

Resources res = getResources();
Toast.makeText(MainActivity.this, String.format(res.getString(R.string.correct),comp, guesses),Toast.LENGTH_LONG).show();

2 Comments

You don't need to get the Resource because getString() is directly available from a context. And you dont have to use String.format() because with getString(int, Object ...), you can do it directly.
Yeah, you are right. It's just a generalized answer. It works even for, say nested classes too.
0

In string.xml

<string name="correct" formatted="false">Good job! The answer was %s\n
               You made %s guesses.\n
               Restart the app to try again.</string>

In java :

Toast.makeText(MainActivity.this,String.format(context.getString(R.string.correct),comp,guesses), Toast.LENGTH_LONG).show();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.