Saurav Kumar
Saurav Kumar

Reputation: 61

How to show result of two edit text Values in the third Edit text automatically after entering the two values?

So i have 3 edit texts and i want to fill the third edit text with the subtracted value of the first two edit text. For example : in the first edit text someone enters 4 and in the 2nd someone enters 2, so i want to show the result 4 - 2 that is 2 in the third edit text as soon as the 2nd edit text is filled, without pressing enter or anything.

I have my xml code as follows:

     <EditText
        android:id="@+id/epET"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/et_back"
        android:backgroundTint="#000000"
        android:hint="@string/entry_price"
        android:inputType="numberDecimal"
        android:padding="8dp"
        android:textColor="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar" />

    <EditText
        android:id="@+id/stopET"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/et_back"
        android:backgroundTint="#000000"
        android:hint="@string/stoploss"
        android:inputType="numberDecimal"
        android:padding="8dp"
        android:textColor="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/epET" />

    <EditText
        android:id="@+id/rptET"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/et_back"
        android:backgroundTint="#000000"
        android:hint="@string/risk_per_trade_in_rs"
        android:inputType="numberDecimal"
        android:padding="8dp"
        android:textColor="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/stopET" />

I haven't tried anything yet as i don't know what to do in this situation. I am using Java.

So, please help me with this.

Upvotes: 0

Views: 847

Answers (1)

Mayur Gajra
Mayur Gajra

Reputation: 9073

As suggested in the commets you need to add TextWatcher to your EditText & whenever afterTextChanged gets called you can call your logic function.

I have created one example for your use case.

public class MainActivity extends AppCompatActivity {

    EditText epET;
    EditText stopET;
    EditText rptET;

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

        epET = findViewById(R.id.epET);
        stopET = findViewById(R.id.stopET);
        rptET = findViewById(R.id.rptET);

        epET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                calculate();

            }
        });

        stopET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                calculate();
            }
        });

    }

    public void calculate() {
        String epText = epET.getText().toString().trim();
        String stopText = stopET.getText().toString().trim();

        if (epText.isEmpty() || stopText.isEmpty()) {
            rptET.setText("");
            return;
        }


        double ep = Double.parseDouble(epText);
        double stop = Double.parseDouble(stopText);

        double result = ep - stop;
        rptET.setText(String.valueOf(result));
    }


}

Upvotes: 1

Related Questions