How to Stop Activity Resizing/Pushing Up on Keyboard Open in Dialog

How to prevent android soft keyboard from resizing my activity

put android:layout_height solid not match_parent , fill_parent , wrape_content
it should be a solid number like android:layout_height="480dp" or whatever

or we can specify it programmatically

using this code

Functions.setActivityDiemention(this,
Functions.getScreenWidth(this) - 20,
Functions.getScreenHeight(this) - 20);

before setContentView

setActivityDiemention code

public static final void setActivityDiemention(Activity activity ,int width,int hieght) {
android.view.WindowManager.LayoutParams params = activity.getWindow()
.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = params.width;
activity.getWindow().setAttributes(
(android.view.WindowManager.LayoutParams) params);
}

getScreenWidth and getScreenHeight

Code

public static int getScreenHeight(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
if (getAPILevel() < 13) {
return display.getHeight();
}
display.getSize(size);
return size.y;
}

public static int getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
if (getAPILevel() < 13) {
return display.getWidth();
}
display.getSize(size);
return size.x;
}

and must use

android:windowSoftInputMode="adjustPan"> 

Android: How do I prevent the soft keyboard from pushing my view up?

You can simply switch your Activity's windowSoftInputModeflag to adjustPan in your AndroidMainfest.xml file inside your activity tag.

Check the official documentation for more info.

<activity
...
android:windowSoftInputMode="adjustPan">
</activity>

If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.

The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)

  1. Content being smashed together
  2. Content hanging off the screen
  3. Content being inacccessable due to it being underneath the keyboard

even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.

DialogFragment is always resized when soft keyboard is opened

I have finally found an solution to my problem.

The creation of the dialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);

if (!hasTitle()) {
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}

if (position != null) {
WindowManager.LayoutParams windowParams = dialog.getWindow().getAttributes();
windowParams.x = position.x;
windowParams.y = position.y;
dialog.getWindow().setAttributes(windowParams);
} else {
WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(hasTitle() ? Color.WHITE : Color.TRANSPARENT));
dialog.getWindow().setGravity(Gravity.TOP | Gravity.START);

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

return dialog;
}

The creation of the fragment (i customize the style of the dialog - it is very important to use No_FRAME):

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// We customize the style of this dialog - we remove the frames of the dialogs and leave the drawing to the
// onCreateView() method, and we use the custom theme for dialogs - this is a special theme which has no
// Background and the status bar is left unchanged
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.Theme_Dialog);
}

The custom theme (I reuse all my styles, so that the dialog looks the same):

<!-- Custom theme for dialogs - this will use the same styling as the main theme, but will disable the background
of the window, and it will also leave the status bar color unchanged -->
<style name="Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<!-- We do not want to change the status bar color -->
<item name="colorPrimaryDark">@color/transparent</item>
</style>

Now my dialog fragment looks like it is FullScreen and it doesn't resize anymore when the soft keyboard is opened, thus creating the flickering effect when manually changing it's size.
The important piece was the DialogFragment.STYLE_NO_FRAME.

I hope this will help others with similar problems.



Related Topics



Leave a reply



Submit