Guest User

Untitled

a guest
Oct 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. final View activityRootView = findViewById(R.id.activityRoot);
  2. activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  3. @Override
  4. public void onGlobalLayout() {
  5. int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
  6. if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
  7. ... do something here
  8. }
  9. }
  10. });
  11.  
  12. /*
  13.  
  14.  
  15. * Author: Felipe Herranz (felhr85@gmail.com)
  16. * Contributors:Francesco Verheye (verheye.francesco@gmail.com)
  17. * Israel Dominguez (dominguez.israel@gmail.com)
  18. */
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.concurrent.atomic.AtomicBoolean;
  22.  
  23.  
  24. import android.os.Handler;
  25. import android.os.Message;
  26. import android.view.View;
  27. import android.view.ViewGroup;
  28. import android.view.inputmethod.InputMethodManager;
  29. import android.widget.EditText;
  30.  
  31. public class SoftKeyboard implements View.OnFocusChangeListener
  32. {
  33. private static final int CLEAR_FOCUS = 0;
  34.  
  35. private ViewGroup layout;
  36. private int layoutBottom;
  37. private InputMethodManager im;
  38. private int[] coords;
  39. private boolean isKeyboardShow;
  40. private SoftKeyboardChangesThread softKeyboardThread;
  41. private List<EditText> editTextList;
  42.  
  43.  
  44. private View tempView; // reference to a focused EditText
  45.  
  46. public SoftKeyboard(ViewGroup layout, InputMethodManager im)
  47. {
  48. this.layout = layout;
  49. keyboardHideByDefault();
  50. initEditTexts(layout);
  51. this.im = im;
  52. this.coords = new int[2];
  53. this.isKeyboardShow = false;
  54. this.softKeyboardThread = new SoftKeyboardChangesThread();
  55. this.softKeyboardThread.start();
  56. }
  57.  
  58.  
  59. public void openSoftKeyboard()
  60. {
  61. if(!isKeyboardShow)
  62. {
  63. layoutBottom = getLayoutCoordinates();
  64. im.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
  65. softKeyboardThread.keyboardOpened();
  66. isKeyboardShow = true;
  67. }
  68. }
  69.  
  70. public void closeSoftKeyboard()
  71. {
  72. if(isKeyboardShow)
  73. {
  74. im.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  75. isKeyboardShow = false;
  76. }
  77. }
  78.  
  79. public void setSoftKeyboardCallback(SoftKeyboardChanged mCallback)
  80. {
  81. softKeyboardThread.setCallback(mCallback);
  82. }
  83.  
  84. public void unRegisterSoftKeyboardCallback()
  85. {
  86. softKeyboardThread.stopThread();
  87. }
  88.  
  89. public interface SoftKeyboardChanged
  90. {
  91. public void onSoftKeyboardHide();
  92. public void onSoftKeyboardShow();
  93. }
  94.  
  95. private int getLayoutCoordinates()
  96. {
  97. layout.getLocationOnScreen(coords);
  98. return coords[1] + layout.getHeight();
  99. }
  100.  
  101. private void keyboardHideByDefault()
  102. {
  103. layout.setFocusable(true);
  104. layout.setFocusableInTouchMode(true);
  105. }
  106.  
  107. /*
  108. * InitEditTexts now handles EditTexts in nested views
  109. * Thanks to Francesco Verheye (verheye.francesco@gmail.com)
  110. */
  111. private void initEditTexts(ViewGroup viewgroup)
  112. {
  113. if(editTextList == null)
  114. editTextList = new ArrayList<EditText>();
  115.  
  116. int childCount = viewgroup.getChildCount();
  117. for(int i=0; i<= childCount-1;i++)
  118. {
  119. View v = viewgroup.getChildAt(i);
  120.  
  121.  
  122. if(v instanceof ViewGroup)
  123. {
  124. initEditTexts((ViewGroup) v);
  125. }
  126.  
  127.  
  128. if(v instanceof EditText)
  129. {
  130. EditText editText = (EditText) v;
  131. editText.setOnFocusChangeListener(this);
  132. editText.setCursorVisible(true);
  133. editTextList.add(editText);
  134. }
  135. }
  136. }
  137.  
  138.  
  139. /*
  140. * OnFocusChange does update tempView correctly now when keyboard is still shown
  141. * Thanks to Israel Dominguez (dominguez.israel@gmail.com)
  142. */
  143. @Override
  144. public void onFocusChange(View v, boolean hasFocus)
  145. {
  146. if(hasFocus)
  147. {
  148. tempView = v;
  149. if(!isKeyboardShow)
  150. {
  151. layoutBottom = getLayoutCoordinates();
  152. softKeyboardThread.keyboardOpened();
  153. isKeyboardShow = true;
  154. }
  155. }
  156. }
  157.  
  158. // This handler will clear focus of selected EditText
  159. private final Handler mHandler = new Handler()
  160. {
  161. @Override
  162. public void handleMessage(Message m)
  163. {
  164. switch(m.what)
  165. {
  166. case CLEAR_FOCUS:
  167. if(tempView != null)
  168. {
  169. tempView.clearFocus();
  170. tempView = null;
  171. }
  172. break;
  173. }
  174. }
  175. };
  176.  
  177. private class SoftKeyboardChangesThread extends Thread
  178. {
  179. private AtomicBoolean started;
  180. private SoftKeyboardChanged mCallback;
  181.  
  182. public SoftKeyboardChangesThread()
  183. {
  184. started = new AtomicBoolean(true);
  185. }
  186.  
  187. public void setCallback(SoftKeyboardChanged mCallback)
  188. {
  189. this.mCallback = mCallback;
  190. }
  191.  
  192. @Override
  193. public void run()
  194. {
  195. while(started.get())
  196. {
  197. // Wait until keyboard is requested to open
  198. synchronized(this)
  199. {
  200. try
  201. {
  202. wait();
  203. } catch (InterruptedException e)
  204. {
  205. e.printStackTrace();
  206. }
  207. }
  208.  
  209. int currentBottomLocation = getLayoutCoordinates();
  210.  
  211. // There is some lag between open soft-keyboard function and when it really appears.
  212. while(currentBottomLocation == layoutBottom && started.get())
  213. {
  214. currentBottomLocation = getLayoutCoordinates();
  215. }
  216.  
  217. if(started.get())
  218. mCallback.onSoftKeyboardShow();
  219.  
  220. // When keyboard is opened from EditText, initial bottom location is greater than layoutBottom
  221. // and at some moment equals layoutBottom.
  222. // That broke the previous logic, so I added this new loop to handle this.
  223. while(currentBottomLocation >= layoutBottom && started.get())
  224. {
  225. currentBottomLocation = getLayoutCoordinates();
  226. }
  227.  
  228. // Now Keyboard is shown, keep checking layout dimensions until keyboard is gone
  229. while(currentBottomLocation != layoutBottom && started.get())
  230. {
  231. synchronized(this)
  232. {
  233. try
  234. {
  235. wait(500);
  236. } catch (InterruptedException e)
  237. {
  238. // TODO Auto-generated catch block
  239. e.printStackTrace();
  240. }
  241. }
  242. currentBottomLocation = getLayoutCoordinates();
  243. }
  244.  
  245. if(started.get())
  246. mCallback.onSoftKeyboardHide();
  247.  
  248. // if keyboard has been opened clicking and EditText.
  249. if(isKeyboardShow && started.get())
  250. isKeyboardShow = false;
  251.  
  252. // if an EditText is focused, remove its focus (on UI thread)
  253. if(started.get())
  254. mHandler.obtainMessage(CLEAR_FOCUS).sendToTarget();
  255. }
  256. }
  257.  
  258. public void keyboardOpened()
  259. {
  260. synchronized(this)
  261. {
  262. notify();
  263. }
  264. }
  265.  
  266. public void stopThread()
  267. {
  268. synchronized(this)
  269. {
  270. started.set(false);
  271. notify();
  272. }
  273. }
  274.  
  275. }
  276. }
  277.  
  278. /*
  279. * Android Manifest: android:windowSoftInputMode="adjustResize"
  280. */
  281.  
  282.  
  283. /*
  284. Somewhere else in your code
  285. */
  286. RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use the layout root
  287. InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
  288.  
  289. /*
  290. Instantiate and pass a callback
  291. */
  292. SoftKeyboard softKeyboard;
  293. softKeyboard = new SoftKeyboard(mainLayout, im);
  294. softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
  295. {
  296.  
  297. @Override
  298. public void onSoftKeyboardHide()
  299. {
  300. // Code here
  301. }
  302.  
  303. @Override
  304. public void onSoftKeyboardShow()
  305. {
  306. // Code here
  307. }
  308. });
  309.  
  310. /*
  311. Open or close the soft keyboard easily
  312. */
  313. softKeyboard.openSoftKeyboard();
  314. softKeyboard.closeSoftKeyboard();
  315.  
  316.  
  317. /* Prevent memory leaks:
  318. */
  319. @Override
  320. public void onDestroy()
  321. {
  322. super.onDestroy();
  323. softKeyboard.unRegisterSoftKeyboardCallback();
  324. }
  325.  
  326. InputMethodManager imm = (InputMethodManager)getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  327. imm.isActive();
Add Comment
Please, Sign In to add comment