• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

SettingsActivity Questions

RhinoCan

Well-Known Member
Jul 5, 2015
184
64
I recently decided to add a few preferences to my app. In researching how to create the Settings, I read about the SettingsActivity template that comes with Android Studio and decided to give it a try. I was pleasantly surprised to see how much of the work it does for me. My preferences are, for the most part, working very well and doing what I want them to do and it took very little time to make that happen.

However, the downside seems to be that it is not clear to me how I can customize the Settings pages generated by the template. I don't mean changing the preferences themselves: that turns out to be pretty straight-forward. I have Text, Switch, and ListPreferences and they were easy enough to write. But I see no obvious ways to do some other things:
  • make the fonts different or bigger
  • change the colours of headings or items or the background colour of the settings pages
  • add "up" navigation so that I don't have to use the back button on the bezel of the phone (I had hoped to add a toolbar with up navigation enabled but I can't figure out how to add a Toolbar to a <PreferenceScreen>; it *looks* like an XML file but it won't tolerate a Toolbar being added to it
Do I just have to live without those things if I choose to use SettingsActivity? Or is there some way to do these things? If so, how?

Also, two other settings-related issues:

  1. One of my settings lets the user choose a date format - 2018-08-08 or 08/08/2018 for example - and I want that setting to take effect immediately. How do I make RecyclerView redraw itself upon returning from the Settings pages? Right now, the only way I can find to force a redraw is to click on the Add, Edit or Remove icons, then click Cancel when those activities start; when I come back from that, the RecyclerView is redrawn.
  2. Is there any way to make it impossible for a user to change a setting once set, unless given an override code by Tech Support (me)? One of the settings that they must set immediately upon using the app for the first time, is their first name/nickname. Once they've chosen that, I use that value on every sale to indicate that Beth or Tom made that sale. If they could change their user name in the Settings, they'd also change the records they're putting on the database. Now, I can simply ask them not to change the name once they choose it but users are not necessarily going to comply with that so I'm wondering if I can lock the setting value somehow - perhaps to be overridden if I give them a secret code of some kind - so that I know they've changed the user name and can deal with the consequences on the rest of the system.
 
One of my settings lets the user choose a date format - 2018-08-08 or 08/08/2018 for example - and I want that setting to take effect immediately. How do I make RecyclerView redraw itself upon returning from the Settings pages? Right now, the only way I can find to force a redraw is to click on the Add, Edit or Remove icons, then click Cancel when those activities start; when I come back from that, the RecyclerView is redrawn.

You change the data in the ArrayList used by RecyclerView list Adapter, and call notifyDataSetChanged()

Is there any way to make it impossible for a user to change a setting once set, unless given an override code by Tech Support (me)?

Wherever you're creating the Settings Activity in your code, just check if the authorisation has been received (a simple boolean variable will do). If false, then show an error message. If true, then continue to create the Settings Activity.
 
  • Like
Reactions: lunatic59
Upvote 0
You change the data in the ArrayList used by RecyclerView list Adapter, and call notifyDataSetChanged()

I'm already doing that. But the only way I get the data to re-display itself is to start one of the other activities and cancel. I must be doing the notify in the wrong place....

I check to see if Settings has been clicked in onOptionsItemSelected(); if it has, I launch the SettingsIntent, then the next line is the notify. Hmm. But the notify never executes, does it? As soon as the SettingsActivity ends, it goes back to the onCreate() of the main activity, doesn't it? How does my onCreate() determine that it's just back from SettingsActivity? Since I can't change the behaviour of the template-generated code, I can't tell it to pass anything back from the SettingsActivity either, can I?


Wherever you're creating the Settings Activity in your code, just check if the authorisation has been received (a simple boolean variable will do). If false, then show an error message. If true, then continue to create the Settings Activity.

I don't want to lock ALL of the settings, just the user name (and maybe the path to the database server); I have no problem with them changing the other things like the date format. Looks like I AM going to have to write my own Settings stuff to get the functionality I want and maybe sooner than later....
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones