Saturday, September 20, 2014

Android: handling input fo TextEdit and updating TextView

Some of the simplest, but very useful apps, can contain forms that that you have to read and fill out. You can do most of that work with TextEdit (input) and TextView (output).

Step 1) Define your layout, name your widgets very well.

Step 2) Define class members for each of the fields that will be used multiple times

public class MainActivity extends ActionBarActivity {
   //during the life-span of the app we will access these fields many times
   private EditText valueEnteredEditText;
   private TextView resultingValueTextView;

Step 3) Find the right layout items by ID and inflate them.

   protected void onCreate(Bundle savedInstanceState) {

      valueEnteredEditText = (EditText) findViewById(;
      resultingValueTextView = (TextView) findViewById(;


Step 4) Add a listener that will react to a value entered

   private void addListenerToValueEnteredEditText() {

      valueEnteredEditText.addTextChangedListener(new TextWatcher() {
         public void onTextChanged(CharSequence s, int start, int before, int count) {
            showToast("onTextChanged ");
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            showToast("beforeTextChanged ");
         public void afterTextChanged(Editable s) {