Search code examples
androidtextviewandroid-edittextandroid-textwatcher

android TextWatcher beforeTextChanged problem


in my application i have many edit text and i implemented textWatcher for them i want them to increase and decrease a textView number but i can only increase it

i tried to check if the old text is bigger that new text and decrease the textview number, but it returns false everytime

my code :

editText.addTextChangedListener(new TextWatcher() {

                            String oldText = "";

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

                                this.oldText = s.toString();

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {


                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                if (!editText.getText().toString().equals("")) {
                                    int price = Integer.parseInt(editText.getText().toString()) * price_db;
                                    productPrice.setText(price + "");

                                    int totalPrice_n = Integer.parseInt(totalPrice.getText().toString());

                                    int min = Integer.parseInt(editText.getText().toString()) - Integer.parseInt(oldText);

                                    if(Integer.parseInt(editText.getText().toString()) > Integer.parseInt(oldText)){
                                        totalPrice.setText((totalPrice_n + min * price_db) + "");

                                    }else{
                                        totalPrice.setText((totalPrice_n - min * price_db) + "");
                                    }




                                }
                            }
                        });

my problem is that if condition only returns false and goes to else part, also my EditText default text is set to 0 so i think beforeTextChanged only take on 0 and check if new text is bigger than 0

i change editText's text with 2 button ( + , - ) and i want when i click on + button to increase the TextView number and also when i click on - button to decrease TextView number but it only increase it i dont know why


Solution

  • Replace my code with your code and check dude !!! :)

    editText.addTextChangedListener(new TextWatcher() {
    
                            String oldText = "";
    
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                                this.oldText = s.toString();
    
                            }
    
                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    
                            }
    
                            @Override
                            public void afterTextChanged(Editable s) {
                                if (!editText.getText().toString().equalsIgnoreCase("")) 
                {
                    int amount=Integer.parseI`enter code here`nt(editText.getText().toString());
    
                    int price = Integer.parseInt(editText.getText().toString()) * price_db;
    
                    int totalPrice_n = Integer.parseInt(totalPrice.getText().toString());
    
                    productPrice.setText(price);                                    
    
                                    int min = amount - Integer.parseInt(oldText);
    
                    if(Integer.parseInt(oldText) > 0)
                    {
                        if(amount > Integer.parseInt(oldText)){
                                            totalPrice.setText((totalPrice_n + min * price_db));
                                    }else{
                                        totalPrice.setText((totalPrice_n - min * price_db));
                                    }
                                  }
                                    else
                    Toast.makeText(context,"old text is 0",Toast.LENGTH_LONG).show();
                                }
                            }
                        });