User-selected backgrounds

JonPM

Well-Known Member
Licensed User
Longtime User
Hello. I am trying to add a functionality to my app that allows the user to select his/her preferred background color scheme. This same color scheme would have to be applied to all of the activities in the app.
I started to create a code module with the following code:
B4X:
Sub DarkBlue
   Dim gd1 As GradientDrawable
   Dim cols1(2) As Int
   cols1(0) = Colors.ARGB(255,0,0,139)   
   cols1(1) = Colors.Black
   gd1.Initialize("TOP_BOTTOM",cols1)
   gd1.CornerRadius=0
End Sub

But I just can't seem to figure out how to apply gd1 to all other activity backgrounds. Any help?
:sign0104:
 

JonPM

Well-Known Member
Licensed User
Longtime User
Thanks! That's what was missing.
Also, what would be the best way to store this selection in a persistent database? I tried with Map but couldn't figure it out.

Something like:
B4X:
Sub DarkBlue As GradientDrawable
    Dim gd1 As GradientDrawable
    Dim cols1(2) As Int
    cols1(0) = Colors.ARGB(255,0,0,139)    
    cols1(1) = Colors.Black
    gd1.Initialize("TOP_BOTTOM",cols1)
    gd1.CornerRadius=0
    Return gd1
End Sub

'from the activity  module
Activity.Background = CodeModuleName.DarkBlue
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
So if I do this:

Map1.Put("Background",Activity.Background)

And try to read it back I get this:
android.graphics.drawable.GradientDrawable@40676ce8

But this won't work:
Activity.Background = Map1.Get("Background")

How do I accomplish this?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to "serialize" the background yourself.
File.ReadMap / WriteMap create a text file. This will not work.
When you set the background you can use a process_global variable to store the background name for example. Then you can save this name to a file.
When you load the settings you should create the correct background based on the name.

Another option is to store the colors instead of the name.
 
Upvote 0
Top