Android Eclipse: Change The Text In The App To A String Created Withing The Program
Solution 1:
How can i get a string that i create in the .java file to show up on the app UI?
I'm assuming you want to display it in a TextView
. If so, you can do the following:
String str = "Hello there!";
private TextView text = (TextView) findViewById(R.id.textViewId);
text.setText(str);
Solution 2:
How can i get a string that i create in the .java file to show up on the app UI?
In Android, anything that visualises somthing to the user is called a View. There are lots of different types, suitable for visualising different types of data. For basic text you will need a TextView
. So simplify things, let's just assume that you have the XML code as presented in your link:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"
/></LinearLayout>
There are numerous resources that explain how to you build/use layouts, their pros, cons and what not, so I'm skipping that part. The important thing to realise is that there is a TextView
defined in the layout presented above. In order to reference it, it will need to have a unique id. Let's add one:
<TextView
android:id="@+id/question_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
You can then reference this TextView from Java code as follows:
TextViewquestionTextView= (TextView) findViewById(R.id.question_textview);
Note that R.id.question_textview
is basically what we named the TextView in the layout earlier. From here on, you have a Java object that you can do all sorts of things with, including getting and settings the text it is showing.
StringtextDisplayed= questionTextView.getText(); // This will get the text currently displaying.
questionTextView.setText("Please display me"); // This will set the displayed text to "Please display me".
I would really advise you to go through some more tutorials and api demos, as this is really basic stuff.
Post a Comment for "Android Eclipse: Change The Text In The App To A String Created Withing The Program"