Search code examples
androidif-statementvisibility

If-else statement and visibility - android


I have five checkboxes in my app along with five progressbars, and have called visibility according to checkboxes to make progressbars visible/gone. so is there any alternative to if-else statements to use in visibility method , cus in that case there are endless possibilities in the five checkboxes so i have to write endless if else statements in order to make the progressbars visible/gone in all the possibilities.

for example- if CheckBox1&checkbox2 are checked, make progressbar2 visible if checkbox1&checkbox3 are checked, make progressbar2 visible

in other words, i want to make Progressbar 1 visible if any One of the checkboxes are checked and progressbar 2 visible if any two are checked and so on..

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

public class Progress extends Activity {

    ProgressBar progressBar1;
    ProgressBar progressBar2;

    CheckBox checkBox1;
    CheckBox checkBox2;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    SharedPreferences setprefsd;

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

        setprefsd = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

        if (setprefsd.getBoolean("FirstCheckBox", false) == true) {
            checkBox1.setChecked(true);
            updateProgressBars();
        }

        if (setprefsd.getBoolean("SecondCheckBox", false) == true) {
            checkBox2.setChecked(true);
            updateProgressBars();
        }
    }

    public void updateProgressBars() {
        progressBar1.setVisibility(View.GONE);
        progressBar2.setVisibility(View.GONE);

        if (checkBox1.isChecked() && checkBox2.isChecked()) {
            progressBar2.setVisibility(View.VISIBLE);
        } else if (checkBox1.isChecked()) {
            progressBar1.setVisibility(View.VISIBLE);
        }
    }
}

Solution

  • Short answer

    You can use the ternary operator to make the visibility change simpler (instead of ifs).

    I guess the approach here would be to count the number of checkboxes, then set the visibilities. You would just have to change your updateProgressBars() method like this:

    public void updateProgressBars() {
        int nbCheckboxes = 0;
        if (checkBox1.isChecked())
            nbCheckboxes++;
        if (checkBox2.isChecked())
            nbCheckboxes++;
    
        progressBar1.setVisibility(nbCheckboxes >= 1 ? View.VISIBLE : View.GONE);
        progressBar2.setVisibility(nbCheckboxes >= 2 ? View.VISIBLE : View.GONE);
    }
    

    Other remarks

    IF statement with boolean expressiosn

    Your are using constructs such as:

    if (myBooleanExpression == true)
    

    The == true is not needed because you're using a boolean expression here. Use instead:

    if (myBooleanExpression)
    

    Using arrays for your variables

    If you use several numbered variables that have the same kind of purpose, you might want to consider using arrays.

    public class Progress extends Activity {
    
        ProgressBar[] progressBars;
        CheckBox[] checkBoxes;
    
        SharedPreferences sharedPreferences;
        SharedPreferences.Editor editor;
        SharedPreferences setprefsd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_progress);
    
            setprefsd = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    
            progressBars = new ProgressBar[3]; // 3, or whatever number you have
            progressBars[0] = (ProgressBar) findViewById(R.id.progressBar1); 
            progressBars[1] = (ProgressBar) findViewById(R.id.progressBar2); 
            progressBars[2] = (ProgressBar) findViewById(R.id.progressBar3);
    
            checkBoxes = new CheckBox[3];  // 3 or whatever number you have
            checkBoxes[0] = (CheckBox) findViewById(R.id.checkBox1);
            checkBoxes[1] = (CheckBox) findViewById(R.id.checkBox2);
            checkBoxes[2] = (CheckBox) findViewById(R.id.checkBox3);
    
            checkBoxes[0].setChecked(setprefsd.getBoolean("FirstCheckBox", false));
            checkBoxes[1].setChecked(setprefsd.getBoolean("SecondCheckBox", false));
            checkBoxes[2].setChecked(setprefsd.getBoolean("ThirdCheckBox", false));
    
            updateProgressBars();
        }
    
        public void updateProgressBars() {
            int nbCheckBoxes = 0;
            for (CheckBox cb : checkBoxes) {
                if (cb.isChecked())
                    nbCheckBoxes++;
            }
    
            for (int i = 0; i < progressBars.length; i++) {
                progressBars[i].setVisibility(nbCheckboxes > i ? View.VISIBLE : View.GONE);
            }
        }
    }