Advertisement
yo2man

TreeHouse: Using String Resources

Jun 30th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. // Using String Resources:
  2. // In the next video we'll address the key name by setting it
  3. // in one master location the string resource file.
  4. // the Idea is simple: you change the "name" in string and all of the "name" changes. Very useful if you decide to change FeedMe to some other name, you can simply change it one time in the string resource file and it is changed everywhere!
  5.  
  6.  
  7.  
  8. //-----------------------------------------------------------------------------------------------------------------------------------
  9. // RES>VALUES>strings.xml
  10.  
  11.  
  12. strings.xml
  13. <resources>
  14.     <string name="app_name">Interactive Story</string>
  15.  
  16.     <string name="hello_world">Hello world!</string>
  17.     <string name="action_settings">Settings</string>
  18.     <string name="title_activity_story">StoryActivity</string>
  19.     <string name="key_name">name</string>  // This here
  20. </resources>
  21.  
  22. //-----------------------------------------------------------------------------------------------------------------------------------
  23.  
  24. public class MainActivity extends ActionBarActivity {
  25.  
  26.     private EditText mNameField;
  27.     private Button mStartButton;
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.  
  34.         mNameField = (EditText)findViewById(R.id.nameEditText);
  35.         mStartButton = (Button)findViewById(R.id.startButton);
  36.  
  37.         mStartButton.setOnClickListener(new View.OnClickListener() {
  38.             @Override
  39.             public void onClick(View v) {
  40.                 String name = mNameField.getText().toString();
  41.                 startStory(name);*
  42.         });
  43.     }
  44.         *private void startStory(String name);
  45.         Intent intent = new Intent(this, StoryActivity.class);
  46.         Intent.putExtra(getString(R.string.key_name), name);        // change "name" to getString(R.string.id)
  47.         startActivity(intent);
  48. }
  49.  
  50.  
  51.  
  52.  
  53. //-----------------------------------------------------------------------------------------------------------------------------------
  54.  
  55. StoryActivity.java
  56.  
  57. public class StoryActivity extends Activity {
  58.        
  59.     public static final String TAG = story Activity.class.getSimpleName();
  60.  
  61.     @Override
  62.     protected void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.activity_story);
  65.  
  66.         Intent intent = getIntent();
  67.         String name = intent.getStringExtra(R.string.key_name));  // change "name" to getString(R.string.id)
  68.         Log.d(TAG, name);
  69.  
  70.         if (name == null) {
  71.            name = "Friend";
  72.     }
  73.  
  74. }
  75. //https://teamtreehouse.com/library/build-an-interactive-story-app/intents-and-multiple-activities/introducing-string-resources
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement