searchbar return event not firing

You must Login before you can answer or comment on any questions.

I have a tab with a searchbar, I've created it independently and inserted it into the window and created it as part of a tableview and no matter what way I do it I can get focus events to fire but never 'return' events so I can't actually execute searches without placing a button somewhere on the screen to execute. This is both with 1.2 and 1.3, hoping it was a 1.2 bug but I'm pulling my hair out with no luck.

7 Answers

Accepted Answer

I do this:

var search_bar = Titanium.UI.createSearchBar({
    barColor:'#000', 
    showCancel:true,
    height:43,
    top:0
});
 
 
search_bar.addEventListener('blur', function(e) {
    Titanium.API.info('search bar:blur received');
});
 
search_bar.addEventListener('cancel', function(e) {
    Titanium.API.info('search bar:cancel received');
    search_bar.blur();
});
 
search_bar.addEventListener('return', function(e) { 
 
    if ( !Titanium.Network.online ) {
        alert( "You need an intrnet connection to search for books.");
    }
 
    else if ( e.value.length >= 3 ) {
 
        do_search(e.value);
        search_bar.blur();
    }
    else {
        alert("Please use minimum 3 characters to search.");
    }
});
 
search_win.add(search_bar);
And it works ok , only that I compile with 1.1.2.

I just looked in git and do not see a 1.1.2 tag, I'll try 1.1.1 and see what happens.

That's almost my code to a T, maybe I need to try 1.1.2, I'll pull from git and compile to give it a shot. For reference here is a cut and paste of what I use for 1.2 and 1.3 that doesn't work for the 'return' event.

var search = Titanium.UI.createSearchBar({
    barColor:win.barColor, 
    showCancel:true,
    height:43,
    top:0,
    value:'Enter name, etc...'
});
 
search.addEventListener('return', function(e)
{
    Ti.API.Info('Search button pressed');
});
 
// event for cancel
search.addEventListener('cancel', function(e)
{
    search.blur();
});
// automatically clear search bar when clicked
search.addEventListener('focus', function(e)
{
    search.value = '';
});
 
 
win.add(search);

I gave it a run and compiled it agains 1.2.0 and it works just fine Maybe if you try to use

hintText:'Enter name, etc...'
instead of value in constructor will change something.

I'm not sure that hintText is implemented here, but give it a try :)

Thanks for the suggestion but I've had no luck so I took it down to the basics but still can't get events to fire. I created a test project using the following code and I can't receive any events on the search bar.. Using SDK 1.2.0.

app.js:

// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
 
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
 
 
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
    url:'search.js',
    title:'Search',
    navBarHidden:true
});
 
var tab1 = Titanium.UI.createTab({  
    icon:'KS_nav_views.png',
    title:'Tab 1',
    window:win1
});
 
var label1 = Titanium.UI.createLabel({
    color:'#999',
    text:'I am Window 1',
    font:{fontSize:20,fontFamily:'Helvetica Neue'},
    textAlign:'center',
    width:'auto'
});
 
win1.add(label1);
 
//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({  
    title:'Tab 2',
    backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({  
    icon:'KS_nav_ui.png',
    title:'Tab 2',
    window:win2
});
 
var label2 = Titanium.UI.createLabel({
    color:'#999',
    text:'I am Window 2',
    font:{fontSize:20,fontFamily:'Helvetica Neue'},
    textAlign:'center',
    width:'auto'
});
 
win2.add(label2);
 
 
 
//
//  add tabs
//
tabGroup.addTab(tab1);  
tabGroup.addTab(tab2);  
 
 
// open tab group
tabGroup.open();
search.js:
// get current window
var win = Titanium.UI.currentWindow;
 
//search bar
//
var searchbar = Titanium.UI.createSearchBar({
    barColor:win.barColor, 
    showCancel:true,
    height:43,
    top:0
});
 
searchbar.addEventListener('return', function(e)
{
    Ti.API.Info('Search button pressed');
});
 
// event for cancel
searchbar.addEventListener('cancel', function(e)
{
    Ti.API.Info('search: cancel');
    search.blur();
});
// automatically clear search bar when clicked
searchbar.addEventListener('focus', function(e)
{
    Ti.API.Info('Search focus');
});
 
 
win.add(searchbar);

Tamas, Thanks for the help...

In the end the first post answered my question, I somehow had "Info" as the log method instead of "info" so it looked like nothing was working.

Umm, this thread has been quiet for three years, but I still have a problem with the Return event on searchBar in Android: the first return event is not registered. It's taken as a change event, strangely.

Your Answer

Think you can help? Login to answer this question!