Can I Directly Edit String Resources In An Xml Document When Making Android Apps?
Solution 1:
so my question is, can I directly edit the string resources from the xml file?
No, you can't change resources
at runtime. But your code seems kind of goofy. This is all going to run when the Activity
starts up. So, you can check the message then just set it in your TextView
of the layout.
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
TextView textView(textView) findViewById(R.id.textView);
Intentintent= getIntent();
Stringmessage= intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
textView.setText(message);
}
simply create a TextView
inside of activity_display_message.xml
(here called textView
) then set the text with the message
variable.
Also, it is often a good idea to do some error checking such as
if (intent !== null && intent.getStringExtra(MyActivity.EXTRA_MESSAGE != null)
{
// do your stuff here
}
or however you want to go about that.
Storing...
You could store that value in a persistent value such as SharedPreferences
, a file, db, etc... if you are wanting to use it later.
You can read more about that here in the docs
Solution 2:
You don't need to refresh your views like this. Fortunately, Android will handle dynamically displaying the modifications that you make to a layout/view once it is set as your activity's view. However, you can do what you are trying to do ; that is, passing your String as an extra in your Intent and setting the text of your dynamically added TextView to that String, then finally adding that view to your activity's content view.
Since you want to add the TextView to your layout, just do this in place of your setContentViews (below refresh views comment).
ViewGrouplayout= (ViewGroup) findViewById(R.layout.activity_display_message);
TextViewtv=newTextView(this);
tv.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText(message);
tv.setTextSize(40);
layout.addView(tv);
That will modify your layout view, adding your new TextView with the message as it's text to your layout.
Post a Comment for "Can I Directly Edit String Resources In An Xml Document When Making Android Apps?"