Resize Android Keyboard

I am developing one application that contains Activity and Fragment . In the fragment layout, I used the Relative Layout as the Parent Layout, one button at the bottom and between the Scrollview. Scrollview contains Scrollview edit fields. If I editTextBox Last editTextBox in scrollview My keyboard hides Fragment . I tried adjustpan|adjustresize in the manifest as well as in my Fragment , but haven't solved the problem yet.

+4
source share
2 answers

There are Android errors in this. After a lot, I can come up with a smooth solution to this problem. This is a one line solution, but it has some preliminary requirements. One line:

 AndroidBug5497Workaround.assistActivity(this, R.id.LayoutInScrollView); 

Your xml layout should look like this:

 RelativeLayout{ HeaderView{} ScrollView{ LinearLayout{ @+id/LayoutInScrollView } } FooterView{} // the buttons u want to appear above keyboard } 

If you are not using full-screen mode, the following class should be sufficient:

 class AndroidBug5497Workaround{ View svChildLayout; int originalGravity; Activity activity; /** * @param activity * @param svChildLayoutId id of the layout that is the first child of the center ScrollView */ public static void assistActivity (Activity activity, int svChildLayoutId) { new AndroidBug5497Workaround(activity, svChildLayoutId); } private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) { this.activity = activity; svChildLayout = activity.findViewById(svChildLayoutId); originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity; //Add listener FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { possiblyResizeChildOfContent2(); } }); } private void possiblyResizeChildOfContent2() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); int heightDifference = usableHeightSansKeyboard - usableHeightNow; if (heightDifference > (usableHeightSansKeyboard/4)) { // keyboard probably just became visible onKeyboardVisible(); } else { // keyboard probably just became hidden onKeyboardHidden(); } usableHeightPrevious = usableHeightNow; } } private void onKeyboardVisible() { ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); params.gravity = Gravity.TOP; svChildLayout.requestLayout(); final ScrollView parentSv = (ScrollView) svChildLayout.getParent(); parentSv.post(new Runnable() { @Override public void run() { View focusedEditText = activity.getWindow().getCurrentFocus(); parentSv.smoothScrollTo(0, focusedEditText.getTop() ); } }); } private void onKeyboardHidden() { ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); params.gravity = originalGravity; svChildLayout.requestLayout(); } } 

If you use full-screen mode, you will need the following class (modified from windowSoftInputMode = "regResize" does not work with translucent action / navigation ):

  public class AndroidBug5497Workaround { // For more information, see https://code.google.com/p/android/issues/detail?id=5497 // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. public static void assistActivity (Activity activity, int svChildLayoutId) { new AndroidBug5497Workaround(activity, svChildLayoutId); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; View svChildLayout; int originalGravity; Activity activity; private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) { this.activity = activity; svChildLayout = activity.findViewById(svChildLayoutId); originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity; 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 onKeyboardVisible(); frameLayoutParams.height = usableHeightSansKeyboard - heightDifference; } else { // keyboard probably just became hidden onKeyboardHidden(); frameLayoutParams.height = usableHeightSansKeyboard; } mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top); } private void onKeyboardVisible() { ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); params.gravity = Gravity.TOP; svChildLayout.requestLayout(); final ScrollView parentSv = (ScrollView) svChildLayout.getParent(); parentSv.post(new Runnable() { @Override public void run() { View focusedEditText = activity.getWindow().getCurrentFocus(); parentSv.smoothScrollTo(0, focusedEditText.getTop() ); } }); } private void onKeyboardHidden() { ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); params.gravity = originalGravity; svChildLayout.requestLayout(); } } 
0
source

Try it on AndroidMenifest

 android:windowSoftInputMode="stateAlwaysHidden|adjustResize 

In editext use

 android:inputType="textMultiLine|textPostalAddress" android:scrollbars="vertical" 
+1
source

All Articles