Search code examples
javaandroidseekbar

TextView does not auto update from SeekBar progress


The SeekBar in my app is not producing the correct result. The SeekBar starts from 0 so sbSyst is added 70 to each value and sbDias is added 40 to each value.

Partial code:

public void ShowText() {
        int inOne = sbSyst.getProgress();
        int inTwo = sbDias.getProgress();

        if ((inOne >= 0 && inOne <= 20) && (inTwo >= 0 && inTwo <= 20)) {
            tvRes.setText("LOW");
            tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
        }
        else if ((inOne >= 21 && inOne <= 50) && (inTwo >= 21 && inTwo <= 40)) {
            tvRes.setText("IDEAL");
            tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
        }
        else if ((inOne >= 51 && inOne <= 70) && (inTwo >= 41 && inTwo <= 50)) {
            tvRes.setText("PRE-HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#D7EF0C"));
        }
        else if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60)) {
            tvRes.setText("HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#F70729"));
        }
    }

According to the above code, the following should produce "HIGH" as the text, instead, it is displaying "IDEAL"

enter image description here

Here is the Pastebin for the entire code: http://pastebin.com/C91SpbBg

Please help me resolve this.

UPDATE: added the following and it's working 100%

public void ShowText() {
    int inOne = sbSyst.getProgress() + 70;
    int inTwo = sbDias.getProgress() + 40;

    Log.i("SYSTOLIC SHOWTEXT", String.valueOf(inOne));
    Log.i("DIASTOLIC SHOWTEXT", String.valueOf(inTwo));

    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 40 && inTwo <= 60)) {
        tvRes.setText("LOW");
        tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
    }
    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 61 && inTwo <= 80)) {
        tvRes.setText("IDEAL");
        tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
    }
    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 81 && inTwo <= 90)) {
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
    }
    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 91 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }

    if ((inOne >= 91 && inOne <= 120) && (inTwo >= 40 && inTwo <= 80)) {
        tvRes.setText("IDEAL");
        tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
    }
    if ((inOne >= 91 && inOne <= 120) && (inTwo >= 81 && inTwo <= 90)) {
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
    }
    if ((inOne >= 91 && inOne <= 120) && (inTwo >= 91 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }

    if ((inOne >= 121 && inOne <= 140) && (inTwo >= 40 && inTwo <= 90)) {
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
    }
    if ((inOne >= 121 && inOne <= 140) && (inTwo >= 91 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }

    if ((inOne >= 141 && inOne <= 190) && (inTwo >= 40 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }
}

Solution

  • Based on the above image, the values of inOne=82 and inTwo=26. Now Looking at your code, HIGH is displayed:

    if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60))
    

    This is not true, since your are using the && operator. Which means both should be in the range of high. This is the problem.

    Solution: Think about how you want to deal with the situation when one of the values is in the High range and one in the Low range etc. etc. Then write your logic accordingly. (Example: You could also use the || operator, meaning if either one is 'HIGH` show high.)

    Update: Here is a good source where you can read about different if/else rules about interpreting blood pressure readings.

    Update 2: I had some free time. Here is an implementation of the showText() method based on the mentioned link:

    public void ShowText() {
        int inOne = sbSyst.getProgress() + 70;
        int inTwo = sbDias.getProgress() + 40;
    
        if((inOne >= 140) || (inTwo >= 90)) {
            // condition 1: if 140 or more systolic or 90 or more diastolic means HIGH BP
            tvRes.setText("HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#F70729"));
        }
        else if((inOne <= 90) || (inTwo <= 60)){
            // (condition 1 is not met)
            // condition 2: if 90 or less systolic or 60 or less diastolic means LOW BP
            tvRes.setText("LOW");
            tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
        }
        else if((inOne >= 120) || (inTwo >= 80)) {
            // (condition 1 and 2 are not met ==> NOT HIGH, NOT LOW)
            // condition 3: if 120 or more systolic or 80 or more diastolic means PRE-HIGH BP
            tvRes.setText("PRE-HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#D7EF0C"));
        }
        else if((inOne >= 90) || (inTwo >= 60)) {
            // (condition 1, 2 and 3  are not met ==> NOT HIGH, NOT LOW, NOT PRE-HIGH)
            // condition 4: if 90 or more systolic or 60 or more diastolic means IDEAL BP
            tvRes.setText("IDEAL");
            tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
        }
    }