Setting/Changing, storing and reusing a custom gradient drawable programmatically - android

i am trying to let the user of my app customize its appearance. The user can set the gradient values for backgrounds, buttons and other controls/views. This is working quite well but i dont get an idea how to save and later retrieve the setting i.e. for a customized background setting.
Is there a way to save the gradient drawables setting in shared preferences or save these things as xml drawable and then let the app use that special customized drawable background ? Or are there better approches to reach that goal? Perhaps saving the settings in a database?
I thought of two drawable xml files for the apps backround (default_background.xml and custom_background.xml) and to choose between those programtically if a boolean pref is set to use custom colors is true...
But it doesnt seem to be possible to modify the custom file at runtime.
i've searched a lot but dont seem to find a proper answer... edit:(or cant find/imagine the right search terms)
perhaps someone here has an idea?
regards
Marco
edit: to perhaps better understand what i am trying to do
normally i define the background of the view/activity, lets say a linear_layout ( main.xml), by creating a drawable xml (bg.xml) file and then set the background property to bg.xml
now the application is started by the user. the user sets the background the way he likes the app to look to. I made a graphicSettings activity for this task. Now i am looking for a nice way to get the new customized background loaded in all (defined) activties. So every layout that formerly had the background property bg.xml now gets a new drawable.
here two pics ... (my repution is to low now :( )
edit: 01.27.15
I got the thing working with saving the values in sqlite db and retrieving the settings from the db. Now i have to load the values in each oncreate of the activities depending on the condition of my boolean pref useCustomGraficSettings.
Isn't there a more elegant way to solve the task?

i've searched a lot
No, you did not. https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

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.

Dynamically change values of colors.xml to change look and feel of android app

Problem :
I am trying to change the look and feel of my Android app, on the fly. Something like, the app starts up and gets a set of values from the server. These values are the elements that typically go inside colors.xml. What I am looking for, is a way to dynamically change the elements inside the colors.xml and update it with these new values received from the server. My understanding is that normally, this cannot be done directly. But has anyone found a workaround?
What I want to avoid if possible :
I would like to avoid setting color values inside each activity's onCreate() method for each element in that view. If at all possible, I would like to avoid this.
Any thoughts?
You can achive this change by newly introduce firebase remote config which provide remote config to change theme color or any other values necessary for app like promotion,updates etc
You can refer this Example
Unfortunately all color values (and other resources) inside the resources directory are hardcoded as static final ints. This means there is no way to change the values at runtime. You can however use one of the previously suggested solutions or have a look at this excellent explanation: https://stackoverflow.com/a/33992017/3662251
For a nice workaround that overrides the activity's getResources method and implements a custom Resources class which is in my opinion the most seamless solution: https://stackoverflow.com/a/34178187/3662251
I have did that in my app getting Hex color code like #06FF67 from my server and stored in sharedpreferences - https://stackoverflow.com/a/23024962/4741746
And when need to set new value that coming from server just override same shared preferences value with new data and set to app
Or you can use Random Color genrater also -https://stackoverflow.com/a/5280929/4741746

Programmatically using alternative resource files?

Is there any way to programmatically select an alternative resource file to use in the app? I have a selection of buttons in my app, and want to use a different set whenever my app is in a certain mode.
Is there any way to achieve this other than manually setting the image resource on every image in code?
You can create a layout filled with exactly the views you want and inflate that at any time. Though maybe I'm not understanding your question... do you wish for R.drawable.myImage to point to 2 different things? The answer to that is no, that's not possible, but it seems like it would be pretty easy to get around this need by creating two (or more) "pointer" arrays that can point to whatever resources you want, and set those as the src for your images.
Declare a two-dimensional array to store the resource constant.
int[][] sets { {R.id.a, R.id.b, R.id.c}, {R.id.d, R.id.e, R.id.f}};
You can choose the set of views by changing the first index of the array set.

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.

Android: Use different drawables after choosing a different theme, keeping the same references

Is possible to make the app use different drawables after choosing a theme in Android?
An explanatory example:
i have a layout which background uses a reference to: "#drawable/backgroundsolid", that is image backgroundsolid.png in res/drawable-mdpi.
I want that, if the user choose "Glass" theme, the reference stays to "#drawable/backgroundsolid" but the resources folder is changed to res/drawable-glass, which contains a different backgroundsolid.png image.
Is it possible to set this programmatically? Thanks a lot!
You can set it programmatically by using view.setBackgroundResourceId(). I am not sure if you do it the other way which is let android pick the right theme for you. Maybe someone else has a better answer

Categories

Resources