I know this question has been asked million times, but I’m not able to find a direct solution for my specific problem.
In my SearchActivity, there is a EditText & a ListView.
I want even if the user touches anywhere except EditText, the keyboard should close.
What I’ve done is setup a TouchListener on ListView & whenever it is touched, the keyboard is closed.
But it is creating a problem that when I touch on any element of the ListView, the keyboard hides, but the element of the list becomes invisible.
Please suggest a better approach than this.
My Code:
public class SearchActivity extends Activity implements OnTouchListener {
RelativeLayout rlSearchLayout;
EditText etSearchBar;
ListView lvSearchResult;
private static String searchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
setTitle("SearchActivity.java");
etSearchBar = (EditText) findViewById(R.id.etSearchBar);
lvSearchResult = (ListView) findViewById(R.id.lvSearchResult);
searchText = etSearchBar.getText().toString();
lvSearchResult.setOnTouchListener(this);
String[] objects = { "One", "two" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, objects);
lvSearchResult.setAdapter(adapter);
}
@Override
public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etSearchBar.getWindowToken(), 0);
return false;
}
}
Add this to your layout file.
Now add a
TouchListenerfor thisimageViewin your activity.