@OverridepublicvoidonConfigurationChanged(ConfigurationnewConfig){super.onConfigurationChanged(newConfig);// Checks whether a hardware keyboard is availableif(newConfig.hardKeyboardHidden==Configuration.HARDKEYBOARDHIDDEN_NO){Toast.makeText(this,"keyboard visible",Toast.LENGTH_SHORT).show();}elseif(newConfig.hardKeyboardHidden==Configuration.HARDKEYBOARDHIDDEN_YES){Toast.makeText(this,"keyboard hidden",Toast.LENGTH_SHORT).show();}}
publicclassKeyPreImeEditTextextendsEditText{publicKeyPreImeEditText(Contextcontext){super(context);}publicKeyPreImeEditText(Contextcontext,AttributeSetattrs){super(context,attrs);}publicKeyPreImeEditText(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);}@OverridepublicbooleanonKeyPreIme(intkeyCode,KeyEventevent){if(keyCode==KeyEvent.KEYCODE_BACK){Log.e("keyboard","onKeyPreIme we got back");}returnsuper.onKeyPreIme(keyCode,event);}}
publicclassKeyboardAwareLinearLayoutextendsLinearLayout{publicKeyboardAwareLinearLayout(Contextcontext){super(context);}publicKeyboardAwareLinearLayout(Contextcontext,AttributeSetattrs){super(context,attrs);}publicKeyboardAwareLinearLayout(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){finalintproposedHeight=MeasureSpec.getSize(heightMeasureSpec);finalintactualHeight=getHeight();if(actualHeight>proposedHeight){Log.e("keyboard","guess keyboard is shown");}else{Log.e("keyboard","guess keyboard has been hidden");}super.onMeasure(widthMeasureSpec,heightMeasureSpec);}}
privatebooleanmKeyboardUp;privatevoidsetListenerToRootView(){finalViewrootView=getWindow().getDecorView().findViewById(android.R.id.content);rootView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener(){@OverridepublicvoidonGlobalLayout(){finalintheaderHeight=getActionBarHeight()+getStatusBarHeight();intheightDiff=rootView.getRootView().getHeight()-rootView.getHeight();if(heightDiff>headerHeight){Log.e("keyboard","keyboard is up");if(!mKeyboardUp){mKeyboardUp=true;}}elseif(mOpen){Log.e("keyboard","keyboard is hidden");mKeyboardUp=false;}}});}