askIT

Android EditText listener

applications-multimedia.png Video tutorial

If you want to run a code or get text from a text field when it changes you can create a listener on the text field.

In the code below we have a text field id editTextUsername. We use the method addTextChangedListener on the variable to activate a listener.

/* EditText Username listener */
EditText editTextUsername = (EditText)findViewById(R.id.editTextUsername);
editTextUsername.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
                if(!(s.toString().equals(""))){
                        // My code here
                }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

Example

Let's create a simple application that prints to screen what is being written to a EditText.

01_skriv_ut_med_listener_android.jpg

We always start with the design. Add a EditText and set ID to editTextUsername.

01_design_brukernavn_id.jpg

Go to MainActivity.java and code the method void sayHello(). This will take in the item EditText and convert it into a string. It will use Toast to write the text string to the screen.

void sayHello(){
        EditText editTextUsername = (EditText)findViewById(R.id.editTextUsername);
        String username = editTextUsername.getText().toString();
        Toast.makeText(this, "Hello " + username, Toast.LENGTH_SHORT).show();
}

The listner will check if there are changes in the textbox EditText. If thre are changes then the method sayHello() will run.

/* EditText Username listener */
EditText editTextUsername = (EditText)findViewById(R.id.editTextUsername);
editTextUsername.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
                if(!(s.toString().equals(""))){
                        // My code
                        sayHello();
                }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

MainActivity.java

package com.nettport.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                /* EditText Username listener */
                EditText editTextUsername = (EditText)findViewById(R.id.editTextUsername);
                editTextUsername.addTextChangedListener(new TextWatcher() {
                        public void afterTextChanged(Editable s) {
                                if(!(s.toString().equals(""))){
                                        sayHello();
                                }
                        }
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                        public void onTextChanged(CharSequence s, int start, int before, int count) {}
                });



        }

        void sayHello(){
                EditText editTextUsername = (EditText)findViewById(R.id.editTextUsername);
                String username = editTextUsername.getText().toString();
                Toast.makeText(this, "Hello " + username, Toast.LENGTH_SHORT).show();
        }

}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.