Tutorial: Switch views with form tabs (part 2)
I have been experimenting around with the tabs and panels and I think I have finally found the easiest way of switching between views/panels on a form using the tabs (as described in the first part of this tutorial). The easiest way is to create as many ScrollPanel objects on your form in GUI Designer, as the number of tabs that you need. But first, you will need to create panel resource objects that you will later on link to each of your scroll panels.

The best part of designing separate panels is, that you can modify the size of the panel. For example, you can make it twice as long as the screen size. When you bind it to the scroll panel later and run the app, the user will automatically be able to scroll through the whole panel using a finger
Going back to the form design, I have created two scroll panels and placed them one above the other. By editing the “Panel ID”-property, each of them has been linked to the corresponding panel that we have produced above.
You may say that this is a dirty solution, as you cannot cleanly access those objects if they are piled up one above the other. I have to disagree there, though. In the top-right corner of the GUI Designer you will see an objects outline, where you can select and modify each scroll panel control separately.
We are done with the GUI Designer, it is time to add some code! In the header file of the form class I have created two additional members to reference both scroll panels:
Osp::Ui::Controls::ScrollPanel *ScrollPanel1_; Osp::Ui::Controls::ScrollPanel *ScrollPanel2_;
I bind them to the corresponding objects by modifying the OnInitializing() code and adding the following lines to it:
ScrollPanel1_ = static_cast<ScrollPanel*>(this->GetControl("IDC_SCROLLPANEL1"));
ScrollPanel2_ = static_cast<ScrollPanel*>(this->GetControl("IDC_SCROLLPANEL2"));
ScrollPanel2_->SetShowState(false);
There we go. As you can see, we directly hide the second panel. By default, the first tab is selected when the app starts, and so we want to see the first panel only. Finally, the tab event handler has to be modified to make it hide/show panels accordingly.
void
MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
switch(actionId)
{
case ID_PANEL_1:
ScrollPanel2_->SetShowState(false);
ScrollPanel1_->SetShowState(true);
this->RequestRedraw(true);
break;
case ID_PANEL_2:
ScrollPanel2_->SetShowState(true);
ScrollPanel1_->SetShowState(false);
this->RequestRedraw(true);
break;
default:
break;
}
}
And this is it! Build the application and run it. You should be able to switch between panels easily by clicking on the corresponding tabs.
There is an additional magic to this whole setup: your panels could be several times longer than the screen size, but you would still be able to scroll through them directly without doing any additional coding. This is yet another example of the simplicity and friendliness of Bada’s UI API. Enjoy!
Related posts:
the Flagship of independent news, reviews and resources for Developers and Users of Samsung's mobile platform Bada 








8 Responses to “Tutorial: Switch views with form tabs (part 2)”
By Yavor on Feb 11, 2010 | Reply
Out of curiosity is there anything wrong with the architecture of BasicApp from /samples? I can think of a few cases where if you load all panels at once, and then simply manipulate their visibility will cause more trouble than good. Just think what will happen if each tab requires some expensive operation.
By wit on Feb 11, 2010 | Reply
Well, there is nothing wrong with that architecture, I guess. Apart from the fact that you will rather want to work with panels instead of forms if you are using tabs.
As you might have noticed, tabs are bound to a certain form, so if you “switched” to another form completely, it’d mean that that form would need to have an exactly the same copy of the tabs – _that_ would do more harm that good, imho. In other words: you are not meant to switch between forms using the tabs, as I understand the current API. I may be wrong though.
Finally, I am not sure what kind of expensive operation each of your tabs would be performing, that you cannot stop.
I do not think that having a few hidden panels with a bunch of controls on them will consume a lot of resources. Unless, of course, you are running some sort of a game/drawing operations of your own there. But then again, it’s simple to stop your game loop on those tabs that aren’t active.
By Yavor on Feb 11, 2010 | Reply
“As you might have noticed, tabs are bound to a certain form, so if you “switched” to another form completely, it’d mean that that form would need to have an exactly the same copy of the tabs – _that_ would do more harm that good, imho”
Yep, I’ve noticed that. This is why I have only one form with a tab view, and a few others derived from it. IMHO this is a better OO design as it allows you to nicely group common behaviour in the parent, and isolate everything else in the derived classes.
By wit on Feb 12, 2010 | Reply
Now I see what you are saying, Yavor
You have created a class form with the tabs you need and then simply derived form classes from it. I have to admit that it is, indeed, an elegant solution. Nice!
By God on Apr 28, 2010 | Reply
could you please post the full source code? the tab switching doesn’t seem to work properly for me.
By flawless on Apr 29, 2010 | Reply
As I see it, Samsung recently removed the option to set a “Panel ID” for the scroll panels… WTF?! What’s the reason to have a scrollpanel at all then?
This tutorial is a little outdated now! Gotta work with derived forms instead!
By fedor ortiz on Jun 17, 2010 | Reply
how can you change the color of tabs?
without using images?