×
Namespaces

Variants
Actions

Working with Adaptive Search feature - S60 Touch UI

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
S60 5th Edition
Article
Created: kiran10182 (02 Nov 2008)
Last edited: hamishwillee (08 Feb 2012)

Contents

Overview

Adaptive Search is a new touch UI feature integrated with existing CAknSearchField API. In this article we will learn how to play with Adaptive Search with different methods.

What is Adaptive Search feature

  • In Adaptive Search feature, characters are shown on the search grid. On selection of each character, the set of remaining characters on the grid get filtered and user can narrow down to the desired search result.
  • It is integrated with existing CAknSearchField API by adding new enumerations for Adaptive Search.
  • Generally it is used in conjunction with the List box control.
  • Native Contact application is the best example of Adaptive Search feature in the S60 5th edition device.

MainFiltered

Creating Adaptive Search using resource

  • First of all we will create listbox resource which will be integrated with CAknSearchField API to be used for Adaptive Search feature.
RESOURCE LISTBOX r_list_box
{
flags = EAknListBoxSelectionList;
array_id= r_list_box_item_array;
}
 
RESOURCE ARRAY r_list_box_item_array
{
items =
{
LBUF { txt = "\tAlbert Eienstein\t";},
LBUF { txt = "\tAlbert Makinstosh\t";},
LBUF { txt = "\tVikram Sarabhai\t";},
LBUF { txt = "\tSarabhai Anand\t";}
};
}


Implementing Adaptive Search feature

AdaptiveSearchAppView.h

  • We will inherit our class from MAdaptiveSearchTextObserver interface and implement its pure virtual method AdaptiveSearchTextChanged() to receive the characters selected from Adaptive Search grid.
  • Declare object of CAknSingleStyleListBox to populate listbox items.
  • Declare object of CAknSearchField to be integrated with listbox to implement adaptive search feature.
...
...
#include <aknlists.h> // For List Box
#include <barsread.h> //For Resource Reader
#include <aknsfld.h> // For SearchField
...
 
// CLASS DECLARATION
class CAdaptiveSearchAppView : public CCoeControl, public MAdaptiveSearchTextObserver
{
....
....
//For listbox
void CreateListBoxL();
TInt CountComponentControls() const;
CCoeControl* ComponentControl(TInt aIndex) const;
TKeyResponse OfferKeyEventL(const TKeyEvent &aKeyEvent, TEventCode aType);
 
//From MAdaptiveSearchTextObserver
virtual void AdaptiveSearchTextChanged (CAknSearchField *aSearchField);
 
private:
CAknSingleStyleListBox* iListBox;
CAknSearchField* iSearchField;
.....
};

AdaptiveSearchAppView.cpp

  • Call to create listbox from ConstructL() as shown below.
// -----------------------------------------------------------------------------
// CAdaptiveSearchAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CAdaptiveSearchAppView::ConstructL(const TRect& aRect)
{
// Create a window for this application view
CreateWindowL();
 
CreateListBoxL(); // Here we are calling listbox creation function to fill the items in listbox
 
// Set the windows size
SetRect(aRect);
 
// Activate the window, which makes it ready to be drawn
ActivateL();
}
  • Create listbox using the resource.
  • Set "this" as an observer for SearchField, hence we will receive callbacks in AdaptiveSearchTextChanged()
void CAdaptiveSearchAppView::CreateListBoxL()
{
iListBox= new (ELeave) CAknSingleStyleListBox();
iListBox->SetContainerWindowL(*this);
 
TResourceReader rr;
iCoeEnv->CreateResourceReaderLC(rr, R_LIST_BOX);
iListBox->ConstructFromResourceL(rr);
iListBox->CreateScrollBarFrameL(ETrue);
iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto );
 
CleanupStack::PopAndDestroy();//rr
 
//Create SearchField enabling Adaptive Search
iSearchField = CAknSearchField::NewL(*this, CAknSearchField::EAdaptive, NULL, 20);
 
STATIC_CAST(CAknFilteredTextListBoxModel*, iListBox->Model())->CreateFilterL(iListBox,iSearchField);
STATIC_CAST(CAknFilteredTextListBoxModel*, iListBox->Model())->Filter()->HandleOfferkeyEventL();
 
//Add adaptive search observer here
iSearchField->AddAdaptiveSearchTextObserverL(this);
}
  • Implementation of AdaptiveSearchTextChanged from MAdaptiveSearchTextObserver
  • In this callback, we are extracting string which has been entered so far on Adaptive Search grid.
