Titanium JIRA Archive
Appcelerator Community (AC)

[AC-987] Android: SearchView in ActionBar expands only if attached to TableView

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionDuplicate
Resolution Date2014-04-09T05:12:52.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labels3.2.2.GA, actionbar, android, menuitem, searchview
ReporterMark Mokryn
AssigneeRitu Agrawal
Created2014-04-08T18:04:02.000+0000
Updated2016-03-08T07:37:18.000+0000

Description

The Android SearchView will not expand in the ActionBar if it is not attached to a TableView. Even if we call expandActionView on the menu item, it will still not open. The root cause is apparently that onOptionsItemSelected in TiMenuSupport.java returns true. As we see here this is bad behavior: http://developer.android.com/guide/topics/ui/actionbar.html#ActionViewCollapsing "Because the system expands the action view when the user selects the action, you do not need to respond to the item in the onOptionsItemSelected() callback. The system still calls onOptionsItemSelected(), but if you return true (indicating you've handled the event instead), then the action view will not expand." Example below:
var win1 = Titanium.UI.createWindow({  
    backgroundColor:'#fff'
});

var searchView; 
win1.addEventListener('open', function() {
	var activity = win1.getActivity();
	var actionBar = activity.actionBar || null;
	if (actionBar){
		searchView = Ti.UI.Android.createSearchView({
        	hintText: 'Search me'
    	});
		activity.onCreateOptionsMenu = function(e) {
			var menu = e.menu;
			var searchMenuItem = menu.add({
				title: 'Search',
				actionView : searchView,
				icon: Ti.Android.R.drawable.ic_menu_search,
				showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS | Ti.Android.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW,
				itemId: 1
			});
/* this will also not work if no TableView attached, and you don't need it if there is a TableView		
			searchMenuItem.addEventListener('click', function() {
				searchMenuItem.expandActionView();
			});
*/
		};
		activity.invalidateOptionsMenu();
	}
        	   	
/* SearchView will expand only if this is uncommented
	var data = [];
	data.push(Ti.UI.createTableViewRow({title:'Apple'}));
	data.push(Ti.UI.createTableViewRow({title:'Banana'}));
	data.push(Ti.UI.createTableViewRow({title:'Orange'}));
	data.push(Ti.UI.createTableViewRow({title:'Raspberry'}));

	var tableview = Titanium.UI.createTableView({
    	data:data,
    	search:searchView,
    	searchAsChild:false
	});

	win1.add(tableview);	
*/	        	
});

win1.open();

Comments

  1. Ritu Agrawal 2014-04-08

    Thanks for reporting this issue. This issue seems to be the duplicate of TIMOB-13936. Please let us know if you are experiencing some other issue.
  2. Mark Mokryn 2014-04-08

    Yup, this is a duplicate. However, note that my example also shows an additional bug - that expandActionView() doesn't work as expected in this case.
  3. Ritu Agrawal 2014-04-09

    [~mokesmokes] We plan to take care of expandActionView() as part TIMOB-13936 as well.

JSON Source