How do I use a string resource for a string variable?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How do I use a string resource for a string variable?

Postby SlumberMachine » Tue Oct 13, 2009 1:08 am

so my string is R.string.menu_exit in the strings xml file. I can call these correctly for stuff like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.getWindow().setTitle(Resources.getText(R.string.main_title));
  2.  
  3.  
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


and
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. msgTextView.setText(R.string.hello_message);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


But how do I set my own local string variable to the resource:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private String myString = R.string.menu_exit;
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


This obviously does not work, what is the correct way?
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Postby padde » Tue Oct 13, 2009 1:50 am

If you are in a class which extends activity you can use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private String myString = getResources().getString(R.string.menu_exit);
  3.  
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby SlumberMachine » Tue Oct 13, 2009 9:44 am

Thank you VERY much! That was exactly what I was looking for. Yes, it is in a "extended activity". Perfect! You made my day! :D
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests