0
public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.example.realapptest1.MESSAGE";

TextView test;
EditText edittext;
String spokenwordstring;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    test = (TextView)findViewById(R.id.textView2);
    edittext = (EditText)findViewById(R.id.spokenmsg);
    spokenwordstring = edittext.getText().toString();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void registermessage(View view) {
    test.setText(spokenwordstring);

}

Basically I am trying to show whatever I write in an EditText on a TextView with the code above but I am not succeeding.

Any help is appreciated!

Aryo
  • 92
  • 1
  • 3
  • 7
  • You should create a button in your xml and set its onClick method to perform the action that you want, registermessage(). – TronicZomB Jun 10 '13 at 19:02
  • there is a properly working button that has android:onClick(registermessage);, i can succeed using test.setText("anything); inside public void registermessage – Aryo Jun 10 '13 at 19:03
  • This is probably an answered [question](http://stackoverflow.com/questions/4310525/android-on-edittext-changed-listener) you may be looking for – wesdfgfgd Jun 10 '13 at 19:08

4 Answers4

0

Try the following:

public void registermessage(View view) {
  spokenwordstring = edittext.getText().toString(); //remove this from onCreate
  test.setText(spokenwordstring);
}

When you put spokenwordstring = edittext.getText().toString(); in your onCreate, you are saving the value of the EditText at the time of creation, which is empty.

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
0

When you enter some text in the EditText your variable spokenwordstring will not get updated. The variable is not bound to the value in your EditText.

To get this working you need to do a getText() on your EditText. Try this.

public void registermessage(View view) {
  spokenwordstring = edittext.getText().toString();
  test.setText(spokenwordstring);
}
Varun
  • 33,833
  • 4
  • 49
  • 42
0

You're calling spokenwordstring = edittext.getText().toString(); in your oonCreate method. The problem with this is that the information has not yet been entered into the EditText. So call that line from the method registerMessage() before calling setText.

chRyNaN
  • 3,592
  • 5
  • 46
  • 74
0

You said there is a button that works, but I don't see it in your code. As an alternative, you can add a OnKeyListener to your EditText and use it to update the TextView when the user uses a direction key or hits the enter key. (You would have to check the key codes in the listener.)

jschabs
  • 562
  • 4
  • 20