I created a searchview and I like to get focus on search view when the searchactivity is launched, after referring some stackoverflow's questions, I got a point to setIconifiedBydefault(false); however it doesn't work, it works when I touch the search icon. guide me if I'm wrong, Here is my code.
public boolean onCreateOptionsMenu(Menu menu) {
final SearchView searchView = new SearchView(this);
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(this.getResources().getString(R.string.search_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
try {
query = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
searchView.clearFocus();
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.putExtra("query", query);
intent.putExtra("search", "true");
startActivity(intent);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
//TODO make menu an xml item
menu.add("search")
.setIcon(R.drawable.ic_action_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
I think you need to call expandActionView().
...
menu.add("search")
.setIcon(R.drawable.ic_action_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView.expandActionView();
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.