Created
March 7, 2015 06:11
-
-
Save altaf933/dc395ffc986b495ca55e to your computer and use it in GitHub Desktop.
KeyboardUtilsAndroid
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
package com.utils.android.util; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.inputmethod.InputMethodManager; | |
import android.widget.EditText; | |
/** | |
* Created by Altaf on 07-03-2015. | |
*/ | |
public class KeyBoardUtils { | |
/** | |
* Hide keyboard from activity | |
* | |
* @param context use for hide keyboard class | |
*/ | |
public static void hideKeyboard(Context context) { | |
Activity activity = (Activity) context; | |
InputMethodManager inputManager = (InputMethodManager) context | |
.getSystemService(Context.INPUT_METHOD_SERVICE); | |
View focus = activity.getCurrentFocus(); | |
if (focus != null) | |
inputManager.hideSoftInputFromWindow(focus.getWindowToken(), | |
InputMethodManager.HIDE_NOT_ALWAYS); | |
} | |
/* | |
* Hide keyboard from focusable text view | |
* | |
* @param et_text focusable edittext | |
* @param context Activity class | |
* */ | |
public static void hideKeyboard(EditText et_text, Context context) { | |
InputMethodManager imm = (InputMethodManager) context | |
.getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.hideSoftInputFromWindow(et_text.getWindowToken(), 0); | |
} | |
/** | |
* Hide keyboard from if touch outside anywhere in class | |
* | |
* @param view root of view class | |
* @param context context data | |
* | |
*/ | |
public void hideKeyBoardOutSideTouch(View view, final Context context) { | |
//Set up touch listener for non-text box views to hide keyboard. | |
if (!(view instanceof EditText)) { | |
view.setOnTouchListener(new View.OnTouchListener() { | |
public boolean onTouch(View v, MotionEvent event) { | |
hideKeyboard(context); | |
return false; | |
} | |
}); | |
} | |
//If a layout container, iterate over children and seed recursion. | |
if (view instanceof ViewGroup) { | |
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { | |
View innerView = ((ViewGroup) view).getChildAt(i); | |
hideKeyBoardOutSideTouch(innerView, context); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment