Display Message like Toast in AndRoid

Hi all,

I’m newbie in Cocos2d-x

I want to display message like Toast display in Android,
But here is C++.

How can i do it?

Thanks for all your help.

If you want this to work on every platform, the best way to approach this would be to make your own class.

A snippet of it might look like this :

//To make sure that your toast is over other layers, change accordingly
#define TOAST_Z 9999
class MyToast : public CCLayer {

public :
    static void displayToast(const char *text, float duartionSeconds, CCNode *target); {

      MyToast *newToast = new MyToast();

     //here you add text and background to it;
     //and set its poistion

    target->addChild(newToast, TOAST_Z);
    newToast->schedule(schedule_selector(dismiss), durationInSeconds;
}

private :
    void dismiss() {

   this->removeFromParentAndCleanup(true);
}

}

This is just a prototype, but it should get you going :slight_smile: I’d suggest using a Scale-9 PNG for the background.

//Edit:
If you want this only on Android you can display the native toasts via a JNI call

Pawel Lopusinski wrote:

If you want this to work on every platform, the best way to approach this would be to make your own class.
>
A snippet of it might look like this :
>
[…]
>
This is just a prototype, but it should get you going :slight_smile: I’d suggest using a Scale-9 PNG for the background.
>
//Edit:
If you want this only on Android you can display the native toasts via a JNI call

Do we need a .h and a .cpp file?

Generally speaking you don’t. But you should use them for tidiness sake and as a good practice which will pay off when your classes start growing both in number and complexity.
The .h file is a header file : all it knows is what methods and fields the class has and which of them are public/protected/private. The.cpp file is an implementation file where you actually write the content of the methods defined i *.h.
Then whenever you need to use your class in another place you just #include “MyClass.h”.

What is your experience with C++ and OOP?