Display variable on-screen using Android by TextViews
I'm having a slight problem trying to display a variable on-screen.
Right now I have a tabbed layout. In the fourth tab there is a nested ActivityGroup
which has a mapactivity inside of it.
The trouble is this map activities setContentView
is set to R.layout.gps
I am trying to display the distance traveled on-screen ontop or beside the map to show how far the user has travelled.
I can get text to display on-screen by cr开发者_开发技巧eating a text view inside of the XML. However using this method I am unable to link it to a Double
variable inside of the activity.
At the same time if I create a TextView t = new TextView(this)
it will not be displayed on-screen as the set content is now on it.
If anyone could shed any light on this it would be greatly appreciated.
Make sure that the TextView you want to use in res/layout/gps.xml has the following in it:
android:id="@+id/mytextview"
And then get use Klaus's code to find your TextView in your Java code:
TextView myTextView = (TextView) findViewById(R.id.mytextview);
myTextView.setText("My double value is " + doubleValue);
Let's assume you have a TextView in your XML with the id of mytextview.
Use:
TextView myTextView = (TextView) findViewById(R.id.mytextview);
to get a dynamic reference to the TextView. Then you can use
myTextView.setText("My double value is " + doubleValue);
to update it as you go along.
精彩评论