cancel
Showing results for 
Search instead for 
Did you mean: 

A Way To Forced state for "Button with Label"

PPanb.1
Associate II

Is there a way to force state for "Button with Label" so that it can behave like "toggle" switch?

Thanks

2 REPLIES 2
Yoann KLEIN
ST Employee

Hello @PPanb.1​ ,

There is several ways to achieve what you want to do.

If you really want to use a LabelButton, you can do that easily using a Boolean variable and the "setBitmaps" method :

  • In TouchGFXDesigner, create an interaction for your button, which calls a virtual function every time the button is pressed.

0693W00000KavgpQAB.png

  • Override this virtual method in Screen1View.hpp.
void ButtonIsPressed() override;

  • Create a Boolean variable in Screen1View.hpp.
bool toggle = false;
  • In Screen1View.cpp, declare your virtual function, and toggle your Boolean variable every time the method is called.
void Screen1View::ButtonIsPressed()
{
	toggle = !toggle;
}
  • Then, depending on the value of your variable, display either the "pressed button" Bitmap or the "released button" one, with the setBitmap(Bitmap,Bitmap) method. Finally, don't forget to call invalidate.
void Screen1View::ButtonIsPressed()
{
	toggle = !toggle;
 
	if(toggle)
	{
		buttonWithLabel1.setBitmaps(Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID), Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));
	}
	else
	{
		buttonWithLabel1.setBitmaps(Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID),Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID));
	}
 
	buttonWithLabel1.invalidate();
}

In any case, I strongly recommend you to use either a ToggleButton or a FlexButton.

With the FlexButton widget, you can add a Text on it, and you can modify the trigger to "Toggle" In TGFXDesigner.

Hope that this helped you,

/Yoann

Yoann KLEIN
ST Software Developer | TouchGFX
PPanb.1
Associate II

Thank you for your solution and recommendation.