-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardUtils.java
89 lines (70 loc) · 2.92 KB
/
KeyboardUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.support.design.widget.FloatingActionButton;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by Hasan Mhd Amin on 9/15/2016.
*/
public class KeyboardUtils {
public static void hideKeyboardOnClickOutside(View view, final Activity activity){
view.setClickable(true);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
setupUI(view, activity);
}
private static void setupUI(View view, final Activity activity) {
//Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText) && !(view instanceof FloatingActionButton)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(activity);
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, activity);
}
}
}
public static void hideSoftKeyboard(Activity activity) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
}
public static void showHideKeyboard(Activity activity) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(activity.getCurrentFocus().getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void showSoftKeyboard(Activity activity, EditText editText) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
public static Boolean isKeyboardOpen(View parentView)
{
Rect r = new Rect();
parentView.getWindowVisibleDisplayFrame(r);
int screenHeight = parentView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
return true;
} else {
// keyboard is closed
return false;
}
}
}