Automatically show result of a calculator

Viewed 33

I'm new to android and java. I've built a calculator and I need it to show the result automatically when I put 3 inputs but I don't know how to.

package com.example.animate_hf;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

public class menu extends AppCompatActivity {
    private EditText txtnr1;
    private EditText editTextNumber2;
    private EditText editTextNumber3;
    private TextView textView5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        txtnr1 = findViewById(R.id.txtnr1);
        editTextNumber2 = findViewById(R.id.editTextNumber2);
        editTextNumber3 = findViewById(R.id.editTextNumber3);
        textView5 = findViewById(R.id.textView5);

    }

    public void multiply(View view) {
        int valor1 = Integer.parseInt(txtnr1.getText().toString());
        int valor2 = Integer.parseInt(editTextNumber2.getText().toString());
        int valor3 = Integer.parseInt(editTextNumber3.getText().toString());
        textView5.setText(String.valueOf((valor1 * valor2 * valor3)*24));
    }

I added multiply to a textview (through the attribute onclick) and it worked but I want it to show automatically in a TextView.

2 Answers

You can simply add a button and set onClick to the multiply method. That's what I do most often. You should also adjust the variable names according to what they hold.

Related