I have a tabhost with three activities and I want to save the pressed state of the buttons of each activity
So now How can I save the pressed state of each button in all three child activities so that when I move from one activity to the other the button pressed state will be reflected on moving back. first activity -> all 4 buttons pressed -> go to 2nd activity -> come back to first activity -> all buttons in first activity should be in pressed state
When I go to second child tab and come to the first child tab the change(The buttons which I pressed are not in pressed state) is not reflecting
Help is always appreciated , Thanks
this is my code in first tabhost child activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
seatdirnbtn.setOnClickListener(listner1);
seatdirnbtn1.setOnClickListener(listner2);
seatdirnbtn.setPressed(true);
seatdirnbtn1.setPressed(true);
this.LoadPreferences();
}
private void SavePreferences() {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("sharedPreferences",MODE_WORLD_READABLE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("state", seatdirnbtn.isEnabled());
editor.putBoolean("state1", seatdirnbtn1.isEnabled());
editor.commit();
}
private void LoadPreferences() {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("sharedPreferences",MODE_WORLD_READABLE);
Boolean state = sharedPreferences.getBoolean("state", false);
Boolean state1 = sharedPreferences.getBoolean("state1", false);
seatdirnbtn.setPressed(state);
seatdirnbtn1.setPressed(state1);
}
#Override
protected void onStart() {
super.onStart();
LoadPreferences();
}
#Override
protected void onPause() {
SavePreferences();
super.onPause();
}
public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
} else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}
isclick = !isclick;
}
};
private View.OnClickListener listner2 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn1.setBackgroundResource(R.drawable.icon2hlt);
} else {
seatdirnbtn1.setBackgroundResource(R.drawable.icon2);
}
isclick = !isclick;
}
};
probably you should override onResume() method in which you should set buttons states. this method is called after onCreate() and even the activity is already created. If you have activities in tabHost they are not created each time you switch between tabs so onCreate() method will be called only once but onResume() every time you switch to tab with particular activity.
your code which is loading preferences is in onStart() method. Look here on activity lifecycle. You can see that this method is called only if your activity was stopped before but will never called if it was just paused.
EDIT:
if you have just 2 states like in your code from question it could be better to use ToggleButton which also generally have 2 states. You can style it to have different backgrounds for each state. This tutorial could be helpfull.
Than you will have a little bit different Listener:
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(checked) {
//do sth if it's checked
} else {
//do sth if it's not checked;
}
}
});
to change states for them programatically:
toggleButton.setChecked(true); //or false
so finally you can save this state to SharedPreferences:
editor.putBoolean("toggleButton1",toggleButton.isChecked());
and when you will need this state:
boolean isChecked = sharedPreferences.getBoolean("toggleButton1",false);
toggleButton.setChecked(isChecked);
selector will take care of switching button backgrounds for each state.
Related
I want to change the theme of my app by a switch button. There is no problem and the theme changes into dark or light as I hit the button. on the other hand, I want to change the background image as the theme changes in different activities. I mean the MainActivity has an image background and other activities have their own background image. I would be very grateful if there is a guy to help me.
this is my code:
private Switch mySwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
if(AppCompatDelegate.getDefaultNightMode() ==
AppCompatDelegate.MODE_NIGHT_YES){
setTheme(R.style.darkTheme);
}
else {setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwitch = (Switch) findViewById(R.id.my_switch);
if(AppCompatDelegate.getDefaultNightMode() ==
AppCompatDelegate.MODE_NIGHT_YES){
mySwitch.setChecked(true);
}else {
}
mySwitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
if(b){AppCompatDelegate.setDefaultNightMode
(AppCompatDelegate.MODE_NIGHT_YES);
restartApp();
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
restartApp();
}
}
});
}
public void restartApp(){
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
}
Saving content in sharedPref
private SharedPreferences mSharedPreferences;
public SharedCache(SharedPreferences sharedPreferences) {
mSharedPreferences = sharedPreferences;
}
public String getThemeName() {
return mSharedPreferences.getString(THEME_NAME, APP_DARK);
}
public void setThemeName(#NonNull String theme) {
mSharedPreferences.edit().putString(THEME_NAME, theme).apply();
}
Here AppDark is my current theme name , you can set your theme name onChange of your theme .
In Activity onCreate method , get the theme from sharedPref . Or you can follow the below link for better understanding on sharedPreference . https://medium.com/android-grid/android-sharedpreferences-example-writing-and-reading-values-7a677f4448c3
I am facing problem with toggle button state on onResume() and onPause() state.
Activity - A (first user toggle ON the button) then go back to Activity - B, then it will comeback to Activity - A then I want toggle Button is ON not OFF, how to handle this state in android.
By default Activity handles its components state which has an id attribute.
If it's not acting like that, you can use onSaveInstanceState and onRestoreInstanceState to handle components state manually:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("Toggle1", toggle.isChecked());
// etc.
}
And to restore the state:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean toggle1State = savedInstanceState.getBoolean("Toggle1");
toggle1.setCheched(toggle1State);
}
toggle_relative.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
#Override
public void onToggle(boolean on) {
if (on == true){
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("toggle_relative", true); // value to store
editor.commit();
Toast.makeText(getContext(),"Relatives will be notified in case of accidental situation",Toast.LENGTH_LONG).show();
}else {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("toggle_relative", false); // value to store
editor.commit();
}
}
});
#Override
public void onResume() {
super.onResume();
boolean boll_toggle_relative = preferences.getBoolean("toggle_relative", false); //default is true
if (boll_toggle_relative == true)
{
toggle_relative.setToggleOn();
}
else
{
toggle_relative.setToggleOff();
}
}
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
I'm doing an with a PreferenceActivity with two Fragments, each one containing a PreferenceScreen.
The thing I want to do is to create an event listener on a Custom preference that I have (a row of this custom Preference is a TextView with a SwitchView). I handle well the Switch preference, it keeps it saved as I want, but now what I want to do is add an event on the TextView between the SwitchView to show what I want on the other part of the screen (the second Fragment).
Let me show you my current code.
This is my CustomPreference (it's just a TextView and a Switch)
public class ItemPreference extends Preference{
private static TextView text; // this is the text at the left of the switch, it's where i want to handle the event
// to show an other preferencescreen in the fragment between
private Switch switcher; // the key of the preference is for the SWITCH !!
private boolean checked;
private Context context;
public TextView getCustomText(){
return text;
}
public ItemPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setLayoutResource(R.layout.row_setting); // the layout resource of my custom preference
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
text = (TextView)view.findViewById(R.id.buttonRowSetting);
// to display the text in the TextView between the Switch, I use the key of the Switch
if (this.getKey().equals("pref_key_classification"))
text.setText(R.string.title_activity_classification);
// the switchview and it's preference saving
switcher = (Switch)view.findViewById(R.id.switchRowSetting);
Boolean value = this.getPersistedBoolean(false);
if (value){
switcher.setChecked(true);
} else {
switcher.setChecked(false);
}
switcher.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setSwitchChecked(isChecked);
}
});
}
public void setSwitchChecked(boolean value) {
if (checked != value) {
checked = value;
persistBoolean(value);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getBoolean(index, false);
}
#Override
protected void onClick() {
super.onClick();
}
#Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
setSwitchChecked(restorePersistedValue ? getPersistedBoolean(checked) : (Boolean) defaultValue);
}
#Override
protected Parcelable onSaveInstanceState() {
return super.onSaveInstanceState();
}
#Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
}
}
This is the SettingsFragment, on the left part of the screen, where I want to handle the event to show what I want on the other part of the screen.
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings); // the left preferencefragment
Preference stat = (Preference) findPreference(getString(R.string.pref_key_statistic));
stat.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if(getActivity() instanceof OnSettingPageListener){
// if i click on this preference, this loads this preferencescreen on the other fragment, works well.
((OnSettingPageListener)getActivity()).onSettingPageChange(R.xml.settings_stat);
}
return true;
}
});
// this is the custompreference, i would like to handle here a listener on the TextView to display a specific preferencescreen on the other fragment
ItemPreference classif = (ItemPreference) findPreference(getString(R.string.pref_key_classification));
// i tried this, and i also tried to make my own Listener also, but doesn't works
classif.getCustomText().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getActivity() instanceof OnSettingPageListener){
// the content i want to load on the other fragment
((OnSettingPageListener)getActivity()).onSettingPageChange(R.xml.settings_classification);
}
}
});
}
// [...]
}
So I hope you understand what's my problem, it's just a matter of listener. Does someone has a solution?
I'm not sure if you want the event on your first fragment to show the second fragment (for further editing) or if you want the event to make a change visible to the second fragment.
Let's say you want the change to be visible to the second fragment:
Have your second fragment implement SharedPreference.OnSharedPreferenceChangeListener with method onSharedPreferenceChanged, implemented to do what you want to have happen when the preference in the first fragment changes.
In the second fragment's onResume method, call registerOnSharedPreferenceChangeListener on your SharedPreferences object.
In the second fragment's onPause method, call unregisterOnSharedPreferenceChangeListener on your SharedPreferences object.
Now, if you want to show the second fragment for editing the preference in the first fragment:
Give your preference a fragment attribute that declares the fragment class that edits your preference
Have your first fragment implement PreferenceFragment.OnPreferenceStartFragmentCallback with method onPreferenceStartFragment implemented to instantiate your second fragment and display it. Your PreferenceActivity subclass will invoke this callback when the preference is clicked.
I finally found a solution, my problem was the way the handle a listener on the textview of my custom preference. I followed the observer pattern and made it work on my own listener.
On my fragment I instantiated my CustomSwitchPreference (I just renamed the ItemPreference class you can see upstairs ^^). I called my own listener, you will see the code after.
CustomSwitchPreference classif = (CustomSwitchPreference)
findPreference(getString(R.string.pref_key_classification));
classif.setHandlerListener(new ItemPreferenceTextViewListener() {
#Override
public void onHandle(TextView textView) {
if (getActivity() instanceof OnSettingPageListener){
((OnSettingPageListener)getActivity())
.onSettingPageChange(R.xml.settings_classification);
}
}
});
And there we go for the listener in the CustomPreference class, first create an interface to made your own listener (you can write it in the same file as the CustomPreference class you are doing) :
interface ItemPreferenceTextViewListener {
// the function which is going to be called when we will use our listener
void onHandle(TextView textView);
}
And then in the CustomPreference class you do this :
// you can put the interface here if you want to make it visible
public class CustomSwitchPreference extends Preference {
private static TextView text;
// the key of the preference is for the SWITCH !! this activates or
// not the "game" on the mainscreen
private Switch switcher;
private boolean checked;
// ---------------------------------------------------------------------- \\
// create a listener in your own custom preference
ItemPreferenceTextViewListener itemPreferenceTextViewListener ;
// this is the function to handle the event on the textview of the
// custom preference item, creates the listener
public void setHandlerListener(ItemPreferenceTextViewListener listener) {
itemPreferenceTextViewListener = listener;
}
// to make the event happen on a textview
// (we will pass the right textview in the onBindView(....) )
protected void myEventListener(TextView textView) {
if(itemPreferenceTextViewListener!=null)
itemPreferenceTextViewListener.onHandle(textView);
}
// ---------------------------------------------------------------------- \\
/**
* #param context
* #param attrs
*/
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.row_setting);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
// the TextView at the right of the switch, this one
// handles a listener to show the other page preference
text = (TextView)view.findViewById(R.id.buttonRowSetting);
// I called it buttonRowSetting because it will handle an event like a button
// I set the text of this TextView with the
// key preference of the related switch
if (this.getKey().equals("pref_key_classification"))
text.setText(R.string.title_activity_classification);
else if (this.getKey().equals("pref_key_matching"))
text.setText(R.string.title_activity_matching);
else if (this.getKey().equals("pref_key_intruder"))
text.setText(R.string.title_activity_intruder);
else if (this.getKey().equals("pref_key_semantic"))
text.setText(R.string.title_activity_semantic);
// AND HERE it's where it happens, simply make a basic listener
// on the textview, and inside the onClick method, handle your own
// listener, on the TextView you initialized just before)
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// just call
itemPreferenceTextViewListener.onHandle(text);
}
});
// ---------------------------------------------------------------------- \\
// to keep the Switch saved in the preferences
switcher = (Switch)view.findViewById(R.id.switchRowSetting);
Boolean value = this.getPersistedBoolean(false);
if (value){
switcher.setChecked(true);
} else {
switcher.setChecked(false);
}
switcher.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
setSwitchChecked(isChecked);
}
});
// ---------------------------------------------------------------------- \\
}
public void setSwitchChecked(boolean value) {
if (checked != value) {
checked = value;
persistBoolean(value);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getBoolean(index, false);
}
#Override
protected void onClick() {
super.onClick();
}
#Override
protected void onSetInitialValue(boolean restorePersistedValue,
Object defaultValue) {
setSwitchChecked(restorePersistedValue
? getPersistedBoolean(checked) : (Boolean) defaultValue);
}
#Override
protected Parcelable onSaveInstanceState() {
return super.onSaveInstanceState();
}
#Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
}
}
Hope this helps, it was my mistake I did not called the onHandle method inside the myTextView.onClickListener. Hope this will help one to understand well how the observer pattern works.
I have a tab-host with three activities and in each activity I have buttons.
On each button press I am changing the background drawable resource
So now How can I save the pressed state of each button in all three child activities so that when I move from one activity to the other, the button pressed state will be reflected on moving back. first activity -> all 2 buttons pressed -> go to 2nd activity -> come back to first activity -> all buttons in first activity should be in pressed state with the drawable resource changed
I have two background Images.
button press -> highlited background image -> second time press same button ->changes to normal background Image
For the first time when I run the application and If I press the button the background image is getting changed. If I go to 2nd child activity and coming back so the button is in pressed state with changed drawable resource. so Its fine
for the second time If I press the same button and coming back to the same activity from second activity. here its not working
First child activity -> press button ->high lighted background image changes ->go to second tabhost child activity -> come back to first child activity -> button is showing the same background image after pressing(this is fine)
the problem is
now you press the same button in first child activity -> background image changes to normal ->go to second tabhost child activity -> come back to first child activity -> but the button background resource is in highlighted instead of normal
and even If I run the application once again the button is showing the highligted background image
the below code I am using in my first child activity
EDITED
public void onResume() {
super.onResume();
SharedPreferences sharedPreferences = this.getSharedPreferences(
"sharedPreferences", MODE_PRIVATE);
int which = sharedPreferences.getInt("sharedPreferences", 1);
switch (which)
{
case 1:
seatdirnbtn.setBackgroundResource(R.drawable.icon4); break;
case 2:
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt); break;
default:
seatdirnbtn.setBackgroundResource(R.drawable.icon4); break;
}
}
public void onPause() {
super.onPause();
boolean isclick = false;
SharedPreferences sharedPreferences = this.getSharedPreferences(
"sharedPreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
editor.putInt("sharedPreferences", 2).commit();
}
}
EDIT2
public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
} else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
}
isclick = !isclick;
}
};
Try this change:
if (isclick) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
editor.clear();
editor.putInt("sharedPreferences", 2).commit();
}
else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
editor.clear();
editor.putInt("sharedPreferences", 1).commit();
}
Here's what i meant:
boolean[] btnBool = new boolean[2]; // number of buttons
private View.OnClickListener listner1 = new View.OnClickListener() {
public void onClick(View v) {
toggleBtn(0); //zero refers to which index of the array for button button
setDrawables();
}
};
public void onResume() {
super.onResume();
toggleAllBtns();
setDrawables();
}
private void toggleBtn(int index) {
if(btnBool[index]) {
btnBool[index] = false;
} else {
btnBool[index] = true;
}
}
private void toggleAllBtns() {
// first switch the values of the array
for (int i = 0; i < btnBool.length; i++) {
if(btnBool[i]) {
btnBool[i] = false;
} else {
btnBool[i] = true;
}
}
}
private void setDrawables() {
// then set drawables based on values
if (btnBool[0]) {
seatdirnbtn.setBackgroundResource(R.drawable.icon4);
} else {
seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
}
}