Bada sample project: Simple Greeting
Hello everybody!
Today a friend of mine started experimenting with one of the latest versions of bada’s SDK and IDE. Samsung shortly will release the SDK/IDE for public use. The versions he has, though, may differ slightly to those released at a later date, therefore it is thinkable that you might encounter slight inconsistencies.
The idea was to go somewhat beyond a simple hello world app and test bada’s IDE as well as its GUI building tool. We want to create a simple greeting app that asks your name and responds with a simple greeting. Something that looks like this:
Having installed the SDK/IDE and after re-starting the computer we start with an empty workspace in the IDE
Go to File->New->Bada Application Project, this will open a wizard. Type “SimpleGreeting” for the project’s name and select bada Application as the project’s type. The wizard will create a new project for you.
For now we only need to open the files SimpleGreeting.cpp and SimpleGreeting.h that hold the definitions and declarations for the bare-bone sample project. Next let us create a form for the app. In the resource window (left bottom corner on the screenshot), right click Form->Insert Resource.

This will create a new emtpy form and open the GUI editor.
Here we need to add three controls to the form: an input box, a button and a label. We start with the input box by clicking on Edit Field in the Palette and dragging a box at the top of the empty form.
Double click the edit field and you’ll see the properties tab coming to front underneath. There change the id to “NAME_FIELD“. Next draw a button below the edit field and a label below the button. In the same way change the id of the button to “HELLO_BUTTON” and the id of the label to “GREETING_LABEL“. Besides, in the properties of the button, for the text property insert something meaningful like “Say Hello!”
. Now we are ready with the form!
Let’s go to the SimpleGreeting.h header file. Here you will have to do a few things. First, let’s define a constant that we will use afterwards to identify the action event of the button. Do it right after the using-statements of all the bada namespaces.
using namespace Osp::App; using namespace Osp::Base; using namespace Osp::Graphics; using namespace Osp::Io; using namespace Osp::Locales; using namespace Osp::System; using namespace Osp::Text; using namespace Osp::Ui; using namespace Osp::Ui::Controls; static const int BUTTON_ACTION_GREET = 1;
Then, since c++ allows us multiple inheritance and we want to keep things simple and straight forward, let us implement the IActionEventListener directly with our application’s class. This will allow our app class to take care of the events all by itself without the need to provide a separate listener (we are lazy today
). Further let us define a few private variables for the controls we use, so that we can reference them later in our code.
/**
* [SimpleGreeting] application must inherit from Application class
* which provides basic features necessary to define an application.
*/
class SimpleGreeting :
public Application, public IActionEventListener
{
private:
Frame *pFrame;
Form *pForm;
Button *pButton;
Label *pLabel;
EditField *pField;
Finally declare a handler function at the end that will handle the action events later:
/** * Called when the battery level changes. */ void OnBatteryLevelChanged(BatteryLevel batteryLevel); /** * Called on Actions */ virtual void OnActionPerformed(const Osp::Ui::Control& source, int actionId); }; #endif
Well… We are ready with the header file. Let’s switch to the SimpleGreeting.cpp . Here we need to bind the variables for controls we declared earlier in the header to the actual objects we designed in the GUI editor + the object for the app’s frame (look into the tutorials for UI: each app has a single frame which holds all forms and controls and stuff within it). Let’s put it all into the OnAppInitializing function.
bool
SimpleGreeting::OnAppInitializing(AppRegistry& appRegistry)
{
// get the frame of the app
pFrame = GetAppFrame()->GetFrame();
// bind pForm to the Form we designed in resources
pForm = new Form();
pForm->Construct("IDF_FORM1");
// add the form as child to the app's frame and set it as current
pFrame->AddControl(*pForm);
pFrame->SetCurrentForm(*pForm);
// bind name input field
pField = (EditField*)pForm->GetControl("NAME_FIELD", true);
pField->SetText("Your name in here!");
// bind label
pLabel = (Label*)pForm->GetControl("GREETING_LABEL", true);
// bind button, set its action id
// and the action listener (this app's class itself)
pButton = (Button*)pForm->GetControl("HELLO_BUTTON", true);
pButton->SetActionId(BUTTON_ACTION_GREET);
pButton->AddActionEventListener(*this);
return true;
}
Now the events sent by the button clicks will be forwarded to this app’s class directly to the function you’ll need to add at the end of this file:
void
SimpleGreeting::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
String greeting = "Hello ";
switch (actionId)
{
case BUTTON_ACTION_GREET:
// Set the greeting
greeting.Append(pField->GetText());
pLabel->SetText(greeting);
// redraw app's frame
pFrame->Draw();
pFrame->Show();
break;
default:
break;
}
}
Finally we need to tell the app to draw the Frame (and all in it) as soon as it is brought to the foreground (and/or as soon as the app starts).
void
SimpleGreeting::OnForeground(void)
{
// draw and show the app's frame
pFrame->Draw();
pFrame->Show();
}
That’s all! Save all three files and build the project (Ctrl+B).
Now we are ready to test this app in the bada simulator. Right-click the project->Run As->Bada Simulator Application. The simulator will open up and run our app shortly.
This is a very brief, quick&dirty and straight-to-the-point example. Someone had to dig up his c++ know-how for this. Please bear with that person
. There is no funky professional c++ stuff in here,… and I know that YOU would rather write much better code instead
Comments are still welcome. Here are the complete two source code files:
SimpleGreeting.h
#ifndef __SIMPLEGREETING_H__
#define __SIMPLEGREETING_H__
#include <FApp.h>
#include <FBase.h>
#include <FGraphics.h>
#include <FIo.h>
#include <FLocales.h>
#include <FText.h>
#include <FSystem.h>
#include <FUi.h>
using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::Graphics;
using namespace Osp::Io;
using namespace Osp::Locales;
using namespace Osp::System;
using namespace Osp::Text;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
static const int BUTTON_ACTION_GREET = 1;
/**
* [SimpleGreeting] application must inherit from Application class
* which provides basic features necessary to define an application.
*/
class SimpleGreeting :
public Application, public IActionEventListener
{
private:
Frame *pFrame;
Form *pForm;
Button *pButton;
Label *pLabel;
EditField *pField;
public:
/**
* [SimpleGreeting] application must have a factory method that creates an instance of itself.
*/
static Application* CreateInstance(void);
public:
SimpleGreeting();
~SimpleGreeting();
public:
/**
* The application must provides its name.
*/
String GetAppName(void) const;
protected:
/**
* The application must provide its ID.
*/
AppId GetAppId(void) const;
/**
* The application must provide a secret code.
*/
AppSecret GetAppSecret(void) const;
public:
/**
* Called when the application is initializing.
*/
bool OnAppInitializing(AppRegistry& appRegistry);
/**
* Called when the application is terminating.
*/
bool OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination = false);
/**
* Called when the application's frame moves to the top of the screen.
*/
void OnForeground(void);
/**
* Called when this application's frame is moved from top of the screen to the background.
*/
void OnBackground(void);
/**
* Called when the system memory is not sufficient to run the application any further.
*/
void OnLowMemory(void);
/**
* Called when the battery level changes.
*/
void OnBatteryLevelChanged(BatteryLevel batteryLevel);
/**
* Called on Actions
*/
virtual void OnActionPerformed(const Osp::Ui::Control& source, int actionId);
};
#endif
SimpleGreeting.cpp
/**
* Name : SimpleGreeting
* Version : 1.0.0
* Vendor :
* Description :
*/
#include "SimpleGreeting.h"
SimpleGreeting::SimpleGreeting()
{
}
SimpleGreeting::~SimpleGreeting()
{
}
Application*
SimpleGreeting::CreateInstance(void)
{
// Create the instance through the constructor.
return new SimpleGreeting();
}
String
SimpleGreeting::GetAppName(void) const
{
//WARNING:
// This method is automatically generated.
// Please don't modify it!
// Return the name of the application.
static String appName(L"SimpleGreeting");
return appName;
}
AppId
SimpleGreeting::GetAppId(void) const
{
//WARNING:
// This method is automatically generated.
// Please don't modify it!
// Return the application ID.
static AppId appId(L"93bt1p123e");
return appId;
}
AppSecret
SimpleGreeting::GetAppSecret(void) const
{
//WARNING:
// This method is automatically generated.
// Please don't modify it!
// Return the secret code of the application.
static AppSecret appSecret(L"9C645DDBA19C71BAD1204DA4DAA7A0B9");
return appSecret;
}
bool
SimpleGreeting::OnAppInitializing(AppRegistry& appRegistry)
{
// get the frame of the app
pFrame = GetAppFrame()->GetFrame();
// bind pForm to the Form we designed in resources
pForm = new Form();
pForm->Construct("IDF_FORM1");
// add the form as child to the app's frame and set it as current
pFrame->AddControl(*pForm);
pFrame->SetCurrentForm(*pForm);
// bind name input field
pField = (EditField*)pForm->GetControl("NAME_FIELD", true);
pField->SetText("Your name in here!");
// bind label
pLabel = (Label*)pForm->GetControl("GREETING_LABEL", true);
// bind button, set its action id
// and the action listener (this app's class itself)
pButton = (Button*)pForm->GetControl("HELLO_BUTTON", true);
pButton->SetActionId(BUTTON_ACTION_GREET);
pButton->AddActionEventListener(*this);
return true;
}
bool
SimpleGreeting::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
{
// TODO:
// Deallocate resources allocated by this application for termination.
// The application's permanent data and context can be saved via appRegistry.
return true;
}
void
SimpleGreeting::OnForeground(void)
{
// draw and show the app's frame
pFrame->Draw();
pFrame->Show();
}
void
SimpleGreeting::OnBackground(void)
{
// TODO:
// Stop drawing when the application is moved to the background.
}
void
SimpleGreeting::OnLowMemory(void)
{
// TODO:
// Free unused resources or close the application.
}
void
SimpleGreeting::OnBatteryLevelChanged(BatteryLevel batteryLevel)
{
// TODO:
// Handle any changes in battery level here.
// Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
}
void
SimpleGreeting::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
String greeting = "Hello ";
switch (actionId)
{
case BUTTON_ACTION_GREET:
// Set the greeting
greeting.Append(pField->GetText());
pLabel->SetText(greeting);
// redraw app's frame
pFrame->Draw();
pFrame->Show();
break;
default:
break;
}
}
Have fun!
Related posts:
the Flagship blog & community for App Developers with main focus on Samsung's bada and cross-platform technologies 













