Created
March 12, 2018 05:22
-
-
Save abhinav272/7c10b5ca05102c7f7f84c3ee0223dd4f to your computer and use it in GitHub Desktop.
Keyboard issue with fullscreen activity >> https://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/28896520#28896520
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 AndroidBug5497Workaround { | |
// For more information, see https://issuetracker.google.com/issues/36911528 | |
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set. | |
public static void assistActivity (Activity activity) { | |
new AndroidBug5497Workaround(activity); | |
} | |
private View mChildOfContent; | |
private int usableHeightPrevious; | |
private FrameLayout.LayoutParams frameLayoutParams; | |
private AndroidBug5497Workaround(Activity activity) { | |
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); | |
mChildOfContent = content.getChildAt(0); | |
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
public void onGlobalLayout() { | |
possiblyResizeChildOfContent(); | |
} | |
}); | |
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); | |
} | |
private void possiblyResizeChildOfContent() { | |
int usableHeightNow = computeUsableHeight(); | |
if (usableHeightNow != usableHeightPrevious) { | |
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); | |
int heightDifference = usableHeightSansKeyboard - usableHeightNow; | |
if (heightDifference > (usableHeightSansKeyboard/4)) { | |
// keyboard probably just became visible | |
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference; | |
} else { | |
// keyboard probably just became hidden | |
frameLayoutParams.height = usableHeightSansKeyboard; | |
} | |
mChildOfContent.requestLayout(); | |
usableHeightPrevious = usableHeightNow; | |
} | |
} | |
private int computeUsableHeight() { | |
Rect r = new Rect(); | |
mChildOfContent.getWindowVisibleDisplayFrame(r); | |
return (r.bottom - r.top); | |
} | |
} |
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 KeyboardUtil { | |
private View decorView; | |
private View contentView; | |
public KeyboardUtil(Activity act, View contentView) { | |
this.decorView = act.getWindow().getDecorView(); | |
this.contentView = contentView; | |
//only required on newer android versions. it was working on API level 19 | |
if (Build.VERSION.SDK_INT >= 19) { | |
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); | |
} | |
} | |
public void enable() { | |
if (Build.VERSION.SDK_INT >= 19) { | |
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); | |
} | |
} | |
public void disable() { | |
if (Build.VERSION.SDK_INT >= 19) { | |
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener); | |
} | |
} | |
//a small helper to allow showing the editText focus | |
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
Rect r = new Rect(); | |
//r will be populated with the coordinates of your view that area still visible. | |
decorView.getWindowVisibleDisplayFrame(r); | |
//get screen height and calculate the difference with the useable area from the r | |
int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels; | |
int diff = height - r.bottom; | |
//if it could be a keyboard add the padding to the view | |
if (diff != 0) { | |
// if the use-able screen height differs from the total screen height we assume that it shows a keyboard now | |
//check if the padding is 0 (if yes set the padding for the keyboard) | |
if (contentView.getPaddingBottom() != diff) { | |
//set the padding of the contentView for the keyboard | |
contentView.setPadding(0, 0, 0, diff); | |
} | |
} else { | |
//check if the padding is != 0 (if yes reset the padding) | |
if (contentView.getPaddingBottom() != 0) { | |
//reset the padding of the contentView | |
contentView.setPadding(0, 0, 0, 0); | |
} | |
} | |
} | |
}; | |
/** | |
* Helper to hide the keyboard | |
* | |
* @param act | |
*/ | |
public static void hideKeyboard(Activity act) { | |
if (act != null && act.getCurrentFocus() != null) { | |
InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE); | |
inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment