How to remove Android preferences from the screen?

Okay the issue in my case was that my preferences were separated into categories and when the preferences are categorized you can't simply ". RemovePreference" like we were trying to do. I first had to reference the category containing the preference I wanted to remove and then remove that preference from that category like so public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { private static final String POLLING_PREFERENCE = "update_frequency_list"; @Override protected void onCreate(Bundle savedInstanceState) { super.

OnCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml. Preferences); // Get a reference to the preferences mPollPref = getPreferenceScreen(). FindPreference(POLLING_PREFERENCE); //If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead) if(Build.VERSION.

SDK_INT > 7) { PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference("notifications_category"); notificationsCategory. RemovePreference(mPollPref); } } .... } I assume this has something to do with the ability to have multiple preferences with the same key (in order to remove the correct one). Anyway thank you Vladimir for sticking with me.

Definitely wouldn't have found the answer without you.

Okay the issue in my case was that my preferences were separated into categories and when the preferences are categorized you can't simply ". RemovePreference" like we were trying to do. I first had to reference the category containing the preference I wanted to remove and then remove that preference from that category like so.. public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { private static final String POLLING_PREFERENCE = "update_frequency_list"; @Override protected void onCreate(Bundle savedInstanceState) { super.

OnCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml. Preferences); // Get a reference to the preferences mPollPref = getPreferenceScreen(). FindPreference(POLLING_PREFERENCE); //If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead) if(Build.VERSION.

SDK_INT > 7) { PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference("notifications_category"); notificationsCategory. RemovePreference(mPollPref); } } .... } I assume this has something to do with the ability to have multiple preferences with the same key (in order to remove the correct one). Anyway thank you Vladimir for sticking with me.

Definitely wouldn't have found the answer without you.

1 Thanks this solved the problem for me! Removing the preference from it's PreferenceCategory works fine! – stealthcopter Jan 30 at 22:17.

Code seems to be right. Tell you that the following code works, just tested it: package com.lid.ps.screens. Edit_activity; import android.app.

AlertDialog; import android.app. DatePickerDialog; import android.content. Context; import android.content.

DialogInterface; import android.os. Bundle; import android.preference. ; import android.util.

Log; import android.view. LayoutInflater; import android.view. Menu; import android.view.

MenuItem; import android.view. View; import android.widget. ; import com.lid.ps.bean.

AbstractData; import com.lid.ps.bean. Action; import com.lid.ps.bean. ActionFactory; import com.lid.ps.bean.

CommonAlertDialog; import com.lid.ps.bean.utils. DateTools; import com.lid.ps.model. Activity; import com.lid.ps.model.

ActivityImpl; import com.lid.ps.screens. R; import java.util. Calendar; /** * User: Vladimir Ivanov * Date: 04.07.2010 * Time: 9:58:17 */ public class EditActivityScreen extends PreferenceActivity implements DatePickerDialog.

OnDateSetListener, AbstractData { private static final int MENU_DELETE_ACTIVITY = 0; private boolean create = true; public boolean isCreate() { return create; } private Activity dataContainer; private EditTextPreference activityNamePref; private EditTextPreference activityDescPref; private Preference startDatePref; private CheckBoxPreference hiddenPref; private int year; private int monthOfYear; private int dayOfMonth; @Override public void onCreate(Bundle savedInstanceState) { super. OnCreate(savedInstanceState); addPreferencesFromResource(R.xml. Activity_prefs); //this.

SetContentView(R.layout. Edit_screen); activityNamePref = (EditTextPreference) findPreference("name"); activityDescPref = (EditTextPreference) findPreference("desc"); // We have to do this to get the save/cancel buttons to highlight on // their own. GetListView().

SetItemsCanFocus(true); // Grab the content view so we can modify it. FrameLayout content = (FrameLayout) getWindow().getDecorView() . FindViewById(com.lid.

Ps0. Content); // Get the main ListView and remove it from the content view. ListView lv = getListView(); content.

RemoveView(lv); // Create the new LinearLayout that will become the content view and // make it vertical. LinearLayout ll = new LinearLayout(this); ll. SetOrientation(LinearLayout.

VERTICAL); // Have the ListView expand to fill the screen minus the save/cancel // buttons.LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams( LinearLayout.LayoutParams.

