How to instantly display the result of an operation on Android?

1

I have 3 EditText .

In the first I put the quantity and in the second a value.

I needed the 3% EditText to instantly show the result of the operation.

Is it possible to do this on Android without using javascript?

    
asked by anonymous 04.08.2015 / 19:32

2 answers

0

You should use a TextWatcher for this.

Here is a detailed example:

<LinearLayout 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:padding="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edittext_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_price"
        android:inputType="number" />

    <EditText
        android:id="@+id/edittext_quantity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_quantity"
        android:inputType="number" />

    <EditText
        android:id="@+id/edittext_amount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_amount"
        android:inputType="number" />

</LinearLayout>

Your activity would look like this:

public class MainActivity extends Activity {

    EditText editTextPrice;
    EditText editTextQuantity;
    EditText editTextAmount;

    double price, quantity, amount;

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

        editTextPrice = (EditText) findViewById(R.id.edittext_price);
        editTextQuantity = (EditText) findViewById(R.id.edittext_quantity);
        editTextAmount = (EditText) findViewById(R.id.edittext_amount);

        editTextPrice.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) {
                if (s.length() > 0) {
                    price = Double.parseDouble(s.toString());
                }

                amount = price * quantity;

                editTextAmount.setText(String.format("%s", String.valueOf(amount)));
            }
        });

        editTextQuantity.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) {
                if (s.length() > 0) {
                    quantity = Double.parseDouble(s.toString());
                }

                amount = price * quantity;

                Locale fmtLocale = Locale.getDefault();
                NumberFormat formatter = NumberFormat.getInstance(fmtLocale);
                formatter.setMaximumFractionDigits(2);

                editTextAmount.setText(String.format("R$ %s", formatter.format(amount)));
            }
        });
    }
}

Do not forget to set in your XML layout the inputType of each EditText to avoid conversion errors.

    
04.08.2015 / 20:14
0

For this you must use TextWatcher :
The following example concatenates the two strings in the third EditText:

TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if(!"".equals(editText1.getText().toString()) && !"".equals(editText2.getText().toString())){
            final String concat = editText1.getText().toString()+" : "+editText2.getText().toString();
            editText3.setText(concat);
        }
    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

editText1.addTextChangedListener(textWatcher);
editText2.addTextChangedListener(textWatcher);

Click here to view the documentation Home Hope this helps!
Greetings!

    
04.08.2015 / 19:57