Actually i have 3 buttons.User should click on any one button then all the 3 buttons should disable permanently throughout the app(when we close and open the app, buttons should be in disable state).How can i achieve this?
Thanks in advance.
define the behavior in SharePreferences:
for example use this in onResume:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = pref.getBoolean("isEnabled",true);
myButton.setEnabled(enabled);
in onClick event of the button do this:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
pref.edit().putBoolean("isEnabled",false).commit();
myButton.setEnabled(false);
Use shared preference to store the clicked state of button.And check the preference value each time in activity/ fragment and disable or enable as per preference value.
you need to save your button state in sharedpreferences and based on your condition you need to enable / disable it in your activity code.
if(stateofbuttonfromprefs) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
You can use SharedPreference for your purpose. For more information refer this
declare SharedPreference before onCreate method
SharedPreferences stateButton;
SharedPreferences.Editor bEditor;
initialize this on onCreate()
stateButton= getApplicationContext().getSharedPreferences("Button_State", 0);
bEditor = stateButton.edit();
add these two methods on your activity
public void setBState(boolean e) {
bEditor.putBoolean("btn_state", e);
bEditor.commit();
}
public boolean getButState(){
return stateButton.getBoolean("btn_state", true);
}
call this to know your button state call
but.setEnabled(getBState());
when you need to disable the button, use
setBState(false);
On Button click
button_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("btn_click", true).commit();
}
});
In Activity OnCreate method
Boolean btnClick= prefs.getBoolean("btn_click", false);
if(btnClick){
//Disable Button
}else{
//Enable Button
}
Related
So i need checkboxes to save state when i exit or switch activity. I need many checkboxes, so I need a function that works for all the checkboxes.
please help.
the simple solution is to use SharedPreferences,
you can read about it here
Code
create these 2 methods in your activity :
private void saveCheckboxesStates(){
SharedPreferences sharedPref =getActivity().getSharedPreferences("fileName",Context.MODE_PRIVATE);//replace fileName with any name you like
SharedPreferences.Editor editor = sharedPref.edit();
//suppose we have 3 checkboxes that we want to save their states
editor.putBoolean("checkbox1_state", checkBox1.isChecked()));
editor.putBoolean("checkbox2_state", checkBox2.isChecked()));
editor.putBoolean("checkbox3_state", checkBox3.isChecked()));
editor.apply();
}
private void loadCheckboxesStates(){
SharedPreferences sharedPref = getActivity().getSharedPreferences("fileName",Context.MODE_PRIVATE);
boolean checkbox1State= sharedPref.getBoolean("checkbox1_state", false);
boolean checkbox2State= sharedPref.getBoolean("checkbox2_state", false);
boolean checkbox3State= sharedPref.getBoolean("checkbox3_state", false);
checkBox1.setChecked(checkbox1State);
checkBox2.setChecked(checkbox2State);
checkBox3.setChecked(checkbox3State);
}
now override onBackPressed and onDestroy methods and call saveCheckboxesStates like that:
#Override
public void onBackPressed() {
super.onBackPressed();
saveCheckboxesStates();
}//this handle the case when the user clicks back the activity
#Override
public void onDestroy () {
super.onDestroy();
saveCheckboxesStates();
}//this handle the case when your app gets killed
then call the method loadCheckboxesStates in your onCreate method and you are done.
how does one save a clicked state in android listview after exiting the app and restore state on app launch.The app should be able to listen to click event on listview and save the state and when the app is closed it saves the clicked state and then restore it on relaunch.
i have tried using getView but it doesnt seem to work as expected. please help
What you have to do is, you have to override the below method in your Activity,
#Override
public void onBackPressed() {
super.onBackPressed();
}
And save the state of your Button using SharedPrefrence, and next time when you enter your Activity get the value from the Sharedpreference and set the enabled state of your button accordingly.
Example,
private void SavePreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("state", button.isEnabled());
editor.commit(); // I missed to save the data to preference here,.
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Boolean state = sharedPreferences.getBoolean("state", false);
button.setEnabled(state);
}
#Override
public void onBackPressed() {
SavePreferences();
super.onBackPressed();
}
onCreate(Bundle savedInstanceState){
LoadPreferences();
//just a rough sketch of where you should load the data
}
I understand that the best way to save values is to use SharedPreferences e.g.
SharedPreferences Savesettings = getSharedPreferences("settingFile", MODE_PRIVATE);
SharedPreferences.Editor example = Savesettings.edit();
example.putString("Name", name)
.putInt("Age", age)
.putInt("Score", score)
example.apply();
But what if I want my program to remember a button being disabled or enabled after the user closes and opens the program? i have tried a RegisterOnChangePreferanceListener however i have no luck e.g.
SharedPreferences Preferences= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.OnSharedPreferenceChangeListener Example =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences preferance, String key) {
Name = name;//only an example not the main focus
Age = age;
Score = score;
enableBTN = false; //disables button
Name.setEnabled(false); //disables the edit text from further editing
}
};
Preferences.registerOnSharedPreferenceChangeListener(Example);
Is there a way to do this, both methods do not seem to be working for me.
You need to save it from within the button's OnClickListener. That way, everytime the button is clicked, you are guaranteed that the button's state is saved. button is a reference to a Button view object
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle bundle) {
Button button = (Button)findViewById("this_button_view_id");
EditText editText = (EditText)findViewById("this_edit_text_id");
SharedPreferences Savesettings = getSharedPreferences("settingFile", MODE_PRIVATE);
// If the Savesettings shared preferences above contains the "isButtonDisabled" key
// It means the user clicked and disabled the button before
// So we use that state instead
// If it does does not contain that key
// We set it to true so that the button is not disabled
// Same for the edit text
button.setEnabled(Savesettings.contains("isButtonDisabled") ? Savesettings.getBoolean("isButtonDisabled") : true);
editText.setEnabled(Savesettings.contains("isEditTextDisabled") ? Savesettings.getBoolean("isEditTextDisabled") : true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// disable the edit text
editText.setEnabled(false);
// disable the button
button.setEnabled(false);
SharedPreferences.Editor example = Savesettings.edit();
// Save the button state to the shared preferences retrieved above
example.putBoolean("isButtonDisabled", true);
// Save the edit text state to the shared preferences retrieved above
example.putBoolean("isEditTextDisabled", true);
example.apply();
}
});
}
}
I'm a rookie in android programming. I have a small problem. When I click a ImageView, I make that ImageView invisible and set a Button to visible. My problem is that how do you save this? For eg, I click the ImageView, Button shows up and ImageView disappears. And I exit the app and enter back into that same activity and I want that Button to remain there. How do I go about doing that?
Thanks!
Use SharedPreferences. here is a good tutorial on how to use them. example
But basically you are good to go by adding this code to your Activity
private boolean isVisible;
#Override
public void onCreate(Bundle myBundle){
super.onCreate(myBundle);
isVisible = getPreferences(MODE_PRIVATE).getBoolean("visible", true);
.... your code
if (isVisible){
// show ImageView
} else {
//don't
}
}
}
public void onPause(){
if(isFinishing()){
getPreferences(MODE_PRIVATE)
.edit().
putBoolean("visible", isVisible).commit();
}
}
Use a shared preference to save the state, i.e. say in your case a boolean value to indicate whether imageview was visible or not when you exit the app.
When you launch the app, use this value and accordingly perform the action.
For usage of shared preference,
How to use SharedPreferences in Android to store, fetch and edit values
you can store the state in shared preference when you leave your app onPause() or on the click event and can get result back on onCreate() method from that preferences
To store data in shared preference(in OnPause() or on the click event):
SharedPreferences prefs = getSharedPreferences("yourPrefName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// save values
editor.putBoolean("isButtonVisible", true);
editor.commit();
To get data from sharedPrefs(in onCreate()):
SharedPreferences prefs = getSharedPreferences("yourPrefName", MODE_PRIVATE);
boolean btnstatus = prefs.getBoolean(Constants.IS_LOGIN, false);
if (btnstatus) {
//put the code to show button and hide imageview
}
Is there XML attribute that does the exact opposite of android:dependency?
What I would like the dependent preference to be enabled when the other is NOT checked and disabled when it IS checked.
edit: maybe the issue isn't with android:dependency maybe there is an xml attribute that I can add to make the default for that preference disabled and then android:dependency will toggle it the opposite way like i want.
edit again:
I tried setting android:enabled="false" in the preference and it disables it like i want but even with it being dependent on the other preference it didn't enable it like i had hoped
Actually found it on my own and figured I'd just post it here to help anyone that might have this same issue:
android:disableDependentsState="true"
Put that in the controlling preference.
Dmytro Zarezenko asked what if you wanted some dependencies to be enabled when the preference on which they depend is true and some to be enabled when that preference is false.
Use the method described above to set the all the dependant preferences of one type (which ever have the greater number). Then (with the class having implements OnSharedPreferenceChangeListener) have code like this in the Preference Activity and/or Preference Fragment:
#Override
public void onResume()
{
super.onResume();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause()
{
super.onPause();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if (key.equals("pref_that_they_depend-upon")
{
// Iterate over the preferences that need to be enabled or disabled,
// lets say there is just one called the_awkward_one.
Preference preference = findPreference("the_awkward_one");
// Or preference.setEnabled(! sharedPreferences.getBoolean(("pref_that_they_depend-upon", defaultValue));
preference.setEnabled(sharedPreferences.getBoolean(("pref_that_they_depend-upon", defaultValue));
}
}
This is my code sample for doing this from code and not XML.
String eitherKey = "either";
String orKey = "or";
CheckBoxPreference either = new CheckBoxPreference(this);
either.setKey(eitherKey);
either.setTitle("Either");
either.setSummary("It is either one or");
either.setDefaultValue(false);
either.setDisableDependentsState(true);
inlinePrefCat.addPreference(either);
try
{
//Crossfade Time
CheckBoxPreference or = new CheckBoxPreference(this);
or.setKey(orKey);
or.setTitle("Or");
or.setSummary("the other");
inlinePrefCat.addPreference(or);
or.setDependency(eitherKey);
}
catch (Exception e)
{
}
I need to change value of dependent preference, so i post my code below, if anyone wants to do this:
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if(preference.getKey().equals("key_a")) {
((CheckBoxPreference)findPreference("key_b").setChecked(false);
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
Make your PreferenceActivity implement
SharedPreferences.OnSharedPreferenceChangeListener
declare in PreferenceActivity:
SharedPreferences prefs;
initialize in onCreate:
SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs = sPrefs;
and register on shared preference change listener
prefs.registerOnSharedPreferenceChangeListener(this);
do the same as Steve said in onResume and onPause methods.
implementation of onSharedPreferenceChanged listener:
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d("SettingsActivity","onSharedPreferenceChanged LISTENER FIRED");
if (key.equals(getString(R.string.key_call))) {
//if call true
if (sharedPreferences.getBoolean(getString(R.string.key_call), false)) {
Preference preference = findPreference(getString(R.string.key_record));
preference.setEnabled(false);
} else { // if call false
Preference preference = findPreference(getString(R.string.key_record));
preference.setEnabled(true);
}
}
if (key.equals(getString(R.string.key_record))) {
//if record true
if (sharedPreferences.getBoolean(getString(R.string.key_record), false)) {
Preference preference = findPreference(getString(R.string.key_call));
preference.setEnabled(false);
} else { // if record false
Preference preference = findPreference(getString(R.string.key_call));
preference.setEnabled(true);
}
}
}
In this case, I have 2 mutually exclusive Preferences in PreferenceActivity.
Call and Record.
When both are unchecked, both can be checked, but as user checks one of them, the other becomes disabled (greyed out).
As user unchecks the checked preference, the user can check the other one.
On both of them other preferences can depend and that can be worked out with android:dependancy attribute in XML file.