skip to Main Content

This is my MainActivity.java.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String myString = "Mary";
        int myInt = 12;
        String formatted = getString(R.string.my_xml_string, myString, myInt);

    }
}

string.xml

<resources>
    <string name="app_name">My First App</string>
    <string name="my_xml_string">My sister %1$s is %2$d years old.</string>
</resources>

However when I run the app,I still got ‘My sister %1$s is %2$d years old.’ as text. Anyone knows why?

On the side note, it says the variable formatted is not used. So how does that link to the string.xml file?

Click to see image

2

Answers


  1. Chosen as BEST ANSWER

    In case anyone is facing the same issue, I use

    #setText to pass value
    RandomGeneratedNumber = (TextView)findViewById(R.id.random);
    RandomGeneratedNumber.setText(random_generated_number);
    

  2. Try this

    String mString = getString(R.string.hello);
    

    or

    String mString= getResources().getString(R.string.hello);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search