0

I am designing a text based game and I want it so that when a user enters something in an EditText field, other items on screen update accordingly. I tried doing the following in the onCreate method to no avail (probably because that method is only called when the activity is first created).

How do I actively check if a user inputted something in the field without them pressing a button and only by them typing something in the field?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Typeface myTypeface;
    TextView myTextView;

    myTypeface= Typeface.createFromAsset(getAssets(), "fonts/script.ttf");



    EditText players = (EditText) findViewById(R.id.id_players);

    //while(players.getText().toString().matches("")) {}

    if(!players.getText().toString().matches(""))
        myTextView.setText(players.getText().toString()); //this is just to debug if it is actually working
}

2 Answers2

1

Use a TextWatcher on the field via addTextChangedListener.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

You can find here the exact question with solution.

Simply use TextWatcher and implement its onTextChanged() where you'll be able to catch every change to the edit text.

Community
  • 1
  • 1
Nativ
  • 3,092
  • 6
  • 38
  • 69