How do I display a string in an Android app?
I'm confused on exactly what your problem is here but you have a couple problems if you want your TextView to display the message. This line here
TextView textView = new TextView(this);
will create a new TextView, not reference the one you have in your xml. And it won't show because you haven't added it to your contentView. So, when you call setContentView(R.layout.activity_display_message); what will be shown is what is in activity_display_message. If you want to access that View then you need to give it an id in your xml file and access it with findViewById(). So give it an id
android:id="@+id/tv1" // give it an id here
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/entered_message" />
then access it after calling setContentView(...)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_display_message);
TextView tv = (TextView) findViewById(R.id.tv1); // reference it here with the id you gave it in the xml
}
Now that you have referenced the TextView, you can call setText(), setTextSize(), etc.. if need be.
I'm not exactly sure what String you want where so its hard to give much more help without a better explanation. But note that
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
will assign the message variable to whatever is passed in your Intent from the calling Activity.
Read more
stackoverflow.comm
No comments:
Post a Comment