cancel
Showing results for 
Search instead for 
Did you mean: 

ClickListener Callback Not Working

jackcampbell
Associate II

I have a custom container that has a box inside of it with the ClickListener enabled. I am trying to setup a callback for when the box is clicked. I have the following code setup:

 

 

#include <gui_generated/containers/NavbarBase.hpp>

class Navbar : public NavbarBase
{
public:
    Navbar();
    virtual ~Navbar() {}

    virtual void initialize();

protected:
    virtual void setupMenuItem(TextArea *label, Image *icon);

    virtual void tMenuItemCallbackHandler(const touchgfx::Box &box, const touchgfx::ClickEvent &e);

    touchgfx::Callback<Navbar, const touchgfx::Box&, const touchgfx::ClickEvent&> tMenuItemCallback;

};
#include <gui/containers/Navbar.hpp>


Navbar::Navbar() : tMenuItemCallback(this, &Navbar::tMenuItemCallbackHandler) {

}


/**
 * Adjusts the menu label's X position to be centered with the icon and
 * adjusts the Y position to be centered with the space below the icon.
 * Disabled touch interaction on both elements.
 */
void Navbar::setupMenuItem(TextArea *label, Image *icon) {
	uint16_t labelAdjustedX = icon->getX() + (icon->getWidth() / 2) - (label->getTextWidth() / 2);
	label->setX(labelAdjustedX);
	uint16_t iconBaseY = icon->getY() + icon->getHeight();
	uint16_t labelYPadding = (this->getHeight() - iconBaseY - label->getTextHeight()) / 2;
	label->setY(iconBaseY + labelYPadding);
	label->setTouchable(false);
	icon->setTouchable(false);
}


void Navbar::tMenuItemCallbackHandler(const touchgfx::Box &box, const touchgfx::ClickEvent &e) {
	this->DMenuIcon.setVisible(false);
}


void DMEModeNavbar::initialize()
{
    NavbarBase::initialize();

    // Adjust the positions of the text
    this->setupMenuItem(&this->TMenuLabel, &this->TMenuIcon);
    this->setupMenuItem(&this->DMenuLabel, &this->DMenuIcon);
    this->setupMenuItem(&this->VMenuLabel, &this->VMenuIcon);

    this->TMenuSelector.setTouchable(true);
    this->TMenuSelector.setClickAction(this->tMenuItemCallback);
}

 

 

 

When TMenuSelector is clicked, DMenuIcon should disappear, however, nothing happens. I have looked through a variety of similar posts and code and I do not see what is going wrong here. Any direction would be appreciated. 

3 REPLIES 3
jackcampbell
Associate II

Small update: I added a button widget to the project to see how it is configured, when I tapped on the rectangle first and then the button, the rectangle callback would execute...not sure why this is delayed until the button (which was not connected to anything) was clicked...

JTP1
Lead

Hello

Try to invalidate() after set visible false:

void Navbar::tMenuItemCallbackHandler(const touchgfx::Box &box, const touchgfx::ClickEvent &e) 
{
	this->DMenuIcon.setVisible(false);
	this->DMenuIcon.invalidate();

}

About ClickListener, here is good example for box object

https://support.touchgfx.com/docs/development/ui-development/touchgfx-engine-features/mixins

Hope this helps !

Br JTP

Hello @jackcampbell ,

Have you tried the solution our amazing friend mentioned? 

Mohammad MORADI
ST Software Developer | TouchGFX