0

I'm using Sugar ORM and I have my Reminder model. I built the "New Reminder" activity manually and it turned out very ugly:New Reminder Activity

I really like the Preference API (https://developer.android.com/guide/topics/ui/settings.html) which could solve many of my issues. However, I don't need preferences to be saved with SharedPreferences, since I'm manually saving them to Sqlite via Sugar ORM.

Is there a way to use that UI anyway?

koichirose
  • 1,815
  • 4
  • 22
  • 30

1 Answers1

0

Override the persist* methods to save your values. If you check Preference class you can see that they are protected. You must also override the getPersisted* methods to suit your own needs.

Take for example EditTextPreference;

You would have to create your own class that extends it like so:

 public class MyCustomEditTextPreference extends EditTextPreference{
    //custom implementation here
 }

See more here

Peter Chaula
  • 3,456
  • 2
  • 28
  • 32
  • So I should make my activity extend Preference and override those two methods? Then I can use the XML layout? My activities already extend a BaseActivity which sets up toolbars etc, how can I do it cleanly? – koichirose Aug 22 '16 at 08:30
  • No. The `Preference` class is the actual "setting" component take a look at my **edit** – Peter Chaula Aug 22 '16 at 09:27
  • Could you please give me a couple other details to start working on it? Right now I have my EditReminderActivity with the 'saveReminder' method, that will go get values by referencing layout components (such as EditText or Switch or Spinner components, see screenshot). It will then call the Sugar ORM's 'save' method on the entity I'm saving. How should I edit it to use the Preference layout? The layout right now is a RelativeLayout inside a LinearLayout. – koichirose Aug 22 '16 at 09:40
  • For a custom layout, take a look a [this question](http://stackoverflow.com/questions/6194116/creating-a-custom-layout-for-preferences) – Peter Chaula Aug 22 '16 at 09:52
  • I don't really need to customize the default Preference layout, I just need to understand how to use it correctly in my Activity, while saving data with Sugar ORM instead of SharedPreferences. – koichirose Aug 22 '16 at 10:02
  • I haven't seen Sugar being used for settings. Please read the [Sugar docs](http://satyan.github.io/sugar/) thoroughly – Peter Chaula Aug 22 '16 at 10:25
  • What do you mean? I'm not using it for settings. I'd like to use the default android preference layout (just the layout), but then I'm saving a standard Sugar ORM Entity. – koichirose Aug 22 '16 at 10:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121531/discussion-between-laser-and-koichirose). – Peter Chaula Aug 22 '16 at 12:21