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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.