preference activity UI for settings - android

I'm beginner in android , now I'm building an android application. .
I'm working in setting screen now and in my research i have seen so many applications that is using preference activity for the settings.So i started my setting page using preference activity. But in my case , my application settings have some options that don't need to save as preference. And that are also connecting with server and the EditTextPreference dialog will be also different. So i got confused in this case.
So what should i do? Do i have to change this to a LinearLayout?
Please help

The preference compat fragment and activity is there to make it simple to create an interface and save it to SharePreference. but you can't change much of the thing. I believe the sharepreference compat and component is itself has the listener when is changing. So let it save to preference even if you don't need it probably Ok. Also if your mockup or design for EditTextPreference is look different from native android then You can try use theme and style to change the look and feel of EditText just for preference only.
Also, You probably can use scrollview + constrainlayout or linearlayout to build it but you're make extra work such as save/load to/from preference, UI like sub preference ...etc. I would sugguest to go with Theme and Style but if your UI is completely different and can't be adapted with Preference then it left no choice. Constraint Layout would be better than LinearLayout.

The PreferenceFragment attached to a preference activity is the ideal way to have a settings screen. You will however need to implement the PreferenceChangeListener in the PreferenceFragment to listen for preference changes and save the changes. If you have options that do not need to be saved you can still add them to the xml and make them disabled by setting android:enabled = false.
EditTextPreferences can have their own UI feel by using the android:theme = #style/yourstyle and specifying the style you want in your styles.xml file
Goodluck.

Related

What is a good approach on saving settings files to change background themes for example?

I have been doing some research and I can't find how it's done, I want to make an activity with settings and files for my app and those will change colors, dark theme, buttons etc.
Also I have some doubts on how should I apply those changes, first thing that comes in mind is onCreate but since that would need a reload what is a good way to do this for example changing font color to white and background to black for a dark theme option ?
Another doubt is on changing button positions. Let's say I want 4 buttons positions, I could make all those 4 buttons and set views to GONE but that looks like a bad idea in terms of performance no ? If it is a bad idea or is there a better way to do that ?
I have a recycler view loaded from a sqlite database.
I saw this documentation : https://developer.android.com/training/data-storage/shared-preferences But still have some doubts, lets say button position 1234 and dark theme true/false.
How should I store one boolean and one Int in separated files? Or I should store a single text file read it and load it into my app like 1t for position 1 and dark theme true ?
I want to work on a very complete settings activity for my app.
I really appreciate any help or suggestions you can provide.
read up on AppCompatDelegate.setDefaultNightMode() as one way of changing themes and colors dynamically, based on the time of day for the user (day mode/night mode). I think you are over-complicating the problem a bit as well, simply create different layout files and styles for the different screens you want to show and then use them as you need them, instead of trying to do everything with one file.
Create different styles for different "themes" you want.
Here's an example of how you can create different themes in a "settings" page and change it in real time.
https://github.com/RoudyK/DemoApp
Basically the settings fragment is a preference fragment and each string maps to a style enum which references one of the styles defines in xml.
Androidx setting activity solved my problem since it already comes with shared preferences.
You can acces the preferences and use if to load different layouts or you can try to use some themes but all I found about themes was they were outdated.
to deal with preferences outside settings activity
open
val preference = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE)
val layoutType = preference.getInt("layout_type", 0)
edit
val preference = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE)
val editor = preference.edit()
val layoutType = preference.getInt("layout_type", 0)
editor.putInt("layout_type",0)
editor.apply()
about androidx activity
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
and use keys inside root_preferences.xml
booleanbla = prefs.getBoolean("some_key", false)
If u have any doubts leave a comment ill edit the answer.

Create Activity that looks like PreferenceActivity

I need to make an Activity that will replace the PreferenceActivity. The reason is that we need to save the settings in the SQL. I need a way to style my settings screen to look exactly like the PreferencesActivity. Can someone give me a link to the code of the PreferenceActivity layout code on Android AOSP? or maybe how can I achieve that?

Android Settings app preference files

I've read Android's settings tutorial, and want to use the new "headers" feature in order to accomplish something like the left image. To be exact, I want to have a global "enabled" checkbox, and below it some preference headers surrounded with a category header.
Can I make it be just by using XMLs ?
Yes it is possible by writing your own adapter class that handles headers. I found this nice pretty straightforward example for native settings looking like preference.
It's working on handset but it's not fixed for tablet (app crashes as it tries to use two-pane layout).

Is there UISwitch settings control in Android like iOS

I took a look at the preference activity that Android use for settings. i'm looking for something like UISwitch as shown in below image, does Android have one like this.
I can see the nearest control on Android preference settings is "list preference"
There is ToggleButton and CompoundButton (one extends the other).
But those have a different default display widget then what you are looking for. However either could be used along with your own drawable resources to create the "switch" type control that you are after.
But by default no the system does not include a widget that serves this function and looks the way you want it to.
No, In Android its called ToggleButton.
But still it can be developed by extending RadioButton placed in RadioGroup and giving its UI way you wanted.

How to make a preference more fancy?

I have been tired apply a style or a layout to a preference, but it didn't work well.
Could anyone help me with this?
I also find someone say it could be done in listview,But I wanna how to make those blank spaces between two block?
Please, help me with this , it has been puzzled me a long time.Thanks!
You can have a transparent divider between ListItems
android:dividerHeight="10 dip"
You can group your preferences by using PreferenceCategories and PreferenceScreens. If you want a preference to have an icon associated with it, you can use the android:icon attribute on the preference. Are there any specific characteristics of the iPhone settings that you're trying to replicate. You're not going to get your preferences to look exactly like the iPhones setting page, but you can certainly mimic certain characteristics.

Categories

Resources