java
sql
php
xml
ajax
database
linux
android
regex
visual-studio
multithreading
flash
html5
perl
algorithm
facebook
cocoa
tsql
apache
api
Suppose your EditText is placed on Linear layout or other ViewGroup. Then you should make this container clickable, focusable and focusableInTouchMode. After that set onClickListenet to this container with following code in onClick method:
@Override public void onClick(View view) { InputMethodManager imm = (InputMethodManager) view.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }
woodshy has the answer for hiding keyboard, but maybe put the code at onClick is not as good as putting it to the onFocusChanged(). As for force it to lose focus, you need to set the object you want to transfer the focus to, at its xml file:
android:focusable="true" android:focusableInTouchMode ="true"
I had the same requirement as I had to hide the keyboard once I touch anywhere outside my EditText box. The setOnFocusChangeListener does not do the job as even if you touch outside the edit text box is still selected.
For this I used the solution edc598 here.
Code sample modified from here:
LinearLayout MainLayout = (LinearLayout) findViewById(R.id.MainLayout); EditText textBox = (EditText) findViewById(R.id.textBox); int X_TOP_LEFT = 157; int Y_TOP_LEFT = 388; int X_BOTTOM_RIGHT = 473; int Y_BOTTOM_RIGHT = 570; MainLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(textBox.isFocused()){ Log.i("Focussed", "--" + event.getX() + " : " + event.getY() + "--"); if(event.getX() <= 157 || event.getY() <= 388 || event.getX() >= 473 || event.getY() >= 569){ //Will only enter this if the EditText already has focus //And if a touch event happens outside of the EditText textBox.clearFocus(); InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); //Do something else } } Log.i("X-Y coordinate", "--" + event.getX() + " : " + event.getY() + "--"); //Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show(); return false; } });