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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:22:18+00:00 2026-05-20T08:22:18+00:00

I have an EditText (accepts 0-9) with a listener. I want to grab input

  • 0

I have an EditText (accepts 0-9) with a listener. I want to grab input as it’s entered, apply a calculation, and display it in the same EditText box.

The box initially displays $0.00. When the user inputs a 2, I want to grab that from the box, parse it to remove the $ and decimal, convert it to an int… divide it by 100 and put a $ in front of it. After setText, it should display $0.02. If they then press 5, I’ll grab it, parse it, end up with 25, do the math and it should display $0.25, etc.

I don’t know if this is the best way, I’m open to new ideas. Here is my current code:

mEditPrice.addTextChangedListener(new TextWatcher(){
        DecimalFormat dec = new DecimalFormat("0.00");
        @Override
        public void afterTextChanged(Editable arg0) {
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start,
                int before, int count) {
            String userInput = mEditPrice.getText().toString().replaceAll("[^\\d]", "");
            int userInputInt = Integer.parseInt(userInput);
            mEditPrice.setText("$"+dec.format(userInputInt / 100));
        }
  • 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-20T08:22:19+00:00Added an answer on May 20, 2026 at 8:22 am

    There’s a few issues to deal with here before you can achieve the kind of functionality you desire.

    1. Whenever you deal with a TextWatcher you need to be careful when setting the text of the EditText object being watched. The reason for this is that every time you call setText on it, it will trigger the watcher again, causing your code to go into an infinite loop.

      To prevent this, you should set the value of text you want to set into a variable outside of the onTextChanged method. When entering the method, check against this variable and only perform your processing code if the value is different from the CharSequence.

    2. The integer variable userInputInt, when divided by 100, will be equal to zero.

      This should be changed to a double to produce values like 0.02 etc.

    3. After those changes we can get the EditText to show $0.02 after entering a 2. But because we have set the value of the EditText in code, the next entry into the EditText will be added to the beginning of the text. Then if we enter a ‘5’ we get $50.02.

      To overcome this, the last thing we need to do is set the position of the EditText to the end of the string, using the set position method.

    Here’s the final solution:

    private String value;
    
    mEditPrice.addTextChangedListener(new TextWatcher(){
        DecimalFormat dec = new DecimalFormat("0.00");
    
        @Override
        public void afterTextChanged(Editable arg0) {
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!s.toString().equals(value)){
                String userInput = mEditPrice.getText().toString().replaceAll("[^\\d]", "");
                double userInputDouble = Double.parseDouble(userInput);
                value = ("$"+dec.format(userInputDouble / 100));
            
                mEditPrice.setText(value);
                mEditPrice.setSelection(value.length());
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an EditText and I want to attach to it a KeyListener that
I have an EditText input that I would like to only allow numbers 1
I have an EditText, which generally shows parallel to the screen X-axis. I want
I have a edittext in which some text are selected . I want to
I have an EditText and a TextView, Now what I want is to fetch
I have Four EditText with different background. It looks like this: I want to
I have an edittext and I want any character that I type in edittext
I have list of products which have EditText in TableLayout . I want Keyboard
I have few EditText objects with text inside. I want that on the first
I want to have EditText object appear when a the User chooses Combination on

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.