Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Using String Resources:
- // In the next video we'll address the key name by setting it
- // in one master location the string resource file.
- // 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!
- //-----------------------------------------------------------------------------------------------------------------------------------
- // RES>VALUES>strings.xml
- strings.xml
- <resources>
- <string name="app_name">Interactive Story</string>
- <string name="hello_world">Hello world!</string>
- <string name="action_settings">Settings</string>
- <string name="title_activity_story">StoryActivity</string>
- <string name="key_name">name</string> // This here
- </resources>
- //-----------------------------------------------------------------------------------------------------------------------------------
- public class MainActivity extends ActionBarActivity {
- private EditText mNameField;
- private Button mStartButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mNameField = (EditText)findViewById(R.id.nameEditText);
- mStartButton = (Button)findViewById(R.id.startButton);
- mStartButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String name = mNameField.getText().toString();
- startStory(name);*
- });
- }
- *private void startStory(String name);
- Intent intent = new Intent(this, StoryActivity.class);
- Intent.putExtra(getString(R.string.key_name), name); // change "name" to getString(R.string.id)
- startActivity(intent);
- }
- //-----------------------------------------------------------------------------------------------------------------------------------
- StoryActivity.java
- public class StoryActivity extends Activity {
- public static final String TAG = story Activity.class.getSimpleName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_story);
- Intent intent = getIntent();
- String name = intent.getStringExtra(R.string.key_name)); // change "name" to getString(R.string.id)
- Log.d(TAG, name);
- if (name == null) {
- name = "Friend";
- }
- }
- //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