Adding dynamic checkbox settings in Android and displaying them on the preferences screen?

I want to implement functionality in which the user can choose which group of elements to display using the general settings of the checkbox. To be precise, I will read the marked items from the settings and display.

Here is my preference class

public class Preferences extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //One way to add default preferences //addPreferencesFromResource(R.xml.prefs); //For now I prefer this setPreferenceScreen(defaultPref()); } // The first time application is launched this should be read private PreferenceScreen defaultPref() { PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); CheckBoxPreference checkboxPref = new CheckBoxPreference(this); checkboxPref.setKey("1"); checkboxPref.setTitle("SomeRandomStuff"); root.addPreference(checkboxPref); return root; } public showAllPreferences () { // TO SHOW ALL THE PREFERENCES BUT NOT SURE HOW TO DISPLAY THEM } } 

Now I canโ€™t understand how to add additional settings dynamically and display them in the settings window.

Here is the main activity class

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { setContentView(R.layout.main); }catch (Exception e) { e.printStackTrace(); } exView = (ExpandableListView) findViewById(R.id.expandableListView1); // STUFF TO ADD IN PREFERENCES editText = (EditText) findViewById(R.id.editText1); //BUTTON TO ADD PREFERENCES.(SEARCH TERM IS IDENTIFIED AND ADDED TO PREF) addButton = (ImageButton) findViewById(R.id.imageButton1); // BUTTON TO DISPLAY PREFERENCES prefButton = (ImageButton) findViewById(R.id.imageButtonPref); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences.Editor editor = settings.edit(); addButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub PrefObject obj = new PrefObject(); String key = Integer.toString(i); String title = editText.getText().toString(); //prefArray.add(obj); editor.putString(key, title); editor.commit(); i++ } }); prefButton.setOnClickListener(new OnClickListener() { // This method should show the preferences activity with new data @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Main.this, Preferences.class); startActivity(intent); // I know how to call the intent but I am not sure if // how to read the saved contents and display it Preferences pref = new Preferences(); pref.showAllPreferences(); } }); 
+4
source share
1 answer

AFAIK, there is no "visibility" option for the settings, which makes sense if you think that this is just a ListView.

See [ 1 ]. For what I see, you can do something like this:

 PreferenceScreen screen = this.getPreferenceScreen(); // Use "1" since you're using "1" to create it. CheckBoxPreference ckbox = (CheckBoxPreference) this.findPreference("1"); screen.removePreference(ckbox); 

To recreate, you can do this [ 2 ]:

 screen.addPreference(ckbox); 

Also, be sure to create your preferences using setOrder(int order) so that when you recreate it, it will be recreated in the correct position.

As you can see, it may be appropriate to maintain a global preference link to make it simpler and faster.

Of course, I do not need to say that you should integrate this logic into your CheckboxPreference listener. See this answer by none other than Reto Meyer himself to see a good way to do this (this is also a check box). There, it registers the listener full screen and checks what preference has caused the listener, but you can make it easier (but in more detail later) by simply setting it to setOnPreferenceChangeListener .

* edit: I see that you also use the button to add preference. You can also implement the same logic above in the button itself. It all depends on whether you want to do this using a check box or button.

Finally, it would be worth it to simply set the on state if you are not doing something like โ€œsee advanced settingsโ€ or something that is worth keeping novice users from having to do dangerous things for your application. But overall, inclusion states work better for users, IMHO.

Hope this answers your question.

+3
source

Source: https://habr.com/ru/post/1383315/


All Articles