how save the properties of android controls? - android

I have implemented an application for save the properties of android content view.
I have used check box for enable and disable a editText.When user check the the check box then i am enabling edit text view and when user unchecked the check box then disabling edit text.Here if user check the check box then edit text view will be enable for enter data then the user click on save button then the edit field propertie as enabling will save if user re lunch the app
I have implemented code for it as follows:
((EditText)findViewById(R.id.editText1)).setEnabled(false);
((CheckBox)findViewById(R.id.checkBox1)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
((EditText)findViewById(R.id.editText1)).setEnabled(true);
}
else{
((EditText)findViewById(R.id.editText1)).setEnabled(false);
}
}
});
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//here how to save the properties of editText1 and checkBox1 upto re-launch
}
});
If user re open the application the modified content only will appear.
Please any body help me....

Try something like this:
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("editTextState", editText.isEnabled());
outState.putBoolean("checkBoxState", checkBox.isEnabled());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
editText.setEnabled(savedInstanceState.getBoolean("editTextState"));
checkBox.setEnabled(savedInstanceState.getBoolean("checkBoxState"));
}
}
Or you can use SharredPreferences.
In your Activity:
private EditText editText;
private CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editText.setEnabled(true);
} else {
editText.setEnabled(false);
}
}
});
loadProperties();
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveProperties();
}
});
}
private void saveProperties() {
SharedPreferences.Editor editor = getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE).edit();
editor.putBoolean("CHECK_BOX_STATE", checkBox.isChecked());
editor.putBoolean("EDITEXT_STATE_STATE", editText.isEnabled());
editor.commit();
}
private void loadProperties() {
SharedPreferences prefs = getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE);
checkBox.setChecked( prefs.getBoolean("CHECK_BOX_STATE", false));
editText.setEnabled( prefs.getBoolean("EDITEXT_STATE_STATE", false));
}

What you need to use is:onSaveInstanceState(android.os.Bundle)
This method makes it possibile to save for example all information of your controls.
Another possibility would be to use SharedPreferences
SharedPreferences settings = this.context.getSharedPreferences("myPreferences", 0);
// read stored value
String storedValue = settings.getString("aKey", null);
// store/override value
SharedPreferences.Editor editor = settings.edit();
editor.putString("aKey", "aValue");
editor.commit();

Related

SharedPreference to remember the checkbox is visible and checked

I have a checkbox that is invisible, however when I click on the menu item the checkbox is visible. What should the SharedPreferences look like if the checkbox is checked, and if I leave the app and return to the app to keep the checkbox visible. Now, when I leave the app and the checkbox is checked, and when I return to the app the checkbox is invisible and did not remember that the checkbox was checked.
<CheckBox
android:id="#+id/my_check"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Save Audio file"
android:textColor="#000000"
android:layout_marginTop="20dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
private CheckBox my_check;
// onCreate
addListenerOnSaveAudio();
switch (item.getItemId()) {
case R.id.save_audio:
saveAudio.setVisibility(View.VISIBLE);
}
public void addListenerOnSaveAudio() {
my_check = (CheckBox) findViewById(R.id.my_check);
my_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
Here is the doc for sharedPreference and how to use it.
You need to save the state of your checkbox when someone clicks your menu to do that you can use:
// in onCreate
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences(
"NAME OF YOUR CHOICE", Context.MODE_PRIVATE);
CheckBox checkBox = findViewById(R.id.my_check);
SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("my_check") && preferences.getBoolean("my_check",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("my_check", true);
editor.apply();
}else{
editor.putBoolean("my_check", false);
editor.apply();
}
}
});
private boolean state;
private SharedPreferences sharedPreferences;
private Checkbox my_check;
public void addListenerOnSaveAudio() {
//Instance for save state of my_check
sharedPreferences = getSharedPreferences("name_preferences",Context.MODE_PRIVATE);
my_check = (CheckBox) findViewById(R.id.my_check);
// If my check not exist by default is false else true
state = sharedPreferences.getBoolean("my_check", false);
my_check.setChecked(state);
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//When do click in my_check will change state in preferences and when open your app is apply changes in view.
sharedPreferences.edit()
.putBoolean("my_check",isChecked);
if (isChecked) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
You can use onCheckedChanged() to register a listener to your checkBox, and then save its check status into shared preference.
And get the shared preference value each time you launch your app, i.e. in onCreate() method
So modify your activity's onCreate() and your addListenerOnSaveAudio() as below
public class MyActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox my_check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
my_check = findViewById(R.id.my_check);
SharedPreferences sharedPrefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
boolean isChecked = sharedPrefs.getBoolean("checkValue", false); // default value is false
my_check.setChecked(isChecked);
addListenerOnSaveAudio();
}
public void addListenerOnSaveAudio() {
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("checkValue", isChecked);
editor.apply();
if (!isChecked)
saveAudio.setVisibility(View.INVISIBLE);
}
});
}
}

how to check and uncheck single radio button

I have an issue like selecting and unselecting a single radio button on click, the setOnCheckChangeListner() works only for the first time below is the code which I have tried.
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) radioButton.setChecked(false);
else radioButton.setChecked(true);
}
});
I have made a solution for it using set selected attribute of the radio button.
final RadioButton radioButton = findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!radioButton.isSelected()) {
radioButton.setChecked(true);
radioButton.setSelected(true);
} else {
radioButton.setChecked(false);
radioButton.setSelected(false);
}
}
});
try to use preference xml this one is essay to save the click events.because its like small db.
How to create RadioButton group in preference.xml window?
You are generating a endless loop. That will cause stackoverflow error.
Because you are changing radio button checked status inside onCheckedChanged.
If you need changing check status then do it in click of a button.
Don't change check status inside onCheckedChanged
Perhaps you need to change status of RadioButton for some condition. For that you will do following.
public class MainActivity extends AppCompatActivity {
RadioButton radioButton = null;
CompoundButton.OnCheckedChangeListener listener;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO: 8/6/2018 your logics
}
};
radioButton.setOnCheckedChangeListener(listener);
changeStatus(radioButton, true);
}
private void changeStatus(RadioButton radioButton, boolean status){
radioButton.setOnCheckedChangeListener(null);
radioButton.setChecked(status);
radioButton.setOnCheckedChangeListener(listener);
}
}
When you need to change status then call
changeStatus(radioButton, true);

How to apply shared preference to checkbox

I am new to android development I created a checkbox and how to save check/uncheck using share preference
final Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
addNewBubble();
add.setEnabled(false);
}
});
CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheckedValue = isChecked;
}
});
}
private void addNewBubble() {
BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
#Override
public void onBubbleRemoved(BubbleLayout bubble) {
finish();
System.exit(0);
}
});
bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {
#Override
public void onBubbleClick(BubbleLayout bubble) {
Intent in = new Intent(MainActivity.this, PopUpWindow.class);
in.putExtra("yourBoolName", isCheckedValue );
startActivity(in);
}
});
bubbleView.setShouldStickToWall(true);
bubblesManager.addBubble(bubbleView, 60, 20);
}
There are two Check boxes in this code add and add_fb I want to make the app remember if the checkbox are checked or unchecked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = (CheckBox)findViewById(R.id.checkBox);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("checked") && preferences.getBoolean("checked",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("checked", true);
editor.apply();
}else{
editor.putBoolean("checked", false);
editor.apply();
}
}
});
}
final String FILE_NAME = "com.myproject.prefs";
final Boolean ADD = "add";
final Boolean ADD_FB = "add_fb";
// prefs file..
SharedPreferences prefs = getSharedPreferences(FILE_NAME, Activity.PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// by default both will be unchecked..
checkBoxAdd.setChecked(prefs.getBoolean(ADD, false);
checkBoxAddFb.setChecked(prefs.getBoolean(ADD_FB, false);
// now on checkedchancedEvent update the preferences
checkBoxAdd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(ADD, isChecked);
editor.apply();
}
});
// similary the second one...
checkBoxAddFb.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(ADD_FB, isChecked);
editor.apply();
}
});
Updated
Here we store our preferences in a file of name "com.myproject.prefs". We need to store two preferences values of Boolean data type (one is add, another one is add_fb, as given in the Q).
Now get SharePreferences and its editor objects.
Set values of checkBoxes from stored preferences, if the preferences were not added before then set these to false. So, on the very first time when you run your app, these will get false values as there aren't any values added before.
Whenever you want to get these preferences values
pass your preferences file_name and mode to getSharedPreferences();
// then use those objects to get any specific value like
prefs.getBoolean(your_value_name, default_value); Already mentioned above
Whenever you want to change these preferences values use SharedPreferences.Editor object. Pass your value name and it's new value to
putBoolean().
like
editor.putBoolean(your_value_name, true/false);
then apply the editing.
Hope, it helps