24 Responses to “Bada sample project: Simple Greeting”
By Codereview on Dec 10, 2009 | Reply
Now, lets bring it down, how crappy your code is:
first, its a no go to open up namespaces in a Header.
second, qualifiers with __ are reserved to the compiler
So, PLEASE also look out for cleaner code in your examples.
By wit on Dec 11, 2009 | Reply
Hehe, thank you for your input, Codereview. There might certainly exist one or another problem with the code – as I said it was a quick & dirty solution.
However, in my defense, I have to say that those things you mentioned are actually pre-generated by the project creation wizard itself.
By civichief on Dec 11, 2009 | Reply
the s8000 jét ist used als illustrated emulator. is there a reason for this? is jét already running on bada or are there any plans to port bada on jét?
By wit on Dec 11, 2009 | Reply
a valid question there
I hear rumors from various seemingly valid sources that jet will actually be upgradeable to bada. Some people say that today s8000 even runs on the previous version of the bada platform (!).
For example, take a look here: http://www.techradar.com/news/phone-and-communications/mobile-phones/10-things-to-know-about-samsung-s-bada-platform-657398
#4 “[...] It’s telling that the Jet was based on an earlier version of Bada from Samsung’s proprietary SHP OS, which was used for several years – it makes the line between feature phone and smartphone even more fluid, if simply adding some bits to an existing framework can make the difference. [...]“
By riccardo on Dec 12, 2009 | Reply
Neither the C compiler nor the Objective-C compiler treats variable names with leading underscores any differently than any other variable name. So it is not reserved by compiler. It is just used as private member variables, not as qualifier (class name or namespace).
By mark on Dec 12, 2009 | Reply
@riccardo Yes you are right, but not in 100%. Codereview’s post describes the best practice as underscore is reserved in C++ standard for global names (no matter what Objective(C) compiler does).
By Ben Morris on Dec 14, 2009 | Reply
Don’t expect phones already in the market to be opened up retrospectively – it would require reflashing, and I just don’t think that will ever happen. But going forward, that is exactly the class of phone that bada will open up. Nice site, BTW.
By piti on Apr 20, 2010 | Reply
hy guys!
first of all: thanks a lot for this tutorial!
BUT: when i want to implement the IActionEventListener to my application-class, the IDE is not able to compile the project any more. it even does not know the IActionEventListener!
You mentioned that there might encounter slight inconsistencies. Here they are?
greez
By wit on Apr 21, 2010 | Reply
Well, the SDK is moving forwards and some inconsistencies appear now and then when you look at old posts like this one
You probably need to give a complete reference to the listener (including namespaces) like:
class YourClass : public Osp::Ui::IActionEventListener { // rest of the codeBy God on Apr 23, 2010 | Reply
I didn’t manage to get this example to work?
By God on Apr 23, 2010 | Reply
my bad…. am an idiot. It works, thanks for the tutorial
By Irony on Jun 16, 2010 | Reply
This is probably a problem caused by something silly and not differences between the SDKs (just based on odds) but I am having the problem that when I compile the private declarations receive the following error:
- ISO C++ forbids declaration of ‘Frame’ with
no type
- expected ‘;’ before ‘*’ token
I did some searching online and am fairly confused by this. What am I forgetting?
thanks!
By Irony on Jun 16, 2010 | Reply
Ok, strike that I figured out that problem,
now when I click the button the simulator crashes, with commenting out code and such I narrowed it down to the redraw, is there some reason this wouldn’t work?
By Lakshmimages on Jun 29, 2010 | Reply
Hi,I am new in bada OS.Nice tutorial.I followed the same code in my application.Its running without error,but at run time the splash screen only coming in the emulator,after the simulator gone.in the console shows the “his application has requested the Run time to terminate it in an unusual way.
Please contact the application’s support team for more information” error.What is the problem?help me.
By slash on Jul 12, 2010 | Reply
Same problem as Lakshmimages, any solution???????
By wit on Jul 12, 2010 | Reply
Hi, this tutorial was based on a very old SDK version.
I don’t see directly where the problem might be.
What you could do is build an empty form application and take the essence of this tutorial (UI Building and source code in OnInitializing function) – to build your own solution.
If you encounter any problems, feel free to ask on the forums
By slash on Jul 13, 2010 | Reply
Hi wit,
thanks a lot for your reply, i will try again
By Wilee on Jul 14, 2010 | Reply
HI Wit,
do you faced difficulty using EditField because the problem is I try to get the text from the field and display at the label(which almost same as you) , I can’t display the words that I type, do you do any setting regarding for that?
thanks
By Shinigami on Aug 12, 2010 | Reply
Wow I finally built it without error and when I launch it, this app crashes simulator.
What am I doing wrong?
I’m completely new to programming for mobile devices, bada and somewhat new to C++. In other words: I’m still learning xD
By Suvin on Aug 27, 2010 | Reply
Hello I want to develop a BADA application which will print the message Helloworld.I have downloaded BADA SDK.It could be done in BADA IDE but i want to compile and run it using command prompt.Now for that we need to compile it using GCC tool chain i think.My Helloworld code is in C:Helloworld.So i have changed directory to it and also have set path to C:\bada\1.0.0b3\Tools\Toolchains\Win32\bin where g++ is located.Now i am compiling it by using command g++ -Wall Helloworld.cpp -o Helloworld.I am getting an error In file included from Helloworld.cpp:11: HelloWorld.h:4: fatal error: FApp.h: No such file or directory compilation terminated. FApp.h is already included in Helloworld.h. Still i am getting this error.When i am running the same code in BADA IDE its showing the output.Could any body help that what needs to be done to compile BADA application from command prompt.
By wit on Aug 27, 2010 | Reply
Hi Suvin,
I would ask on our forums (or at least take a look around there). If I remember it right, there were quiet a few people compiling bada stuff from command prompt!
Cheers,
- wit