Reputation: 37
How to make the visibility method used in onpagestarted and onpagefinished detect whether a checkbox is checked ? in other words, i want to modify and use this method for checkBoxes .for example if R.id.checkBox1 is checked, make R.id.progressbar1 visible And if R.id.checkBox1 and R.id.checkBox2 BOTH are checked, make R.id.progressbar2 visible and R.id.progressbar1 Invisible
here is my updated main activity
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.Toast;
public class Settings extends Activity {
CheckBox checkBox_one = null;
CheckBox checkBox_two = null;
CheckBox checkBox_three = null;
CheckBox checkBox_four = null;
CheckBox checkBox_five = null;
ProgressBar progressBar1;
ProgressBar progressBar2;
CheckBox checkBox1;
CheckBox checkBox2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
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);
}
//SAVE CHECKBOX STATE//
checkBox_one = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox_one.setChecked(isChecked);
checkBox_one.setChecked(true);//Enable By Default
checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
checkBox_two = (CheckBox) findViewById(R.id.checkBox2);
boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox_two.setChecked(isCheckedTwo );
checkBox_two.setChecked(true);//Enable By Default
checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});
checkBox_three = (CheckBox) findViewById(R.id.checkBox3);
boolean isCheckedThree = getBooleanFromPreferences("isCheckedThree");
checkBox_three.setChecked(isCheckedThree );
checkBox_three.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedThree");
}
});
checkBox_four = (CheckBox) findViewById(R.id.checkBox4);
boolean isCheckedFour = getBooleanFromPreferences("isCheckedFour");
checkBox_four.setChecked(isCheckedFour );
//checkBox_four.setChecked(true);//Enable By Default
checkBox_four.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedFour");
}
});
checkBox_five = (CheckBox) findViewById(R.id.checkBox5);
boolean isCheckedFive = getBooleanFromPreferences("isCheckedFive");
checkBox_five.setChecked(isCheckedFive );
checkBox_five.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedFive");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
//-------------------------//
@Override
public void onBackPressed()
{
// Stop back button Functioning
}
public void openrate1(View view) {
Intent intent = new Intent(this, Rate.class);
startActivity(intent);
}
public void gotohome(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void savesettings(View view) {
Toast toast=Toast.makeText(this, "Settings successfully saved!", Toast.LENGTH_LONG);
toast.show();
}
}
Upvotes: 1
Views: 1227
Reputation: 3386
First if you want to a Settings activity i suggest you to you Default preferenceActivity. And why do you want to show progress bar. Is it for saving settings. If It is, you don't need to use it. Because saving settings is a very fast.
You already write some thing for saving and reading Settings. You can use it in your listener like you used before.
You can use simple if checks for that
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.Toast;
public class Settings extends Activity {
CheckBox checkBox_one = null;
CheckBox checkBox_two = null;
CheckBox checkBox_three = null;
CheckBox checkBox_four = null;
CheckBox checkBox_five = null;
ProgressBar progressBar1;
ProgressBar progressBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
checkBox_one = (CheckBox) findViewById(R.id.checkBox1);
checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
updateProgressBars();
}
});
checkBox_two = (CheckBox) findViewById(R.id.checkBox2);
checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
updateProgressBars();
}
});
}
public void updateProgressBars() {
progressBar1.setVisibility(View.INVISIBLE);
progressBar2.setVisibility(View.INVISIBLE);
if (checkBox_one.isChecked() && checkBox_two.isChecked()) {
progressBar2.setVisibility(View.VISIBLE);
} else if (checkBox_one.isChecked()) {
progressBar1.setVisibility(View.VISIBLE);
}
}
Upvotes: 1
Reputation: 3112
To query checkbox state, use it's isChecked() method:
CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
Upvotes: 0