I've been racking my brains all night but can't seem to accomplish this one small thing. I would like to add a SwitchPreference into my PreferenceActivity of an app. Below is a picture.
Before I say too much, my problem is exactly this: I cannot seem to put a listener on just the Switch part of the preference. I am able to set an onPreferenceTreeClick and an onPreferenceClick on the preference, and that works fine if I press on the text portion. But When the Switch itself does nothing when I change it from OFF to ON.
I've read the documentation on SwitchPreference. I also looked at the android/packages/Settings and it looks like AOSP uses a Switch and not a SwitchPreference for Wi-Fi and Bluetooth.
Here is my attempt (working if you press on the entire preference item, but not if you just press the Switch):
Sample:
public class Preferences extends SherlockPreferenceActivity {
public static final String PREF_THEME = "pref_theme_interface";
public static final String PREF_ROOT = "pref_root";
public static final String PREF_APP = "pref_app";
public static SharedPreferences mTheme;
private static SharedPreferences mUpdate;
public static SharedPreferences.Editor mEditor;
public boolean SDK_COMPAT = true;
boolean pSwitch = false;
boolean update = true;
Preference autoUpdate;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(MainActivity.THEME);
super.onCreate(savedInstanceState);
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.ic_preferences);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
SDK_COMPAT = false;
}
mUpdate = PreferenceManager.getDefaultSharedPreferences(this);
update = mUpdate.getBoolean("update", false);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
setPreferenceScreen(createPreferenceSDK());
}
private PreferenceScreen createPreferenceSDK() {
// Root
PreferenceScreen root = (PreferenceScreen)findPreference(PREF_ROOT);
PreferenceCategory prefApp = (PreferenceCategory)findPreference(PREF_APP);
//root.addPreference(prefApp);
if (SDK_COMPAT == true) {
pSwitch = true;
autoUpdate = new SwitchPreference(this);
autoUpdate.setKey("auto_update_pref");
autoUpdate.setTitle(R.string.auto_update);
//autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
prefApp.addPreference(autoUpdate);
} else {
pSwitch = false;
autoUpdate = new CheckBoxPreference(this);
autoUpdate.setKey("auto_update_pref");
autoUpdate.setTitle(R.string.auto_update);
autoUpdate.setSummary(R.string.auto_update_summary);
prefApp.addPreference(autoUpdate);
}
autoUpdate.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
mEditor = mUpdate.edit();
boolean checked = ((SwitchPreference) preference)
.isChecked();
if (checked) {
update = true;
mEditor.putBoolean("update", true);
mEditor.commit();
autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
} else {
update = false;
mEditor.putBoolean("update", false);
mEditor.commit();
autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
}
return true;
}
});
return root;
}
So to reiterate my question in case I lost you. How does one set a listener on the Switch portion of the SwitchPreference? Please be kind if it is something so obvious. It was pretty late last night when I tried to add this.
Thank you so much in advance.
Notes:
1. I am not opposed to sticking with the CheckBoxPreference, but I prefer to use Switch because it looks nice.
Yes I know there is an easier/better? way of adding dynamic preference using res/xml and res/xml-v14 instead of doing the SDK check. I just did that for testing.
picture of preference screen
EDIT
Hopefully this helps someone else! Thanks Tushar for suggestion :-)
autoUpdate.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
boolean switched = ((SwitchPreference) preference)
.isChecked();
update = !switched;
mEditor = mUpdate.edit();
mEditor.putBoolean("update", update);
mEditor.commit();
autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
return true;
}
});
Use setOnPreferenceChangeListener() instead of setOnPreferenceClickListener().
Working code
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
SwitchPreference vibrateSwitch = (SwitchPreference) findPreference("notification_vibrate");
if (vibrateSwitch != null) {
vibrateSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference arg0, Object isVibrateOnObject) {
boolean isVibrateOn = (Boolean) isVibrateOnObject;
if (isVibrateOn) {
Vibrator v = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(400);
}
return true;
}
});
}
}
}
If you still haven't figured out a good way for doing this I have figured something out that doesn't create multiple calls to onPreferenceChange which clicking the preference does. I wrote it in another question: dual functionality SwitchPreference.
Related
does someone know where the mistake is? There is an error in Android Studio.
The following is the code for now.
final String keyFirstTime = "keyFirstTime";
prefsEditor.putBoolean(keyFirstTime, false);
if (keyFirstTime = false) {
Thanks in advance.
keyFirstTime is a string (see comment)
you are PUTTING a value not getting a value
You are using an assignment in an if statement
You are comparing a STRING to a BOOLEAN
In Activity 1 you should have:
final String keyFirstTime = "keyFirstTime";
prefsEditor.putBoolean(keyFirstTime, false);
In Activity 2 you should have:
boolean firstTime = prefs.getBoolean(keyFirstTime, false); //you don't need the editor
if (firstTime) {
...
}
Please go here for a tutorial: https://developer.android.com/training/basics/data-storage/shared-preferences.html
EDIT Try doing this (stolen from here)
private static final String FIRST_RUN = "FIRST_RUN";
SharedPreferences prefs = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
prefs = getSharedPreferences(getApplicationContext().getPackageName(), MODE_PRIVATE);
}
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean(FIRST_RUN, true)) {
prefs.edit().putBoolean(FIRST_RUN, false).commit();
//call relevant function for first run
} else {
//call relevant function for every other run
}
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); prefs.edit().putBoolean("keyFirstTime", true).commit();
Now to get Boolean value you have to use
Boolean check = prefs.getBoolean("keyFirstTime", false);
Now you can check this way
if(check){ your code here }
I have declared my variable, 'changed' too null so that I can check if it changes when the edittext is changed,
The problem I think is, when I press either the save button or the cancel button, it will always produce the value null, as upon clicking the button it is still null. However, I thought that the textwatcher would listen to the EditText and even if nothing was changed in the EditText it would by default change the SetChanged() to false as it provided "live updates", however clearly this isn't the case, am I doing something wrong? or am I supposed to approach it in a different way?, is there some way of refreshing it?
Advise would be greatly appreciated.
(P.S Some code was deleted to reduce the size and make it look easy on the eye, so excuse me for any missing braces. Furthermore, the activity does run properly as it shows the layout.However upon pressing any of the buttons it causes it to crash.)
public class EditNewItemActivity extends AppCompatActivity{
private Boolean changed = null;
private TextView title,content;
private Button saveBtn,cancelBtn;
private String date;
private int id;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
title = (TextView) findViewById(R.id.editItemTitle);
content = (TextView) findViewById(R.id.editItemDescription);
saveBtn = (Button) findViewById(R.id.editItemSaveBtn);
cancelBtn = (Button) findViewById(R.id.editItemCancelBtn);
Bundle extras = getIntent().getExtras();
title.setText(extras.getString("title"));
content.setText(extras.getString("content"));
date = extras.getString("date");
id = extras.getInt("id");
GenericTextWatcher textWatcher = new GenericTextWatcher();
title.addTextChangedListener(textWatcher);
content.addTextChangedListener(textWatcher);
ClickEvent clickEvent = new ClickEvent();
saveBtn.setOnClickListener(clickEvent);
cancelBtn.setOnClickListener(clickEvent);
}
private class ClickEvent implements View.OnClickListener{
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.editItemSaveBtn:
save();
break;
case R.id.editItemCancelBtn:
cancel();
break;
}
}
}
private void cancel() {
if (getChanged() == null){
//This was used to simply verify that getchanged was still null.
}
if (title.getText().toString() != "" || content.getText().toString() != ""){
if (getChanged() == false) {
// if nothing has been changed let it cancel etc
}else {
}
}
}
private void save() {
if (tempTitle != "" || tempContent != "") {
if(getChanged() == true){
}
}
public Boolean getChanged() {
return changed;
}
public void setChanged(Boolean changed) {
this.changed = changed;
}
private class GenericTextWatcher implements TextWatcher{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.v("Beforetext:", s.toString());
EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(false);
}
#Override
public void afterTextChanged(Editable s) {
Log.v("afterTextChanged:", s.toString());
EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(true);
Log.v("Status:", editItem.getChanged().toString());
}
}
You had change the changed. But which you changed is in your new EditNewItemActivity not in your current page.
This is where you made mistake (beforeTextChanged and afterTextChanged in your GenericTextWatcher):
EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(false); //or true
You should just call:
setChanged(false); // or true
In fact, you should not new an activity yourself, activity must be create by the Android Framework so that it can be managed by the system.
On one Android phone's I'm getting a NullPointerException and on the other one I'm not. I'm trying to save multiple Checkbox states for my privacy settings. As I said on one of the phones it works fine, saves it to sharedPreferences as it should, but on my main phone it crashes as soon as I open the Activity. Anyone see the issue?
public class PrivacySettings extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox showAge,showLocation,showRelationship,showGender,showFacebookButton;
String TAG = getPackageName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_privacy_settings);
showAge=(CheckBox)findViewById(R.id.cbShowAge);
showAge.setChecked(getFromSP("cbShowAge"));
showAge.setOnCheckedChangeListener(this);
showLocation=(CheckBox)findViewById(R.id.cbShowLocation);
showLocation.setChecked(getFromSP("cbShowLocation"));
showLocation.setOnCheckedChangeListener(this);
showRelationship=(CheckBox)findViewById(R.id.cbShowRelationshipStatus);
showRelationship.setChecked(getFromSP("cbShowRelationship"));
showRelationship.setOnCheckedChangeListener(this);
showGender=(CheckBox)findViewById(R.id.cbShowGender);
showGender.setChecked(getFromSP("cbShowGender"));
showGender.setOnCheckedChangeListener(this);
showFacebookButton=(CheckBox)findViewById(R.id.cbShowFacebookLink);
showFacebookButton.setChecked(getFromSP("cbShowFacebookButton"));
showFacebookButton.setOnCheckedChangeListener(this);
setTitle("Privacy Settings");
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(TAG, android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(TAG, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_privacy_settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.cbShowAge:
saveInSp("cbShowAge",isChecked);
break;
case R.id.cbShowLocation:
saveInSp("cbShowLocation",isChecked);
break;
case R.id.cbShowRelationshipStatus:
saveInSp("cbShowRelationship",isChecked);
break;
case R.id.cbShowGender:
saveInSp("cbShowGender",isChecked);
break;
case R.id.cbShowFacebookLink:
saveInSp("cbShowFacebookButton",isChecked);
break;
}
}
Edit:
I changed the TAG name to "Different_name" just to test it and it worked. Can anyone explain as to why its working like this SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE); ?
Not sure did I get it properly (since there is no log posted as of now) but from your comment
SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE);
and your code
String TAG = getPackageName();
it seems your shared preference file name is returned as null on some devices, i.e. using getPackageName()
TAG == null
If you change your TAG to your activity name and it needs to be public and static:
public static final String TAG = PrivacySettings.class.getSimpleName();
you can use it from anywhere as:
SharedPreferences preferences = getContext().getSharedPreferences(PrivacySettings.TAG, Context.MODE_PRIVATE);
In short, it seems getPackageName() fails for some reason so just use a value which will never fail on any device, e.g. current class name
I'm trying to use a SearchView and I got everything to work, except when I want to search an empty string.
The onQueryTextChange does react when I remove the last character, but I want the user to be able to press the search button when the searchfield is empty.
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
I've also tried using a OnKeyListner. but it does not seem to work either.
searchView.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//Do something
return true;
}
});
This seems such a simple thing to do, but I can't get it to work. Any suggestions?
Edit
I have looked for a solution for a while now and just some minutes after posting this, I found a solution.
On this thread I found out this was not a bug, but it actually was deliberate.
Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string
So I just downloaded ActionBarSherlock and made some modification to the method onSubmitQuery()
From
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
}
And the modified version
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if(query == null) {query = "";}
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
Hope this helps if anyone else is having this problem.
Perhaps it's not suited for your app but you also have the option to use a simple EditText, then add a TextWatcher listener. This way you catch every input even if the user enters an empty string.
I had same issue with SearchView but I did not want to use ActionBarSherlock so I came up with solution which consists of styling your own SearchView as shown in guide http://novoda.com/blog/styling-the-actionbar-searchview/ and setting OnEditorActionListener for EditText( How do I trigger an action when the user has hit enter?) which is part of the SearchView.
final SearchView searchView = (SearchView)findViewById(R.id.search);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//Do something
}
return false;
});
I've searched long and far and found a few different ways people are dealing with removing a single and often specific tab from a TabHost object. I would like to try, if I may, to gather all those methods to this single question so that people my "shop" for the right method they need and hopefully get the answer they need to write their code; I also feel that it will cut down on the number of these questions.
At the moment I'm having trouble finding a solution to my code as well. I'm attempting to get rid of a particular tab without touching the others; hopefully I too will find my answer.
I wont pick a particular answer for this question for a while, so please just list the method you used to deal with this problem here for others to see.
Thank you.
Update:
Okay, here is my current code:
TabHost mTabHost;
TabHost.TabSpec spec;
Intent mSettingsIntent;
Intent mSearchIntent;
int tabNum = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSearchIntent = new Intent().setClass(this, SearchTab.class);
mTabHost = getTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
makeTab("Tab");
final Button newSearchBtn = (Button) findViewById(R.id.new_search_btn);
newSearchBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
EditText searchBar = (EditText) findViewById(R.id.search_bar);
final String searchString = searchBar.getText().toString();
makeTab(searchString);
}
});
final EditText edittext = (EditText) findViewById(R.id.search_bar);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
EditText searchBar = (EditText) findViewById(R.id.search_bar);
final String searchString = searchBar.getText().toString();
makeTab(searchString);
return true;
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tab_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_close:
// TODO: Find/define method to close a single tab
closeTab();
return true;
case R.id.menu_settings:
// TODO: Create a basic Settings Activity and call constructor here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void makeTab(String tabText) {
String tabTag = "Tab" + tabNum++;
View tabview = createTabView(mTabHost.getContext(), tabText);
spec = mTabHost.newTabSpec(tabTag).setIndicator(tabview).setContent(new Intent().setClass(this, SearchTab.class));
mTabHost.addTab(spec);
mTabHost.setCurrentTabByTag(tabTag);
}
private void closeTab() {
// TODO: Define method for closing a single tab with tabTag
mTabHost.removeViewAt(mTabHost.getCurrentTab());
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
My app locks up due to a NullPointer Exception I'm not sure why; I'm still trying to figure out how to read the debugger perspective in Eclispe for clues. I'll update when I have more.