Guest User

Untitled

a guest
Jun 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. package com.pedro.example;
  2.  
  3. import android.content.Context;
  4. import android.support.constraint.ConstraintLayout;
  5. import android.util.AttributeSet;
  6. import android.util.Log;
  7.  
  8. /**
  9. * A common ConstraintLayout with a listener callback that notifies when the soft keyboard
  10. * is shown or hidden, as for now there is no such feature in the android framework
  11. */
  12. public class SoftKeyboardAwareConstraintLayout extends ConstraintLayout {
  13.  
  14. private SoftKeyboardVisibilityChangeListener callback;
  15.  
  16. private boolean isKeyboardShown;
  17.  
  18.  
  19. public SoftKeyboardAwareConstraintLayout(Context context) {
  20. super(context);
  21. }
  22.  
  23. public SoftKeyboardAwareConstraintLayout(Context context, AttributeSet attrs) {
  24. super(context, attrs);
  25. }
  26.  
  27. public SoftKeyboardAwareConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  28. super(context, attrs, defStyleAttr);
  29. }
  30.  
  31.  
  32. @Override
  33. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  34. if(callback != null) {
  35. final int proposedHeight = MeasureSpec.getSize(heightMeasureSpec);
  36. final int actualHeight = getHeight();
  37.  
  38. if (actualHeight > proposedHeight) {
  39. if(!isKeyboardShown) {
  40. Log.d("KEYBOARD_EVENTS", "Keyboard is shown");
  41. callback.onSoftKeyboardShow();
  42. isKeyboardShown = true;
  43. }
  44. } else {
  45. if(isKeyboardShown) {
  46. Log.d("KEYBOARD_EVENTS", "Keyboard is hidden");
  47. callback.onSoftKeyboardHide();
  48. isKeyboardShown = false;
  49. }
  50. }
  51. }
  52. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  53. }
  54.  
  55. public void setCallback(SoftKeyboardVisibilityChangeListener callback) {
  56. this.callback = callback;
  57. }
  58.  
  59.  
  60. public interface SoftKeyboardVisibilityChangeListener {
  61. void onSoftKeyboardShow();
  62. void onSoftKeyboardHide();
  63. }
  64. }
Add Comment
Please, Sign In to add comment