Leonardo Cavazzani
Leonardo Cavazzani

Reputation: 293

Update TextView dynamically using RxJava

I have 2 EditText that receive a number value, and I need to sum both values and show dinamically in a TextView. I dont have a button to start the sum, so when the user types the TextView need to change automatically.

I've tried with TextWatcher but I'm having problems when the user types 2 numbers in the same EditText (if he types "1" than "2", the TextView display "3" and not "12")

Here is my XML:

  <EditText
    android:layout_height="wrap_content"
    android:layout_width="200dp"
    android:id="@+id/edit1" />
  <EditText
    android:layout_height="wrap_content"
    android:layout_width="200dp"
    android:id="@+id/edit2" />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"/>

My Java code:

 editText1.addTextChangedListener(new TextWatcher(){
        DecimalFormat dec = new DecimalFormat("0.00");
        @Override
        public void afterTextChanged(Editable arg0) {
            if(!arg0.toString().equals(current)){

                String edittext1_value = arg0.toString();
                int total = Integer.parseInt(edittext1_value + edittext2_value)


            }
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
            finish_contribuir.setVisibility(View.VISIBLE);
            add_more.setVisibility(View.VISIBLE);
        }
        private String current = "";
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int 
        count) {
            if(!s.toString().equals(current)){

            }
        }
    });

Some guys here at office said that maybe RxJava can solve my problem

Upvotes: 0

Views: 322

Answers (1)

jcmhong
jcmhong

Reputation: 88

Replace:

Integer.parseInt(edittext1_value + edittext2_value)    

with:

Integer.parseInt(edittext1_value)+ Integer.parseInt(edittext2_value);

Upvotes: 2

Related Questions