Android ToggleButton toggle state issue

I am new to android. I am creating a app which uses a toggle button. I want the toggle button to do some tasks when the button is in checked state and do some another tasks when the button is unchecked. And I want the toggle button to retain its state even when the user closes the app(by pressing back switch) and comes back. I managed to get it done by using shared preference, but the problem is that when the user turns the toggle button on and goes back and come back again, the tasks are getting done again. Is there any way to retain my toggle button on state, and not to the tasks associated with it?? And sorry for my bad English :)
public class MainActivity extends AppCompatActivity {
ToggleButton onoff;
public static Bundle bundle=new Bundle();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onoff = (ToggleButton) findViewById(R.id.toggleButton);
onoff.setChecked(false);
onoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// do some tasks here
} else {
//do some tasks here
}
}
});
}
#Override
public void onPause(){
super.onPause();
bundle.putBoolean("ToggleButtonState", onoff.isChecked());
}
#Override
public void onResume(){
super.onResume();
if(bundle.getBoolean("ToggleButtonState",false))
{
onoff.setChecked(true);
}
}
}``
Simply add another condition to your OnCheckedChangeListener:
onoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && !bundle.getBoolean("ToggleButtonState",false) {
// do some tasks here
} else if(!isChecked){ //optional + , your preference on what to to :)
//do some tasks here
}
}
});
So, when you return, and your button is checked, but your saved boolean is also true, it will not execute your task.
Edit:
For use with onSaveInstanceState :
In your activity, add the following method:
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("ToggleButtonState", onoff.isChecked());
super.onSaveInstanceState(outState);
}
And retrieve this boolean in onCreate(), under findViewById(...):
boolean checkedStatus = savedInstanceState.getBoolean("ToggleButtonState", false);
onoff.setChecked(checkedStatus);
For additional Information visit Recreating an Activity

How I know CheckBox is clicked or not clicked?

How I know CheckBox is clicked or not clicked?
Thank all
xml file:
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
java file:
public class Setting extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
}
//declaring the checked variable at the top
checked=false;
if(checkbox.isChecked())
{
checked=true;
}
else
{
checked=false;
}
try this hope it will help you:
checkbox mchk1=(CheckBox)findViewById(R.id.chk11);
Boolean cBox1;
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
cBox1=pref.getBoolean("check1", false);
if(cBox1==true){
mchk1.setChecked(true);
//Rb1.setChecked(true);
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
if(mchk1.isChecked() ){
editor.putBoolean("check1", true);
}
else if(!mchk1.isChecked() )
{
editor.putBoolean("check1", false);
}
editor.commit();
Try this
CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
if (cb.isChecked()) {
// code to run when checked
} else {
// code to run when unchecked
}
Make changes in your activity as below,
public class Setting extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox myCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
myCheckBox = findViewById(R.id.checkBox);
myCheckBox.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//here the checkBox is checked
}else {
//here the checkBox is not checked
}
}
}
Let me know if it works...

Categories

Resources