0

I am relatively new to Android development but I do have a pretty good understanding of Java and xml etc. so excuse me if this is an easy question and I just can't put two and two together.

Anyway, I am trying to have a user input a few characters into an EditText field. When they press a Button, it will call a method that will output a String. I then want this String to be displayed on the same activity as the EditText field and Button.

How do I go about taking the String variable that is the result of the method and putting into a String in the strings.xml file?

Bert B.
  • 579
  • 2
  • 12
  • 26

1 Answers1

2

See this question. I don't think that is possible, what you probably want to do is store what the user enters in SharedPreferences.

EDIT: To take the string variable and display it on the screen you would want to add a TextView to your layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/textview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text=""/>

Then in code you will add a listener to your button to listen for click events, and have a reference to the TextView in your class:

Fields in class:

TextView tv;
Button myButton;
EditText et;

onCreate():

tv = (TextView)findViewById(R.id.textview);
myButton = (Button)findViewById(R.id.button);
et = (EditText)findViewById(R.id.edittext);

//Now set the onclick listener:
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(args....) 
{
    String txt = et.getText().toString();
    tv.setText(txt);
}
});

Also check out this tutorial.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • Hmm, I see. I think I should rephrase my question though. What's the simplest way to just take the String variable and display it on the screen? – Bert B. Aug 03 '11 at 18:33
  • Ah, that's it right there. I want to kick myself for missing something so tiny. I had all the code in place with the exception of setText(variable); Thank you! – Bert B. Aug 03 '11 at 19:06