Dynamic Tab Removal/Deletion in Tabhost - java

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.

Related

How to update method to return updated information from textwatcher after pressing button

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.

How to play a specific sound for a Listview item when it is clicked

I am creating an android dictionary app with sounds... I have listview, when an item is selected, a new activity open, inside the new activity contains 4 textviews and an image button, the textviews function perfectly but the image button was not. The audio files are placed in raw folder. How can I put the specific sounds of an item that was clicked?
Here's the code:
MainActivityJava
public class MainActivity extends AppCompatActivity {
ListView lv;
SearchView sv;
String[] tagalog= new String[] {"alaala (png.)","araw (png.)","baliw (png.)","basura (png.)",
"kaibigan (png.)","kakatuwa (pu.)", "kasunduan (png.)","dambuhala (png.)",
"dulo (png.)","gawin (pd.)","guni-guni (png.)","hagdan (png.)","hintay (pd.)",
"idlip (png.)","maganda (pu.)","masarap (pu.)", "matalino (pu.)"};
int[] sounds= new int[]{R.raw.alaala,
R.raw.araw,
R.raw.baliw,
R.raw.basura,
R.raw.kaibigan,
R.raw.kakatuwa,
R.raw.kasunduan,
};
ArrayAdapter<String> adapter;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
sv = (SearchView) findViewById(R.id.searchView1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tagalog);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tagword =tagalog[position];
String[] definition = getResources().getStringArray(R.array.definition);
final String definitionlabel = definition[position];
String[] cuyuno = getResources().getStringArray(R.array.cuyuno);
final String cuyunodefinition = cuyuno[position];
String[] english = getResources().getStringArray(R.array.english);
final String englishdefinition = english[position];
Intent intent = new Intent(getApplicationContext(), DefinitionActivity.class);
intent.putExtra("tagword", tagword);
intent.putExtra("definitionlabel", definitionlabel);
intent.putExtra("cuyunodefinition",cuyunodefinition);
intent.putExtra("englishdefinition", englishdefinition);
startActivity(intent);
}
});
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
DefinitionActivity.java
public class DefinitionActivity extends AppCompatActivity {
MediaPlayer mp;
String tagalogword;
String worddefinition;
String cuyunoword;
String englishword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_definition);
TextView wordtv = (TextView) findViewById(R.id.wordtv);
TextView definitiontv = (TextView) findViewById(R.id.definitiontv);
TextView cuyunotv = (TextView) findViewById(R.id.cuyunotv);
TextView englishtv = (TextView) findViewById(R.id.englishtv);
ImageButton playbtn = (ImageButton) findViewById(R.id.playbtn);
final Bundle extras = getIntent().getExtras();
if (extras != null) {
tagalogword = extras.getString("tagword");
wordtv.setText(tagalogword);
worddefinition = extras.getString("definitionlabel");
definitiontv.setText(worddefinition);
cuyunoword = extras.getString("cuyunodefinition");
cuyunotv.setText(cuyunoword);
englishword = extras.getString("englishdefinition");
englishtv.setText(englishword);
}
playbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
you can pass the raw id in the intent extra and play it on meadiaPlayer
What you want to accomplish is pretty simple.
you can ofcourse pass the id.
But I created this method for your case you can paste it in your activity or class and make a call to it. In my case, I put this method in a class that holds all the common functions, methods, strings, etc. The choice is yours :
public static void playDisSound(Context c, int soundID){
//Play short tune
MediaPlayer mediaPlayer = MediaPlayer.create(c, soundID);
mediaPlayer.setOnCompletionListener( new OnCompletionListener(){
#Override
public void onCompletion( MediaPlayer mp){
mp.release();
}
});
mediaPlayer.start();
}
And this is how to use it in your case :
Example I want to play an audio track from :
int[] sounds= new int[]{R.raw.alaala,
R.raw.araw,
R.raw.baliw,
R.raw.basura,
R.raw.kaibigan,
R.raw.kakatuwa,
R.raw.kasunduan,
};
So I just do :
//TODO ~ pls. remember to define context inside "onCreate" as
//call this before "onCreate"
Context context;
//And do this inside "onCreate" :
context = getApplicationContext();
OR
context = MainActivity.this;
//Then here comes the solution, just make a call to the playDisSound method with the id , in this case the "sounds[postion_referencer_i]"
playDisSound(context, sounds[postion_referencer_i]);
//And now on the question of what your "position_referencer_i" would be .... it also depends on how you intend to pass the id.
Are your going to make a match between the position picked and the position of the sound. It depends on you. But I would have created a set of integers to signify which try I want to play and do a matching simple calculation between the position picked for the item clicked to arrive at the position_referencer_id.
//But simply : note that in your array if I want to play for example "R.raw.baliw" I would just call :
playDisSound(context, R.raw.baliw);
I hope this works perfectly for you. So if I elaborated too much. Do let me know if you may need to stream the sound so I would just paste/send you a very cool method I have been using here in an app am working.
//FINALLY PLS. Remember this : this method would play the sound alright but it wont hesitate to play the sound all over again if you repeat the process. So do remember to check if the sound did play and finished before allowing the user to repeat, if not it could lead to repeated or kind of two speakers playing from the same song but at different time. (And the user may start to think that there is problem with the app. Pls. be very logical and sensitive in using this method)
In solving that, you can disable the button or the UI element that initiates the sound playing until the sound has finished playing, by way of monitoring duration of the track (which I am sure you should know and inculcate into your logic or by simply listening if sound is already playing)
All the best. Era. :)

