Android Keyboard Adjust Resize
I am Developing one Application which contains Activity and Fragment. In Fragment Layout I used Relative Layout as Parent Layout,one button in bottom and in between Scrollview. Scr
Solution 1:
Try this in AndroidMenifest
android:windowSoftInputMode="stateAlwaysHidden|adjustResize
In your editext use this
android:inputType="textMultiLine|textPostalAddress"android:scrollbars="vertical"
Solution 2:
There are Android's bugs in this. After struggling a lot, I am able to come-up with a smooth workaround to this problem. it is a one line solution, but it has some pre-reqs. The one line is:
AndroidBug5497Workaround.assistActivity(this, R.id.LayoutInScrollView);
Your xml Layout must be like:
RelativeLayout{
HeaderView{}
ScrollView{
LinearLayout{
@+id/LayoutInScrollView
}
}
FooterView{} // the buttons u want to appear above keyboard
}
If you are not using Full Screen, the following class should be enough:
classAndroidBug5497Workaround{
View svChildLayout;
int originalGravity;
Activity activity;
/**
* @param activity
* @param svChildLayoutId id of the layout that is the first child of the center ScrollView
*/publicstaticvoidassistActivity (Activity activity, int svChildLayoutId) {
new AndroidBug5497Workaround(activity, svChildLayoutId);
}
privateAndroidBug5497Workaround(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() {
publicvoidonGlobalLayout() {
possiblyResizeChildOfContent2();
}
});
}
privatevoidpossiblyResizeChildOfContent2() {
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;
}
}
privatevoidonKeyboardVisible() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = Gravity.TOP;
svChildLayout.requestLayout();
final ScrollView parentSv = (ScrollView) svChildLayout.getParent();
parentSv.post(new Runnable() {
@Override
publicvoidrun() {
View focusedEditText = activity.getWindow().getCurrentFocus();
parentSv.smoothScrollTo(0, focusedEditText.getTop() );
}
});
}
privatevoidonKeyboardHidden() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = originalGravity;
svChildLayout.requestLayout();
}
}
If you are using full screen, you'll need the following class (modified from windowSoftInputMode="adjustResize" not working with translucent action/navbar ):
publicclassAndroidBug5497Workaround {
// 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.publicstaticvoidassistActivity (Activity activity, int svChildLayoutId) {
new AndroidBug5497Workaround(activity, svChildLayoutId);
}
private View mChildOfContent;
privateint usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
View svChildLayout;
int originalGravity;
Activity activity;
privateAndroidBug5497Workaround(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() {
publicvoidonGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
privatevoidpossiblyResizeChildOfContent() {
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;
}
}
privateintcomputeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
privatevoidonKeyboardVisible() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = Gravity.TOP;
svChildLayout.requestLayout();
final ScrollView parentSv = (ScrollView) svChildLayout.getParent();
parentSv.post(new Runnable() {
@Override
publicvoidrun() {
View focusedEditText = activity.getWindow().getCurrentFocus();
parentSv.smoothScrollTo(0, focusedEditText.getTop() );
}
});
}
privatevoidonKeyboardHidden() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = originalGravity;
svChildLayout.requestLayout();
}
}
Post a Comment for "Android Keyboard Adjust Resize"