Colors in stored variables


I’m an absolute beginner. I have a button and want the App to save the last clicked color of the button, so when I close and reopen the App I know what the last button was, that was clicked. Because of the difference in color between the design screen en the Blocks screen you have to set "when screen1 opens set to button’s background cloor to…So it doesn’t work with a stored variable.

1 Like

Hey @frederikdeclercq44o1, welcome to the Community :wave:

At the moment though you’re not storing the colour values in your variable - is that the issue?

1 Like

I tried storing it but, it doesn’t work because: "when screen1 opens set background color to…
If you don’t use the block “when Screen1 opens” switching colors doesn’t work.
I want the App to store the color of my button; I don’t care how. Let’s say it’s an exercise App and when I’ve completed an exercise I push a button and it turns green. The next week when I open the App I want it to show that I have finished the exercise and the button is green. Adding a reset button …turning the button to the starting color.

I’m not sure what you mean here - this was my approach and the value is stored between sessions.

  1. Test for the initial, empty state and set the button to the default colour
  2. Write the new value to the variable every time a change occurs

Try it out:
https://x.thunkable.com/copy/03bcaaddf3b516407723bd8266b16f81

3 Likes

Hi everyone
Just like @frederikdeclercq44o1 said, if I store something in a variable, it resets the next time open the app.

Then you aren’t using stored variables correctly.

Post a screenshot of your blocks.

1 Like

Thanks, it kind of works. Sometimes I have to click more then once to change the color? Sometimes 3x sometimes 2x …
I’ve added a clear button and removed some colors.


Schermafbeelding 2023-09-21 115733

Sure, that makes sense, you’re randomly drawing from a list of 2 items. It’s just like flipping a coin, you might get 3 heads in a row, you might get 100 in a row, but on average it will flip back and forth every couple of clicks/flips

I only used the random colours for illustration purposes @frederikdeclercq44o1, in your case are you trying to make a toggle button, is that right?

1 Like

I want to click a button, so it changes from red to green and back. (toggle) and I want to store the change, so I know what last color was which was clicked even if I close the App.(stored variable) If that is not possible, maybe a switch is an option and saving its position in a stored variable.
Btw, Thx for all the help.

That’s what I thought - there’s no need for the random list item so, you just need to store the button colour.

I linked to an tutorial on creating toggles in my previous post :point_up:

Yep, a switch or a checkbox would also work with the same sort of logic too

So it’s not possible to store the button color in a stored variable, because of the difference in color between the ‘Design’ and the ‘Blocks’ screen. I have to use a switch or a checkbox? How do I store the setting of a switch in a stored variable?
All I wanted was when I clicked a button, the color changed and the color was stored so when I closed the App en reopened it, it showed the last shown color.
e.g. : when clicked, button is red or green. Close App en reopen App, show last color.

It’s definitely possible, just use rgb or HEX values

It works fine on my computer, but not on my phone.

This is what I see on my laptop:


On my phone, the screen is blank and the Blocks page looks like this:

The variable blocks are different???
The toggle with the label change works, but how do I make it work with the switch? I use the drag and drop version of Thunkable.


Only the text of the switch changes.

When using a stored variable, you should check to see if it’s equal to null when the screen opens and if it is, give it a starting value such as an empty string (“”) or a default color value.

If you want to share a link to your project, I can take a look at your blocks. I’ve saved color values in stored variables in various projects and it’s worked for me on an iPhone or web browser.

https://x.thunkable.com/projects/65101d53a580be5304b6aa99/e3fe278a-a329-44b4-95a7-8f18cc47cb67/designer


I understand, the stored variable is empty when first used. But I can’t work out how to set it to the starting color, value or null or Empty???
The screen starts blank, once you’ve clicked the button once, it works because the variable has a value. Correct?

Is want to do the same thing with a switch. Saving the settings of the switch in a stored variable. Do I have to have a variable for every setting, tint color, thumb tint color, switch value??? Any good tutorials.
I really appreciate all the help.

In the example I shared with you, when the screen opens we check if the stored variable has been initialised and, if not, set the button colour to grey. You can do something similar in your project

Somehow it worked with the grey block but not a block with #7ED321.
Anyway, once the variable has a starting value it works.
Thx for the help.

I’m trying the same thing with a switch.
https://x.thunkable.com/projects/65102b5470b935bd8fc41c24/b9c88b5b-e720-4af6-97ab-b6884aeb96fc/designer


It works fine on my laptop, but on my phone the color of the thumb does not change and stays green.

@domhnallohanlon @frederikdeclercq44o1 This seems to be less a problem with stored variables and more a problem with the switch component.

When I disconnect all of your blocks from their event blocks and add another switch will default properties, both switches change colors when I click on them in a web preview but neither of the switches change colors on my iPhone in Thunkable Live.

Modified project: Thunkable

So the switch component doesn’t work as it should. OK. I will use buttons instead.
Will you keep me posted?
Thx

I don’t work for Thunkable so it would be better to ask that of someone who does.