Last active
September 21, 2015 06:37
-
-
Save quangdh/c1e9bf00054d5e3a1e7e to your computer and use it in GitHub Desktop.
Capture the 'virtual keyboard show/hide' event in Android
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
<?xml version="1.0" encoding="utf-8"?> | |
<com.danbistudio.apps.view.ResizeRelativeLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/layout_contain" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/white" | |
android:orientation="vertical" > | |
<!-- contain --> | |
</com.danbistudio.apps.view.ResizeRelativeLayout> |
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
public class MainActivity extends Activity | |
implements ResizeRelativeLayout.OnResizeLayout{ | |
//... | |
private ResizeRelativeLayout layout; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContainView(R.layout.activity_main); | |
//... | |
layout = (ResizeRelativeLayout) findViewById(R.id.layout_contain); | |
layout.setOnResizeLayout(this); | |
} | |
@Override | |
public void onSoftKeyboardHide() { | |
//... | |
} | |
@Override | |
public void onSoftKeyboardShow() { | |
//... | |
} | |
} |
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.danbistudio.apps.view; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.KeyEvent; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.widget.RelativeLayout; | |
/** | |
* Created by quangdh on 8/13/15. | |
*/ | |
public class ResizeRelativeLayout extends RelativeLayout | |
implements ViewTreeObserver.OnGlobalLayoutListener{ | |
OnResizeLayout mOnResizeLayout = null; | |
private boolean isKeyboardShown = false; | |
private int currentHeight = Integer.MAX_VALUE; | |
public void setOnResizeLayout(OnResizeLayout mOnResizeLayout){ | |
this.mOnResizeLayout = mOnResizeLayout; | |
} | |
public ResizeRelativeLayout(Context context) { | |
super(context); | |
getViewTreeObserver().addOnGlobalLayoutListener(this); | |
} | |
public ResizeRelativeLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
getViewTreeObserver().addOnGlobalLayoutListener(this); | |
} | |
public ResizeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
getViewTreeObserver().addOnGlobalLayoutListener(this); | |
} | |
public ResizeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
getViewTreeObserver().addOnGlobalLayoutListener(this); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec); | |
final int actualHeight = getHeight(); | |
Log.i("ResizeRelativeLayout","onMeasure()... "+proposedheight+" "+ actualHeight); | |
if(mOnResizeLayout != null) { | |
if (actualHeight > proposedheight) { | |
if (!isKeyboardShown) { | |
isKeyboardShown = true; | |
mOnResizeLayout.onSoftKeyboardShow(); | |
} | |
} | |
} | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
@Override | |
public boolean dispatchKeyEventPreIme(KeyEvent event) { | |
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { | |
if(mOnResizeLayout != null) { | |
if (isKeyboardShown) { | |
isKeyboardShown = false; | |
mOnResizeLayout.onSoftKeyboardHide(); | |
} | |
} | |
} | |
return super.dispatchKeyEventPreIme(event); | |
} | |
@Override | |
public void onGlobalLayout() { | |
int rootViewHeight = getRootView().getHeight(); | |
Log.i("ResizeRelativeLayout","onGlobalLayout()... "+rootViewHeight + " ... " +currentHeight); | |
if(rootViewHeight > currentHeight){ | |
if(mOnResizeLayout != null) { | |
if (isKeyboardShown) { | |
isKeyboardShown = false; | |
mOnResizeLayout.onSoftKeyboardHide(); | |
} | |
} | |
} | |
currentHeight = rootViewHeight; | |
} | |
public interface OnResizeLayout{ | |
public void onSoftKeyboardShow(); | |
public void onSoftKeyboardHide(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment