Android Switch. On or Off without user selection - android

Is there away to change the state of the switch, either on or off without the user pressing it?
http://developer.android.com/reference/android/preference/SwitchPreference.html

Take a look on the inherited methods from class android.preference.TwoStatePreference, there you'll find setChecked method:
public void setChecked (boolean checked)
It sets the checked state and saves it to the SharedPreferences. So SwitchPreference.setChecked(true) and SwitchPreference.setChecked(false) should work fine for you.

If you want to do it programatically...
setChecked(true);
setChecked(false);
This is inherited from the superclass TwoStatePreference.
http://developer.android.com/reference/android/preference/TwoStatePreference.html#setChecked(boolean)

Related

SwitchPreference not changing state after setting onPreferenceChangeListener

I have an activity which extends PreferenceActivity and i have a class which extends PreferenceFragment.
After I initiate the switchPreference variable (inside the Fragment) I set OnPreferenceChangeListener to it.
The problem is when I do set a listener I cant change the state of the switch button (it remain at the same position).
If I disable the statement where Im setting the listener the switch button works fine and also the state is saved.
I also have a wrapper class for the sharedPreferences which I want to save a data into it on switchpreference change.
Did anyone encounter such a weird behavior?
Any help will be appreciate.
Well, I found the the "onPreferenceChange" function returned false and when I changed it to true it worked.
Thanks to all who was looking for answering me.

Reflect Settings change in View on update

In my Android app, I use SharedPreferences to let the user manage some settings. Now after a user changes any setting, and returning to the app from the settings page, I want my Views (Fragments) to use the latest values from SharedPreferences.
The changes could include reloading a Custom View to use a color scheme or remove filtering for a List View.
Currently only when the app is restarted, the required changes are applied. I am convinced that there is a way to solve my problem, but I am unable to figure it out.
Assume that I am supporting Android 2.2 and above, so that any newer APIs for this may not be used unless its present inside the Support Library.
Not exactly sure what your question is but it seems to me that when your user changes a setting and then hits the back button to return to the fragment you are not seeing the changes? if this is the case it is because when the user goes back android reinstates the version of the fragment that was on the stack (the one before any changes were made). My suggestion would be to try moving the loading of the new shared prefs to the onResume method for the fragment. This way they should be loaded when the user goes back.
Try this
#Override
public void onResume(){
super.onResume();
setContentView(R.layout.currentFrag);
}
this should reload the page with the correct changes.
You need to use the onWindowFocusChanged Method to check if the current activity has lost focus.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// Add the code to perform on the fragments after the settings have changed.
}

Enabling EditText OnClick when it is already disabled

I've noticed that I cannot use the EditText.setOnClickListener() when EditText.setEnabled(false) has been called before.
Is there any way to enable the EditText after clicking on it?
By calling EditText.setEnabled(false) you are disabling the EditText So what will you do by implementing EditText.setOnClickListener(), You can't do like this
public void setEnabled (boolean enabled) --
Set the enabled state of this view. The interpretation of the enabled state varies by subclass.
See the official Docs --
http://developer.android.com/reference/android/view/View.html#setEnabled%28boolean%29
If you want to use EditText.setOnClickListener()
then write EditText.setEnabled(true) before EditText.setOnClickListener()

CAB "check" button

How can we handle the check button in the CAB menu?
Is the item associated to some internal id like android.R.id.checkbox?
I've read that it could be handled in SherlockActionBar CAB, but could it be the same with the native ActionBar CAB?
Is there any way to detect the interaction of this item? onActionModeFinished() is not sufficient since I'm calling it multiple times since the CAB needs to be present due to previous changes that happened.
Thanks.
Ok, finally found a solution after trying some things.
Place the code you want for the checkbox or onbackpressed in the method below
public void onDestroyActionMode(ActionMode mode)
{
//place the code you want for the checkbox or back icon here. If you don't want
// this code run if other selections are used, then just create a boolean value that
//you earlier on and check the value in this section before implementing the code
}
};

Android Spinner OnItemChange Vs OnItemSelect events

In Android Spinner, I can set selection to a particular Item in the code (using setSelection(int)). This will end up calling OnItemSelectedListener, which is fine.
But when the user selects an item from the screen, by clicking the spinner and the item, I want to handle this as a different event, because the logic in my application should do different things.
How to achieve this?
To summarize, My Spinner should do this
If Value set from the code {
//do this..
}
else if user select a value {
//do that..
}
Extend Spinner and override setSelection(int). Insert functionality you want to happen in this case and then call super.setSelection(int) to keep the Spinner working normally.
I ended up using a global variable to indicate me who is calling the onItemSelected method. Looks like solved my purpose. But any better solutions are welcome.

Categories

Resources