void CAdaptiveSearchAppView::AdaptiveSearchTextChanged(CAknSearchField* aSearchFiled)
{
TBuf<50> buffer;
aSearchFiled->GetSearchText(buffer);
 
// added to prevent search box disappearance
SizeChanged();
DrawNow();
}
  • Handling size change events
// -----------------------------------------------------------------------------
// CAdaptiveSearchAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CAdaptiveSearchAppView::SizeChanged()
{
if (iListBox)
{
if (iSearchField)
{
CAknColumnListBox* aknListBox = STATIC_CAST(CAknColumnListBox*, iListBox);
AknFind::HandleFixedFindSizeChanged(this, aknListBox, iSearchField);
 
}
else
{
iListBox->SetRect(Rect()); // Sets rectangle of lstbox.
}
}
}
  • Returning number of controls, in our case they are Listbox and SearchField
TInt CAdaptiveSearchAppView::CountComponentControls() const
{
return 2; // return number of controls inside this container
}
 
CCoeControl* CAdaptiveSearchAppView::ComponentControl(TInt aIndex) const
{
switch ( aIndex )
{
case 0:
return iListBox;
break;
case 1:
return iSearchField;
break;
default:
return NULL;
}
}
  • Handling pointer events for Search field in HandlePointerEventL().
// -----------------------------------------------------------------------------
// CAdaptiveSearchAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// -----------------------------------------------------------------------------
//
void CAdaptiveSearchAppView::HandlePointerEventL(
const TPointerEvent& aPointerEvent)
{
 
iSearchField->HandlePointerEventL(aPointerEvent);
 
// Call base class HandlePointerEventL()
CCoeControl::HandlePointerEventL(aPointerEvent);
}
  • Handling keyboard events in OfferKeyEventL()
TKeyResponse CAdaptiveSearchAppView::OfferKeyEventL(const TKeyEvent &aKeyEvent, TEventCode aType)
{
TKeyResponse ret = EKeyWasNotConsumed;
switch (aKeyEvent.iCode)
{
case EKeyDevice3:
break;
 
default:
if(iListBox)
{
if(iSearchField)
{
TBool needRefresh( EFalse );
 
// Offers the key event to find box.
if( AknFind::HandleFindOfferKeyEventL( aKeyEvent, aType, this, iListBox, iSearchField, EFalse, needRefresh )==EKeyWasConsumed )
{
if ( needRefresh )
{
SizeChanged();
DrawNow();
}
 
return EKeyWasConsumed;
}
}
ret = iListBox->OfferKeyEventL(aKeyEvent, aType);
}
return ret;
}
}
  • Make sure to delete object of Listbox and SearchField in the destructor of the class
// -----------------------------------------------------------------------------
// CAdaptiveSearchAppView::~CAdaptiveSearchAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CAdaptiveSearchAppView::~CAdaptiveSearchAppView()
{
.....
.....
if(iSearchField)
{
iSearchField->RemoveAdaptiveSearchTextObserver(this);
delete iSearchField;
iSearchField = NULL;
}
if(iListBox)
{
delete iListBox;
iListBox = NULL;
}
}
  • Add our view on the control stack to receive OfferKeyEventL in the ConstructL of AppUi as shown below.
  • Remove the view from control stack in the destructor of AppUi as shown below.
// -----------------------------------------------------------------------------
// CAdaptiveSearchAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CAdaptiveSearchAppUi::ConstructL()
{
// Initialise app UI with standard value.
BaseConstructL(CAknAppUi::EAknEnableSkin);
 
// Create view object
iAppView = CAdaptiveSearchAppView::NewL(ClientRect());
 
AddToStackL( iAppView ); //Add our view on control stack
....
....
....
}
 
 
// -----------------------------------------------------------------------------
// CAdaptiveSearchAppUi::~CAdaptiveSearchAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CAdaptiveSearchAppUi::~CAdaptiveSearchAppUi()
{
if (iAppView)
{
RemoveFromStack(iAppView); // Remove it from control stack as shown here
delete iAppView;
iAppView = NULL;
}
}

Useful functions

CAknSearchField

  • TSearchFieldStyle::EAdaptiveSearch
  • GetSearchText()
  • SetSearchTextL()
  • HandlePointerEventL()
  • ShowAdaptiveSearchGrid()
  • SetAdaptiveGridChars()

MAdaptiveSearchTextObserver

  • AdaptiveSearchTextChanged()

Keywords

Headers

  • #include <aknsfld.h>

Classes

  • CAknSearchField
  • MAdaptiveSearchTextObserver

Libraries

  • avkon.lib

Example Application

Related links

Reference list

This page was last modified on 8 February 2012, at 07:28.
83 page views in the last 30 days.
×