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 7833703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:51:00+00:00 2026-06-02T12:51:00+00:00

To remove white space from text i have written the below code.my requirement was

  • 0

To remove white space from text i have written the below code.my requirement was to remove white space coming between the text while copy-pasting the text from same EditText(type “text” copy and paste it in same EditText.the text should be “texttext” but actually it was coming like “text text”). But the problem is it is crashing when i add textChangedListener to my Edittext.

 textForm.addTextChangedListener(new TextWatcher() {

     @Override
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

         String str = textForm.getText().toString().replace(" ", ""); 
         textForm.setText(str);
     }

     @Override
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
        // TODO Auto-generated method stub

     }

     @Override
     public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }
 });

logcat error is given below

04-24 14:55:12.660: E/AndroidRuntime(20780): java.lang.StackOverflowError
04-24 14:55:12.660: E/AndroidRuntime(20780):    at android.text.TextUtils.getChars(TextUtils.java:60)
  • 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-06-02T12:51:18+00:00Added an answer on June 2, 2026 at 12:51 pm

    You’ve managed to create an infinite loop 🙂

    You change the text, which causes your onTextChanged listener to be called, where you change the text, which causes your onTextChanged listener to be called where you change the text…..

    Try this

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    
         if (textForm.getText().toString().contains(" ")){
             String str = textForm.getText().toString().replace(" ", ""); 
             textForm.setText(str);
         }
    }
    

    Or this

    @Override
    public void afterTextChanged(Editable s) {
        String replacedText = s.toString().replaceAll(" ", "");
        if (!replacedText.equals(s.toString())) {
             textForm.setText(replacedText);   
        }
    }
    

    There are probably more elegant ways to do this and getting the textview text twice is a small inefficiency, but you get the idea..

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

Sidebar

Related Questions

I need to remove all white space between words in interbase. Coming from a
My code as below, How to remove the white space after add hello. to
I want to remove all the white spaces from a given text file. Is
I was just wondering what the best way to remove the white space from
I'm trying to remove any white space in the number from the textbox (txtAllNum)
Possible Duplicate: how remove wordwrap from textarea I have a text area : <textarea
Following code is an example of text placed into a textarea from a database.
$(#topNav + $(#breadCrumb2nd).text().replace( , )).addClass(current); This is a snippet from my code. I want
I need to remove white spaces between xml tags, e.g. if the original xml
I am trying to remove all spaces from a text field when a form

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.