Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:42:13+00:00 2026-05-25T18:42:13+00:00

I have an EditText and a TextWatcher. Skeleton of my code: EditText x; x.addTextChangedListener(new

  • 0

I have an EditText and a TextWatcher.

Skeleton of my code:

EditText x;
x.addTextChangedListener(new XyzTextWatcher());

XyzTextWatcher implements TextWatcher() {
    public synchronized void afterTextChanged(Editable text) {
        formatText(text);
    }
}

My formatText() method inserts some hyphens at some positions of the text.

private void formatText(Editable text) {
    removeSeparators(text);

    if (text.length() >= 3) {
        text.insert(3, "-");
    }
    if (text.length() >= 7) {
        text.insert(7, "-");
    }
}

private void removeSeparators(Editable text) {
    int p = 0;
    while (p < text.length()) {
        if (text.charAt(p) == '-') {
            text.delete(p, p + 1);
        } else {
            p++;
        }
    }
}

The problem I have is – what is displayed on my EditText isn’t in sync with the Editable. When I debugged the code, I saw that the variable text (Editable) has the expected value, but what’s shown on the EditText doesn’t always match the Editable.

For example, when I have a text
x = “123-456-789”
I cut the text “456” from x manually.
After formatting, my Editable has the value “123-789-”
However, the value shown on my EditText is “123–789”

They have the same value in most cases though.

I assumed that the EditText IS the Editable and they should always match. Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:42:14+00:00Added an answer on May 25, 2026 at 6:42 pm

    Ok, you never actually change the EditText just the Editable. Android EditTexts are not children of the Editable class. Strings are subclasses of the Editable class. The onTextChangedListener doesn’t receive the EditText as an argument but the Editable/String displayed in the EditText. After you format the Editable with the hyphens you then need to update the EditText. Something like this should work fine:

    class MyClass extends Activity{
    
        //I've ommited the onStart(), onPause(), onStop() etc.. methods
    
        EditText x;
        x.addTextChangedListener(new XyzTextWatcher());
    
        XyzTextWatcher implements TextWatcher() {
            public synchronized void afterTextChanged(Editable text) {
                String s = formatText(text);
                MyClass.this.x.setText(s);
            }
        }
    
    }
    

    To prevent the slowdown why not change the formatText method something like this?

    private Editable formatText(Editable text) {
        int sep1Loc = 3;
        int sep2Loc = 7;
    
        if(text.length==sep1Loc)
        text.append('-');
    
        if(text.length==sep2Loc)
        text.append('-');
    
        return text;
    }
    

    Note: I haven’t tested this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code is : EditText edt edt.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0)
I have an edit text meant for searching purpose. I have added searchET.addTextChangedListener(new TextWatcher()
I have an edittext, and a textwatcher that watches if SPACE arrived or not.
I have a edittext in which some text are selected . I want to
I have an EditText field with a Customer Text Watcher on it. In a
I need to detect text changes in an EditText. I've tried TextWatcher, but it
I implement TextWatcher in the Activity : public class Coordinate extends Activity implements TextWatcher
I have attached a TextWatcher to an EditText component in my application, and are
I have a ListView and an EditText . I implement addTextChangedListener on EditText to
I have a EditText and button aligned to parent's bottom. When I enter text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.