FILL_PARENT, LinearLayout.LayoutParams. WRAP_CONTENT); lp. Weight = 1; ll.

AddView(lv, lp); // Inflate the buttons onto the LinearLayout. View v = LayoutInflater. From(this).

Inflate( R.layout. Edit_screen, ll); startDatePref = findPreference("time"); hiddenPref = (CheckBoxPreference) findPreference("hidden"); // Initialize buttons Button cancelButton = (Button) v. FindViewById(com.lid.

Ps1. Cancel_activity); Button saveButton = (Button) v. FindViewById(com.lid.

Ps1. Save_activity); cancelButton. SetOnClickListener(new CancelOnClickListener()); saveButton.

SetOnClickListener(new SaveOnClickListener()); setContentView(ll); // if edit mode... if (getIntent()! = null && getIntent().getExtras()! = null) { // some stuff } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if (preference == startDatePref) { new DatePickerDialog(this, this, year, monthOfYear, dayOfMonth).show(); } return super.

OnPreferenceTreeClick(preferenceScreen, preference); } private void popAlarmSetToast(Context context) { String toastText; //... final Toast toast = Toast. MakeText(context, toastText, Toast. LENGTH_LONG); toast.show(); } /** * updates the preference summary.

* * @param datePicker date picker * @param I year * @param i1 month * @param i2 day */ @Override public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { year = i; monthOfYear = i1; dayOfMonth = i2; updateDate(); } private void updateDate() { startDatePref. SetSummary( new StringBuilder() // Month is 0 based so add 1 . Append(year).

Append("-") . Append(monthOfYear + 1). Append("-") .

Append(dayOfMonth). Append(" ")); } private class CancelOnClickListener implements View. OnClickListener { public void onClick(View view) { // go back to the previous page final Action action = ActionFactory.

CreateAction("back_edit_activity"); if (action! = null) { action. DoAction(EditActivityScreen.

This); } } } private class SaveOnClickListener implements View. OnClickListener { public void onClick(View view) { // validation if (activityNamePref.getSummary() == null || activityNamePref.getSummary().length() == 0) { AlertDialog dialog = new AlertDialog. Builder(EditActivityScreen.

This).create(); dialog. SetTitle(R.string. Validation_failed); dialog.

SetMessage(EditActivityScreen.this.getResources(). GetString(R.string. Activity_create_fail)); dialog.

SetButton(AlertDialog. BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); dialog.show(); } else { final Action action = ActionFactory. CreateAction("save_edit_activity"); if (action!

= null) { action. DoAction(EditActivityScreen. This); } popAlarmSetToast(EditActivityScreen.

This); } } } } What about your check for sdk build? Does the code really go under the condition? And please, check the name of your preference.

May be you haven't got it with findPreference method.

Thanks for your quick response. Yes, before posting I actually had the code printing the result of the removal (which returns a boolean) and it's giving me a false. I removed the if-statement just to eliminate any variables and I also double checked and tried different preferences with the same result.

Any chance I could see your entire class that you tested it with? I have no idea why it's not working for me. – honeal Nov 2 '10 at 20:59 Edited the answer.

– Vladimir Ivanov Nov 3 '10 at 7:56 Thanks dude, helped tons. You had no way of knowing what exactly was going wrong without seeing my XML first. Thanks again.

I would promote your answer if I was above 15. – honeal Nov 3 '10 at 15:51 great that you have solved your problem and thanks for posting the answer. Now you have reputation to promote.

– Vladimir Ivanov Nov 3 '10 at 15:56.

Get the main ListView and remove it from the content view. // make it vertical. LayoutParams lp = new LinearLayout.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions