Adding and removing toolbar items

Toolbar items are controls that derive from the CCoeControl base class. The floating toolbar can have only buttons (CAknButton) if it is focusing. The number of buttons displayed in floating toolbar depends on the screen resolution in use. The fixed toolbar always has three buttons.

You can define toolbar items either in the resource file or create them dynamically at runtime.

If you define the items in resources, use the TBAR_CTRL structure. Define at least the type, ID, and the control itself in this structure. For the resource definitions, see Constructing the toolbar.

To add the items dynamically, use the toolbar functions as illustrated in the examples below.

You can add items to a specific position in toolbar or as the last item in the toolbar.

You can also remove toolbar items at runtime.

The example below shows how to add a button dynamically as the last toolbar item.

void CMyAppView::AddItemAsLastToToolbarL()
    {
    CAknToolbar* toolbar = Toolbar();
    if ( toolbar )
        {
        CAknButton* newButton = CAknButton::NewL( R_MYAPP_BUTTON );
        toolbar->AddItemL( newButton, EAknCtButton, KButtonId, 0 );
        }
    }

The example below shows how to add a button dynamically as the first toolbar item.

void CMyAppView::AddItemAsFirstToToolbarL()
    {
    CAknToolbar* toolbar = Toolbar();
    if ( toolbar )
        {
        CAknButton* newButton = CAknButton::NewL( R_MYAPP_BUTTON );
        // Last parameter the index at where to add the button
        toolbar->AddItemL( newButton, EAknCtButton, KButtonId, 0, 0 );
        }
    }

The example below shows how to remove a button from the toolbar based on the button ID.

void CMyAppView::RemoveItemFromToolbar()
    {
    CAknToolbar* toolbar = Toolbar();
    if ( toolbar )
        {
        toolbar->RemoveItem( KButtonId );
        }
    }