Getting NullPointerException with Checkbox

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

How to use onClickListener and onMenuSelectListener in mainactivity?

Hello im trying to use onclicklistener along with onmenuselectlistener in my mainactivity class and the program runs but selecting the popup menu opions dont work they wont set the text to want it want anyone have any idea's? I know i did not implement onMenuSelectListener and maybe thats my problem if it is is there any other way to makes this work?
Here is my code:
public class MainActivity extends Activity implements OnClickListener {
// init variables
Handler uiHandler;
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
DrawingUtils callDU = new DrawingUtils();
DrawingTools callDT = new DrawingTools();
EditTools callET = new EditTools();
Conversion callConversion = new Conversion();
GLSurfaceView mGLSurface;
String Tag = "Debug";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurface = new GLSurfaceView(this);
mGLSurface.setRenderer(new BasicRenderer());
setContentView(R.layout.canvas);
FrameLayout v = (FrameLayout) findViewById(R.id.canvas);
v.addView(mGLSurface);
// init views and buttons
info = (TextView) findViewById(R.id.info);
enter = (Button) findViewById(R.id.enter);
line = (Button) findViewById(R.id.line);
arc = (Button) findViewById(R.id.arc);
cl = (EditText) findViewById(R.id.cl);
/*
* Handler for Main Thread uiHandler = new Handler() { public void
* handleMessage(Message msg) { switch (msg.what) {
*
* } Bundle bundle = msg.getData(); String string1 =
* bundle.getString("P1Key"); String string2 =
* bundle.getString("P2Key"); info.setText(string1);
* info.setText(string2); } };
*/
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.enter:
break;
case R.id.line:
break;
case R.id.arc:
break;
}
};
public void CreatePopupMenu(View v) {
PopupMenu mypopupmenu = new PopupMenu(this, v);
MenuInflater inflater = mypopupmenu.getMenuInflater();
inflater.inflate(R.menu.filemenu, mypopupmenu.getMenu());
mypopupmenu.show();
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.newCanas:
info.setText("New");
Log.d(Tag, "New was clicked");
break;
case R.id.open:
break;
case R.id.save:
break;
}
return super.onMenuItemSelected(featureId, item);
}
}
It doesn't look like you're attaching onClickListeners to anything. What that means is you're saying "whenever the onClick event is fired with me as the target, perform this action". But then you're never making yourself a target. Try adding the following code to your onCreate.
arc.setOnClickListener(this);
line.setOnClickListener(this);
enter.setOnClickListener(this);
The same thing happens with the PopupMenu it appears. Try adding mypopupmenu.addOnMenuItemSelectListener(this) right after you inflate the layout for your menu.
Jonathan is right. You should add .setOnClickListener(this); to each of the buttons added after you create them.
For the menu items though, you have to do the following:
1) Create a layout with the items on your menu and store it in your res/menu/ directory.
Example:
main.xml
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
2) Override the method called: onCreateOptionsMenu() to populate the items in the menu.
Example:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
3) Override the method onOptionsItemSelected() to do whatever you want with a switch like you were doing with the actionListeners.
4) Additionally, you could also override the method onPrepareOptionsMenu() which is the same as the previous one, but it is called every time the menu opens.
Good luck

SwitchPreference onChecked/onClick Listener

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.

Categories

Resources