8

I am new to android programming and wanna know this:

I have 3 strings defined in the strings.xml file: <resources> <string name="one">First Click </string> <string name="two">Second Click </string> <string name="three">Third Click </string> <resources>

and a text view which displays the first string in the strings.xml file. i don't want to use settext("******") to change the text of text view when the user clicks on a button. how can i make the textview switch to the text already defined in the strings.xml file, say from

First Click to Second Click

2
  • 2
    you can't change text of TEXTVIEW (dynamic) without settext("***") so you have to do that.... Commented Nov 12, 2012 at 5:48
  • 1
    How will the TextView know the Button was clicked? How will the Button know which TextView is holding the string? Commented Nov 12, 2012 at 6:25

5 Answers 5

16

Use setText(getResources().getString(R.string.one));

2
  • .thank you very much....but pls in a case where there are a lot of strings specified in the strings.xml file, is there a way a variable can be used to to determine the string to show....for instance setText(getResources().getString(R.string.VARIABLE)); or something like that...so that it can show different texts..
    – thunderson
    Commented Nov 12, 2012 at 6:07
  • I can't understand you, what do you want. Please post a small example here. If you satisfy for my answer, then please check the tick mark on my answer. Commented Nov 12, 2012 at 7:05
4

Get the resources of the application, and then get a string with the ID you are looking for.

getResources().getString(R.string.one);
2
  • pls...can the id be a variable....if not...how do i use a variable to determine which string to print out?
    – thunderson
    Commented Nov 12, 2012 at 6:11
  • The ID is just an integer that references a point in memory specified by the auto-compiled R file. You could use an integer variable, but chances are the point in memory will change, and your strings will get all mixed up. For example, R.string.one may be 0x7f06000b one run, and 0x7c0af0cb the next. You could always use a switch(var){case:} statement.
    – Matt Clark
    Commented Nov 12, 2012 at 6:55
4

To determine which string you use with a variable you will have to use a switch, as below

switch(anyInt) {
    case 1://if the int == 1, then the textview will be set to this
        tv.setText(getResources().getString(R.string.one);
        break;
    case 2://if the into == 2 then the TV will be set to this
        tv.setText(getResources().getString(R.string.two
        break;
    default:
        tv.setText("into does not have value 1-2")
}

Add as many of these statements as you need, I believe it is very memory efficient even if you have a lot of statements

2

I Think you need this if i am wrong then get back to me.

Try this.

getResources().getString(R.string.app_name);

You just simply read the resource of your application .You can use any resource of your application by getResources() of Resource Class.

Now , Here you need to read string form String.xml so you can use getString() which is the method of resource so you will get your output Now.

2
  • thank you very much....but pls in a case where there are a lot of strings specified in the strings.xml file, is there a way a variable can be used to to determine the string to show....for instance setText(getResources().getString(R.string.VARIABLE)); or something like that...so that it can show different texts...depending on certain user actions...
    – thunderson
    Commented Nov 12, 2012 at 6:22
  • can you give example which type of user input can change string .? mean for what type of input that can be change my output string . Commented Nov 12, 2012 at 6:38
-2

This would give you the desired effect:

yourEditText.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {

        String text = yourEditText.getText().toString();
        if(text.equals(getResources().getString(R.string.one)) {

            yourEditText.setText(getResources().getString(R.string.two)
        } else if(text.equals(getResources().getString(R.string.two)) {

            yourEditText.setText(getResources().getString(R.string.three)
        } else  {

            yourEditText.setText(getResources().getString(R.string.one)
        }
    });
3
  • i don't want to use settext("******") did you read this in question? Commented Nov 12, 2012 at 5:49
  • why exactly don't you want to use settext()? there is no other way. Commented Nov 12, 2012 at 5:58
  • i want to use a variable to determine the string to print out....i.e. a variable that will determine whether the first string or the last string, etc. in the strings.xml file should be printed out
    – thunderson
    Commented Nov 12, 2012 at 6:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.