Search code examples
javaandroidtextwatcherandroid-textwatcher

TextWatcher questions


I seem to be having some issues with textwatcher, Being new to java/android development I have a couple questions. In a nutshell, I have 3 fields, 2 editable and 1 view able. The idea is to change either two fields and have the third subtract them.

  1. The below code seems to run fine, but the field doesn't seem to ever change, even if edit the field textwatcher is watching.

  2. Is there a way to watch multiple fields? Ideally I'd like both fields to be watched and change the third field when either are edited.

  3. Is there a better way to have the first two edit fields not null without using try/catch? If I don't use the try/catch, the app just crashes.

Thanks for any help.

package com.autotools.autotools;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class autoLoan extends AppCompatActivity {


double TotalAfterTrade, FinalAutoPrice, TradeInPrice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_auto_loan);

    final TextView PriceAfterTradeText = (TextView) findViewById(R.id.textViewSubTotal);
    EditText FinalAutoPriceText = (EditText) findViewById(R.id.editTextPrice);

    try {
        EditText TradeInPriceText = (EditText) findViewById(R.id.editTextTrade);
        FinalAutoPrice = Double.parseDouble(FinalAutoPriceText.getText().toString());
        TradeInPrice = Double.parseDouble(TradeInPriceText.getText().toString());
        TotalAfterTrade = FinalAutoPrice - TradeInPrice;


    } catch (NumberFormatException e) {
    }


      FinalAutoPriceText.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {
          }

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

          public void onTextChanged(CharSequence s, int start,
                                    int before, int count) {
              PriceAfterTradeText.setText(String.valueOf(TotalAfterTrade));
          }
      });

}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.autotools.autotools.autoLoan">

<EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editTextPrice"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="80dp"
    android:text=""
    android:textSize="25dp" />

<TextView
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Price of Vehicle"
    android:id="@+id/textViewPrice"
    android:layout_alignBottom="@+id/editTextPrice"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="25dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Auto Loan Calculator"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/textView2"
    android:layout_alignEnd="@+id/textView2"
    android:layout_above="@+id/textView2"
    android:textSize="35dp"
    android:layout_marginTop="20dp"
    android:textAlignment="center" />

<EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editTextTrade"
    android:layout_marginTop="40dp"
    android:text=""
    android:textSize="25dp"
    android:layout_below="@+id/textViewPrice"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Trade-in Value"
    android:id="@+id/textViewTrade"
    android:textSize="25dp"
    android:layout_alignBottom="@+id/editTextTrade"
    android:layout_alignLeft="@+id/textViewPrice"
    android:layout_alignStart="@+id/textViewPrice" />

<TextView
    android:layout_width="175dp"
    android:textSize="25dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="0"
    android:id="@+id/textViewSubTotal"
    android:layout_below="@+id/editTextTrade"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="60dp"
    android:textAlignment="center"
    android:layout_alignRight="@+id/editTextTrade"
    android:layout_alignEnd="@+id/editTextTrade" />

<TextView
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Price after Trade"
    android:id="@+id/textViewPriceAfterTrade"
    android:textSize="23dp"
    android:layout_alignTop="@+id/textViewSubTotal"
    android:layout_alignLeft="@+id/textViewTrade"
    android:layout_alignStart="@+id/textViewTrade" />


</RelativeLayout>

Solution

  • It does not change becuase you always put inside the same value which you ware pass to it in onCreate here

     TotalAfterTrade = FinalAutoPrice - TradeInPrice;
    

    That value never changes.

    To change value in textField on change use code like:

            public void onTextChanged(CharSequence s, int start,
                                        int before, int count) {
            FinalAutoPrice = Double.parseDouble(FinalAutoPriceText.getText().toString());
            TradeInPrice = Double.parseDouble(TradeInPriceText.getText().toString());
            TotalAfterTrade = FinalAutoPrice - TradeInPrice;
            PriceAfterTradeText.setText(String.valueOf(TotalAfterTrade));
           }