-5

I have an EditText. setText() method of EditText is called on button click . I want to identity if button click is used or user manually enters the location. I have used TextWatcher , the afterTextChange() is called on if user manually enters location and when setText is used.

Is it possible to identity the scenenerio if editext is manually edited or programatically edited.

Please help.

Thanking you in anticipation.

sa_infinite
  • 49
  • 2
  • 5
  • One would think that if a program sets a text it would be able to remember this in a very basic way. Or are there any cases known already where software shows Alzheimer's disease? – class stacker May 03 '13 at 10:07
  • That's very general... When do you want to check if they've entered text? when they click a button? When they touch the screen? When the keyboard is no longer showing? – rcbevans May 03 '13 at 10:10
  • use a textwatcher on edittext. – Raghunandan May 03 '13 at 10:10
  • Naturally, if the `setText()` is called in your app, you, _the developer_, will know where it is triggered from. For example, on the click of a `Button`. Wherever you need to check how the text was set, why not use a `boolean` flag to check it? – Siddharth Lele May 03 '13 at 10:10
  • 1
    Please mention the purpose of doing such a wierd thing. May be we could guide you for doing something better than this option. – Chintan Soni May 03 '13 at 10:17

3 Answers3

3

You could do something like this:

private boolean isTextSetProgrammatically = false;

private void setTextProgrammatically(String text){

myEditText.removeTextChangedListener(instanceOfMyTextWatcher);
myEditText.setText(text);
isTextSetProgrammatically = true;
myEditText.addTextChangedListener(instanceOfMyTextWatcher)

}

And then also set a TextWatcher to your EditText and set isTextSetProgrammatically to false when onTextChanged() gets triggered.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
0

Use a textwatcher along with the suggestion by Ahmad. If use types in edit text you will know.

   EditText search= (EditText) findViewById(R.id.search);
   search.addTextChangedListener(new TextWatcher() {

       public void onTextChanged(CharSequence s, int start, int before, int count) {
                 // s is the typed character sequence.   

       }

       public void beforeTextChanged(CharSequence s, int start, int count,
           int after) {


         }

         public void afterTextChanged(Editable s) {
         }
        });
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
-2

you can write a custom EditText extends EditText and override the setText() method.

@override
public void setText(CharSequence str) {
   super.setText(...);
   // to do you callback
}
chinabrant
  • 255
  • 2
  • 6