Ramz
Ramz

Reputation: 7164

remove special charecter in the while entring in a edittext in android;

i need to remove characters if i add any special symbols to the edit text while am entering data to it.for example i am type a word smwinæ æ is a special characters so if i add that chareter edit text should remove the æ and display only smwin by replacing æ .i used text change listener. check the code below

email.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            // Abstract Method of TextWatcher Interface.
            System.out.println("started after");
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            System.out.println("started");

            // Abstract Method of TextWatcher Interface.
        }

        public void onTextChanged(CharSequence searchcontact, int start,
                int before, int count) {
            System.out.println("sssssssssss"+searchcontact);
            String a=searchcontact.toString();
            System.out.println("casted "+a);
            String[] parts = a.split(" ");
            String lastWord = parts[parts.length - 1];
            System.out.println("------------------------------"+lastWord);

//              String lastWord = a.substring(a.lastIndexOf(" ")+1);
//              System.out.println("lastword"+lastWord);
            if(a.equals("p"))
            {

            try {  
                email.getText().delete(email.getSelectionEnd() - 1, email.getSelectionStart());  
            } catch (Exception e) {  
                try {  
                    email.getText().delete(email.length() - 1, email.length());  
                } catch (Exception myException) {  
                //textfield.getText().delete(textfield.length(), textfield.length() - 1);  
                }  
            }  
            // Method User For Sort Out THE Words
                                        // in
                                        // The SearchBar
            }
        }

    });

here when text change i try to get the last word in the string but i failed to do this.used

String a=searchcontact.toString(); System.out.println("casted "+a); String[] parts = a.split(" "); String lastWord = parts[parts.length - 1];

to get the last word but it prints the entire string in the edittext how can i do this please help

Upvotes: 1

Views: 2140

Answers (1)

Jay
Jay

Reputation: 1492

You can add a TextWatcher to an EditText and get notified each time the text is notified. Using that, you can just parse the String to find the characters after a space, and update them to uppercase.

Here's a quick test I made which works pretty well (far from optimal, because each time you edit the Editable it calls the listener again...).

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        String string = s.toString();
        int index = -1;
        while (((index = string.indexOf(' ', index + 1)) != -1) && (index + 1 < string.length())) {
            // Get character
            char c = string.charAt(index + 1);
            if (Character.isLowerCase(c)) {

                // Replace in editable by uppercase version
                s.replace(index+1, index + 2, Character.toString(c).toUpperCase());
            }
        }
    }
});

To avoid being called to often, you could make all the changes in a char[] and only commit to the Editable if changes were made.

A simpler solution is probably to just use split(' ') on your String, replace all first letters in the String[] by the uppercase version (if needed), and commit only once to the Editable.

A simpler optimisation would be to add a boolean to your anonymous class, set it to try when you enter afterTextChanged, set it back to false when you exit it, and only process the string if the boolean is false.

Upvotes: 1

Related Questions