Display variable on-screen using Android by TextViews
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);
Admin
Updated on March 03, 2020Comments
-
Admin about 4 years
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 toR.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 creating 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.
-
Admin about 13 yearsI tried to do that as well but I got this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carbonaware.gps/com.carbonaware.gps.FirstGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carbonaware.gps/com.carbonaware.gps.gps}: java.lang.NullPointerExcep....... but it will work if I don't use the method .setText which has me confused