Search code examples
javaandroidandroid-edittextandroid-textwatcher

How to change TextView if there's input in multiple EditTexts?


I'm trying to wrote android program which displays Greatest Common Divisor of two integers specified in two different EditText fields. First I've done it with button, everything worked (you can see onclick listener commented out in code below). Now I want to do this: app checks when both EditTexts are not empty and then automatically starts calculating and shows gcd. Buty app crashes when I start typing in any of EditText fields. Also I tried to add TextChangeListener only on one of EditTexts. Everything is good until I delete all input from one of the fields, then app crashes again. I'm only starting to understand android development and made this app mostly by modifying examples found on internet so maybe I did something wrong... Can anyone help me? Thanks

MainActivity.java

    public class MainActivity extends Activity 

{
    EditText a;
    EditText b;
    TextView gcdResult;
    Button calculateGcd;
    int a, b, gcdValue

    TextWatcher textWatcher = new TextWatcher(){
        @Override
        public void afterTextChanged(Editable s){}

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

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

            AutoCalculateGcd();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        a = (EditText)findViewById(R.id.aText1);
        b = (EditText)findViewById(R.id.bText1);
        gcdResult = (TextView)findViewById(R.id.resultTextView1);
        calculateGcd = (Button)findViewById(R.id.calcButton1);

        /* calculateGcd.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                AutoCalculateRatio();
            }
        });*/

        a.addTextChangedListener(textWatcher);
        b.addTextChangedListener(textWatcher);

    }



    //Euclidean alghorithm to find gcd
    public static int gcd(int a, int b) {
        if (b == 0) return w;
        else return gcd(b a % b);

        }
    public static boolean isInputNotEmpty(EditText a, EditText b){
        String a = a.getText().toString();
        String b = b.getText().toString();
        if(a.equals("") && b.equals("") ){
             return false;
    }
    else{
        return true;
    }

    }
    public void AutoCalculateGcd(){
        if(isInputNotEmpty(a, b)){
            a = Integer.parseInt(width.getText().toString());
            b = Integer.parseInt(height.getText().toString());
            gcdValue = gcd(a, b);
            ratioResult.setText(Integer.toString(gcdValue));
        }
        else{
            //Toast.makeText(this, "No input", Toast.LENGTH_SHORT).show();
        }
    }
}

Solution

  • Actually, you should replace

    public static boolean isInputNotEmpty(EditText a, EditText b) {
        String a = a.getText().toString();
        String b = b.getText().toString();
        if (a.equals("") && b.equals("")) {
             return false;
        }
        else {
            return true;
        }
    }
    

    with

    public static boolean isInputNotEmpty(EditText a, EditText b) {
        String a = a.getText().toString();
        String b = b.getText().toString();
        if (a.equals("") || b.equals("")) {
             return false;
        }
        else {
            return true;
        }
    }
    

    Or even

    public static boolean isInputNotEmpty(EditText a, EditText b) {
        return !(a.getText().toString().isEmpty() || b.getText().toString().isEmpty());
    }
    

    Because you want to know if any ( || ) of them is empty, not if both (&&) are.