java - How to make SearchView lose focus and collapse when clicking elsewhere on activity -


in app, i'm making search interface in searchview collapses , expands when loses , gains focus respectively. however, losing focus thing happening in 2 cases:

  1. when button pressed.

  2. when home icon beside searchview pressed.

i want lose focus (and hence collapse) if user clicks not on these 2 things, anywhere else on screen (e.g., button or blank portion of screen without view on it).

well found out following solution. used setontouchlistener on every view not instance of searchview collapse searchview. worked perfect me. following code.

public void setupui(view view) {      if(!(view instanceof searchview)) {          view.setontouchlistener(new ontouchlistener() {              public boolean ontouch(view v, motionevent event) {                 searchmenuitem.collapseactionview();                 return false;             }          });     }      //if layout container, iterate on children , seed recursion.     if (view instanceof viewgroup) {          (int = 0; < ((viewgroup) view).getchildcount(); i++) {              view innerview = ((viewgroup) view).getchildat(i);              setupui(innerview);         }     } } 

this answer referred to.


Comments