1

I have a program where i have an editText . I want to implement TextWatcher to reflect the changes in the total edittext. I want to show the alert dialog when editbox reach the max character limit 10. When i implement Text watcher it works but it show two alert box when it reach 10 character. here is my code

private TextWatcher mTextEditorWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // This sets a textview to the current length
        // mTextView.setText(String.valueOf(s.length()));

    }

    public void afterTextChanged(Editable s) {
        if (s.length() == 10) {
            messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    SendSms.this);

            alert.setMessage("You Cross the limit of 10 Words !");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    });

            alert.show();

        }

    }
};
Gaurav kumar
  • 796
  • 2
  • 8
  • 25

6 Answers6

1
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
             SendSms.this);
     AlertDialog alertDialog ;

 private TextWatcher mTextEditorWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // This sets a textview to the current length
            // mTextView.setText(String.valueOf(s.length()));

      if (s.length() >= 10) {
                messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
                        alertDialogBuilder.setMessage("You Cross the limit of 10 Words !");
         alertDialogBuilder.setPositiveButton("OK",
                 new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog,
                             int whichButton) {

                     }
                 });

         alertDialog = alertDialogBuilder.create();
         if(alertDialog != null  && !alertDialog.isShowing()){
             alertDialog.show();
         }



            }
    else
    {
       mTextView.setText(String.valueOf(s.length()));
    }



        }

        public void afterTextChanged(Editable s) {


        }
    };

I have editted the code . Globally declare alert Dialog and check whether alertDialog is already shown and then display the alert

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
1

This is common behavior of this method. You need to handle using flag.

Use this code

   private TextWatcher mTextEditorWatcher = new TextWatcher() {
        boolean flag = false; 

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

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

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() == 10) {
            if (!flag) {
                messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
                AlertDialog.Builder alert = new AlertDialog.Builder(
                        SendSms.this);

                alert.setMessage("You Cross the limit of 10 Words !");
                alert.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                            }
                        });

                alert.show();
            }
            flag = true; 
        } else {
            flag = false; 
        }

    }
});

Other references

TextWatcher events are being fired multiple times

Events of TextWatcher are being called twice


Better is to use setError. Read how to use it.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • 1
    No no no. replace your code with this. See what as I updated the code – Pankaj Kumar Dec 06 '13 at 04:55
  • 1
    woo! thats nice!. Congs.. and one more thing you can do is `s.length() >= 10` instead of `s.length() == 10`. – Pankaj Kumar Dec 06 '13 at 05:06
  • sir i want to use test view bellow edittext. that shows that 150,149 character are left. can you help me ?? – Gaurav kumar Dec 06 '13 at 05:09
  • Yes sir :).. Read this http://stackoverflow.com/questions/3013791/live-character-count-for-edittext-android . See they have added `mTextView.setText(String.valueOf(s.length()));`. Here `mTextView` i the TextView where you want to show count of chars. – Pankaj Kumar Dec 06 '13 at 05:14
  • Read above, if any prob say me, will try to clear :) – Pankaj Kumar Dec 06 '13 at 05:14
  • sir i want to show. if i write 9 charcter in edit box then text view show that 151 character are left. if i write 29 character then textview show that 131 charcter are left – Gaurav kumar Dec 06 '13 at 05:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42621/discussion-between-pankaj-kumar-and-gaurav-kumar) – Pankaj Kumar Dec 06 '13 at 05:17
0

Try this..

have you tried in onTextChanged

private TextWatcher mTextEditorWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // This sets a textview to the current length
        // mTextView.setText(String.valueOf(s.length()));


        if (s.length() == 10) {
            messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    SendSms.this);

            alert.setMessage("You Cross the limit of 10 Words !");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    });

            alert.show();

        }

    }

    public void afterTextChanged(Editable s) {

    }
};
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Why should this work instead of `afterTextChanged`? They should both give the same result. – Michael Yaworski Dec 06 '13 at 04:30
  • 1
    @Gauravkumar if you type 10 letters it'll show one dialog then if you press ok then you type extra 1 letter then you erase that letter means then dialog will show. – Hariharan Dec 06 '13 at 04:51
0

Simply make the alert instance global it will work fine.

    private AlertDialog.Builder alert ;

in your onCreate

    alert = new AlertDialog.Builder(
                 RegisterActivity.this);

in your afterTextChanged

    public void afterTextChanged(Editable s) {
        if (s.length() == 10) {
            //messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            alert.setMessage("You Cross the limit of 10 Words !");
            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    });

            alert.show();

        }

    }
Deniz
  • 12,332
  • 10
  • 44
  • 62
  • Gaurav in my case it is working fine. Dont put AlertDialog.Builder( RegisterActivity.this); in your afterTextChanged() method – Deniz Dec 06 '13 at 04:46
0

Hi I did such work for phone number, when it reaches 5 digits, it automatically put space. You can utilize my code. Please check below code.

public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (s.length() == 5 && start == 4) {
                txtNumber.removeTextChangedListener(this);
                txtNumber.append(" ");
                txtNumber.addTextChangedListener(this);
            }
            if (s.length() == 6 && start == 5) {
                txtNumber.removeTextChangedListener(this);
                String newtext = s.toString().substring(0, s.length() - 1)
                        + " " + s.charAt(s.length() - 1);
                txtNumber.setText("");
                txtNumber.append(newtext);
                txtNumber.addTextChangedListener(this);
            }
        }

Let me know if you need more help

Vimal Rughani
  • 234
  • 4
  • 14
0

Try Following Code :

Global Instance :

private AlertDialog.Builder alert;

TextWatcher :

private TextWatcher mTextEditorWatcher = new TextWatcher() {

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

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() >= 10) {
            messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
            // alert = new AlertDialog.Builder(MainActivity.this);
            // alert.setMessage("You Cross the limit of 10 Words !");
            // alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            // public void onClick(DialogInterface dialog, int whichButton) {
            // }
            // });
            // alert.show();

            // Else you should use the following one to show error message
            messageBody.setError("You Cross the limit of 10 Words !");

        } else {
            messageBody.setError(null);
        }

    }

    public void afterTextChanged(Editable s) {
    }
};
SilentKiller
  • 6,944
  • 6
  • 40
  • 75