Cinthia Danielle Cinthia Danielle - 10 months ago 105
Java Question

progressBar with 5 checkbox

I need progressBar to load the percentage of how many checkboxes are checked.

I tried to use it for 5 checkbox:

"progressbar.setProgress (20)" if it was checked, and if it was not "progressbar.setProgress (-20)"

The progressBar loads only "20%" even if the 5 checkboxes are checked.Can someone help me?

public class MainActivity extends AppCompatActivity {

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


final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);


SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if (preferences.contains("checkbox1") && preferences.getBoolean("checkbox1", false) == true) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);

}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox.isChecked()) {
editor.putBoolean("checkbox1", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox1", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});


if (preferences.contains("checkbox2") && preferences.getBoolean("checkbox2", false) == true) {
checkBox2.setChecked(true);
} else {
checkBox2.setChecked(false);

}
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox2.isChecked()) {
editor.putBoolean("checkbox2", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox2", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});


if (preferences.contains("checkbox3") && preferences.getBoolean("checkbox3", false) == true) {
checkBox3.setChecked(true);
} else {
checkBox3.setChecked(false);

}
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox3.isChecked()) {
editor.putBoolean("checkbox3", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox3", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});

if (preferences.contains("checkbox4") && preferences.getBoolean("checkbox4", false) == true) {
checkBox4.setChecked(true);
} else {
checkBox4.setChecked(false);

}
checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox4.isChecked()) {
editor.putBoolean("checkbox4", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox4", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});

if (preferences.contains("checkbox5") && preferences.getBoolean("checkbox5", false) == true) {
checkBox5.setChecked(true);
} else {
checkBox5.setChecked(false);

}
checkBox5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox5.isChecked()) {
editor.putBoolean("checkbox5", true);
progressBar.setProgress(20);
editor.apply();
} else {
editor.putBoolean("checkbox5", false);
progressBar.setProgress(-20);
editor.apply();
}
}
});

}


}

Answer Source

Your problem is you always set it to 20 or -20 instead of updating it from its current value. use

progressBar.setProgress(progressBar.getProgress()+20);

or

progressBar.setProgress(progressBar.getProgress()-20);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download