Skip to content Skip to sidebar Skip to footer

Detect When User Enters Data Into Edittext Immediately Shows Answer

How is it possible to detect if a character is inputted into an EditText which makes it from 0 to 1 characters long and then perform some action?

Solution 1:

Since you have a rather abstract question, let me attempt an equally generic answer:

In your onCreate(), declare and cast your EditText

EditTexteditText= (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);

And then, outside the onCreate(), setup the filterTextWatcher like this:

privateTextWatcherfilterTextWatcher=newTextWatcher() {

    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
        // DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONSintradius=0;
        radius = Integer.valueof(s.toString);
        doublearea= Math.PI * radius * radius;
    }

    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @OverridepublicvoidafterTextChanged(Editable s) {

    }
};

EDIT:

Updated code with possible calculation. (UNTESTED CODE: I JUST TYPED IT IN WITHOUT TESTING IT. MODIFY WHERE NECESSARY)

Read more about TextWatcher's here

And here are a few examples to get you started:

  1. http://www.android-ever.com/2012/06/android-edittext-textwatcher-example.html
  2. http://www.cybernetikz.com/blog/android-textwatcher-example/
  3. http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=22b00052-dad0-4e09-a07e-b74f115ab247
  4. http://myandroidsolutions.blogspot.in/2012/06/android-edittext-change-listener.html

Solution 2:

You can use TextWatcher on your EditText and handle whatever you want to do with your user Input data Immediately.

here is an example of formatting immediately when user inputs data

finalEditTextEditTxtFinancialCode= (EditText) findViewById(R.id.edtNewCpFinancialCode);


    EditTxtFinancialCode.addTextChangedListener(newTextWatcher() {

        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {

            Stringa="";
            booleanflag=true;
            String eachBlock[] = EditTxtFinancialCode.getText().toString().split("-");
            for (inti=0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 4) {
                    flag = false;
                }
            }
            if (flag) {

                EditTxtFinancialCode.setOnKeyListener(newOnKeyListener() {

                    @OverridepublicbooleanonKey(View v, int keyCode, KeyEvent event) {

                        if (keyCode == KeyEvent.KEYCODE_DEL)
                            KeyDel = 1;
                        returnfalse;
                    }

                });

                if (KeyDel == 0) {

                    if (((EditTxtFinancialCode.getText().length() + 1) % 5) == 0) {

                        if (EditTxtFinancialCode.getText().toString().split("-").length <= 2) {
                            EditTxtFinancialCode.setText(EditTxtFinancialCode.getText() + "-");
                            EditTxtFinancialCode.setSelection(EditTxtFinancialCode.getText().length());
                        }

                    }
                    a = EditTxtFinancialCode.getText().toString();
                } else {
                    a = EditTxtFinancialCode.getText().toString();
                    KeyDel = 0;
                }

            } else {
                EditTxtFinancialCode.setText(a);

            }

        }

        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
        @OverridepublicvoidafterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });

Solution 3:

http://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html

Make a listener.

   myTextBox.addTextChangedListener(newTextWatcher() {

   publicvoidafterTextChanged(Editable s) {
   }

   publicvoidbeforeTextChanged(CharSequence s, int start, 
     int count, int after) {
   }

   publicvoidonTextChanged(CharSequence s, int start, 
     int before, int count) {
   TextViewmyOutputBox= (TextView) findViewById(R.id.myOutputBox);
   myOutputBox.setText(s);
   }
  });

see http://www.mysamplecode.com/2012/06/android-edittext-text-change-listener.html

Post a Comment for "Detect When User Enters Data Into Edittext Immediately Shows Answer"