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

  • Home
  • SEARCH
  • 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 7565107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:04:01+00:00 2026-05-30T14:04:01+00:00

In my Android application I need to implement a TextWatcher interface to implement onTextChanged

  • 0

In my Android application I need to implement a TextWatcher interface to implement onTextChanged. The problem I have is, I want to update the same EditText With some extra string. When I try to do this the program terminates.

 final EditText ET = (EditText) findViewById(R.id.editText1);
 ET.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            try
            {
                 ET.setText("***"+ s.toString());
                 ET.setSelection(s.length());
            }
            catch(Exception e)
            {
                Log.v("State", e.getMessage());
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s)
        {               
        }
    });

My program terminates and even I try to catch the exception like in my code still it terminates.
Does anyone have any idea why this happens and how I can achieve this? Thanks.

  • 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-30T14:04:02+00:00Added an answer on May 30, 2026 at 2:04 pm

    The content of the TextView is uneditable on the onTextChanged event.

    Instead, you need to handle the afterTextChanged event to be able to make changes to the text.

    For more thorough explanation see: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged


    Note: Error onTextChanged

    Obvioulsy, you are causing an endless loop by continuously changing the text on afterTextChanged event.

    From the ref:

    public abstract void afterTextChanged (Editable s)
    This method is called to notify you that, somewhere within s, the text has been
    changed. It is legitimate to make further changes to s from this
    callback, but be careful not to get yourself into an infinite loop,
    because any changes you make will cause this method to be called again
    recursively. …

    • Suggestion 1: if you can, check if the s is already what you want when the event is triggered.

      @Override
      public void afterTextChanged(Editable s)
      {    
          if( !s.equalsIngoreCase("smth defined previously"))
               s = "smth defined previously";              
      }
      
    • Suggestion 2: if you need to do more complex stuff (formatting,
      validation) you can maybe use a synchronized method like in this
      post.

    Note 2 : Formatting the input as partially hidden with n stars till the last 4 chars ( ****four)

    You can use something like this in suggestion 1:

        @Override
        public void afterTextChanged(Editable s)
        {    
           String sText = ET.getText().toString()
    
            if( !isFormatted(sText))
                 s = format(sText);              
        }
        bool isFormatted(String s)
        {
         //check if s is already formatted
        }
    
        string format(String s)
        {
          //format s & return
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement some functions into an Android application using NDK and thus
In one of my Android Application I need to keep the title bar same
Is there any sample? I have my android application and I need to connect
I want to implement a search in my Android application. In this page, first
I have a application already working fine. I need to implement a quick search
I'm trying to implement some automatic logout code for my Application on Android. I
I have used facebook application for one of my android application. But i need
Mi problem is that I need to implement Contact List in my application for
I need to implement a news feed in my android application. It should animate
I want create a android application and i need to connect to a webservice

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.