Home » Android » dialog – How to prevent android soft keyboard from resizing my activity

dialog – How to prevent android soft keyboard from resizing my activity

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’ve an activity that specified as dialog with this line in Manifest like this

android:theme="@android:style/Theme.Dialog"

so how to prevent soft Keyboard from pushing it up and resizing ??

How to&Answers:

You can simply switch your activity’s windowSoftInputMode flag to “adjustPan”. Check the official documentation for more info.

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

If you are using ScrollView add this too android:isScrollContainer="false" to the ScrollView.

Try it..

Answer:

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">