Search code examples
androidif-statementsettextandroid-textwatcher

show TextView text depending on an answer


I am very new to this sort of thing and am trying to teach myself but have gotten stuck here. I am writing an app which will calculate the size battery required. The calculator side of things i have done and it is working however i want to add a feature that when the answer is displayed, it will tell you what part number you need for the battery required. I have tried to work this out myself and failed miserably so i am hoping someone may be able to point me in the right direction

The answer from the battery calculator shows in ahresult. what i want is battreq to show the part number required for the correct size battery. so for example, if the calculator showed that a 2.43Ah battery was required, battreq would show "A 3.2ah battery is required" as that is the next size available.

ahresult = (TextView) findViewById(R.id.ahresult);

        battreq.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) {
                Integer.parseInt(ahresult.getText().toString());
                if (ahresult.equals(>= "0.00" && <=  "1.20")){
                    battreq.setText("A 1.2Ah battery is required");
                }
                else if (ahresult.equals (>= "1.21 && <= 2.10")){
                    battreq.setText("A 2.1Ah battery is required");
                }
                else if (ahresult.equals (>= "2.11 && <= 3.20")){
                    battreq.setText("A 3.2Ah battery is required");
                }
              // so on and so on with different battery sizes
            }

Here is my (poor) effort at having a go but i am getting illegal start of expression.

Could someone guide me in the right direction please?

EDIT: Still seem to be having issues with this despite the help here. Below is the updated code. Have i got anything wrong here?

ahresult.addTextChangedListener(new TextWatcher() {

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

                double result = Double.parseDouble(ahresult.getText().toString());

                if (ahresult.equals(result >= 0.001 && result <=1.200 )) {
                    battreq.setText("1.2Ah req");

                }
                else if (ahresult.equals(result >= 1.201 && result <=2.100 )) {
                    battreq.setText("2.1Ah req");
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Solution

  • Firstly, you should add addTextChangedListener to ahresult instead of battreq.

    Secondly, you have to parse the changed text s to Double then compare within your desired range:

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    
        Double result = Double.parseDouble(s.toString());
    
       if (ahresult.equals(result >= 0.00 &&  result <=  1.20)){
           battreq.setText("A 1.2Ah battery is required");
       }
       else if (ahresult.equals (result>= 1.21 && result <= 2.10)){
           battreq.setText("A 2.1Ah battery is required");
       }
       else if (ahresult.equals (result>= 2.11 && result <= 3.20)){
           battreq.setText("A 3.2Ah battery is required");
       }
       // so on and so on with different battery sizes
    }