Created
May 19, 2015 06:26
-
-
Save VladSumtsov/fd337f1b6b0630c5a6d8 to your computer and use it in GitHub Desktop.
Hide keyboard, when click out of edit text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author "Sumtsov Vlad" | |
* idicates when user clicked on out of range of all activity edittexts | |
*/ | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.view.ViewGroup; | |
import android.view.ViewGroup.LayoutParams; | |
import android.widget.EditText; | |
import android.widget.FrameLayout; | |
public class EditTextsOutOfRange { | |
private static final int interceptViewId = 21342341; | |
private ViewGroup rootView; | |
private OutOfEditTextsClicked outOfRangeListener; | |
private List<View> editTexts; | |
/** | |
* @param view | |
* checked view on out of range click | |
* @param listener | |
* listener for outRange event | |
*/ | |
public EditTextsOutOfRange(OutOfEditTextsClicked listener, Context context) { | |
if (context == null) { | |
return; | |
} | |
editTexts = new ArrayList<View>(); | |
rootView = (ViewGroup) ((Activity)context).getWindow().getDecorView().getRootView(); | |
View interceptView = rootView.findViewById(interceptViewId); | |
if (interceptView == null) { | |
interceptView = new View(context); | |
LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | |
interceptView.setLayoutParams(params); | |
interceptView.setId(interceptViewId); | |
outOfRangeListener = listener; | |
rootView.addView(interceptView); | |
} | |
interceptView.setOnTouchListener(touchListener); | |
} | |
private OnTouchListener touchListener = new OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
boolean hasSomeViewInRange = false; | |
View focusedView = rootView; | |
getEditTexts(rootView); | |
for (View view : editTexts) { | |
int[] editLocation = new int[2]; | |
view.getLocationOnScreen(editLocation); | |
int[] location = new int[2]; | |
v.getLocationOnScreen(location); | |
float eventX = event.getX(); | |
float eventY = event.getY(); | |
int editTextX = editLocation[0] - location[0]; | |
int editTextY = editLocation[1] - location[1]; | |
boolean isXinRange = isCoorndInRange(eventX, editTextX, editTextX + view.getWidth()); | |
boolean isYinRange = isCoorndInRange(eventY, editTextY, editTextY + view.getHeight()); | |
if (isXinRange && isYinRange) { | |
hasSomeViewInRange = true; | |
break; | |
} | |
if (view.isFocused()) { | |
focusedView = view; | |
} | |
} | |
if (!hasSomeViewInRange) { | |
outOfRangeListener.onOutOfEditTextsClicked(focusedView); | |
} | |
return false; | |
} | |
}; | |
private void getEditTexts(ViewGroup viewGroup) { | |
for (int i = 0; i < viewGroup.getChildCount(); i++) { | |
View view = viewGroup.getChildAt(i); | |
if(view instanceof ViewGroup) { | |
getEditTexts((ViewGroup)view); | |
} else if (view instanceof EditText) { | |
editTexts.add(view); | |
} | |
} | |
} | |
private boolean isCoorndInRange(float eventCoord, int minCoord, int maxCoord) { | |
return (eventCoord < maxCoord) && (eventCoord > minCoord); | |
} | |
public interface OutOfEditTextsClicked { | |
/** | |
* @param view | |
* which is checked on out of range click | |
*/ | |
void onOutOfEditTextsClicked(View view); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment