Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Progress Dialog hides the soft keyboard android

Query Description: I am using custom progress dialog with the transparent theme. And this works fine. What problem I am facing is, when my keyboard is open and if I show Custom Progress Dialog then It force closes my Keyboard. However, if I use default Progress Dialog then it works fine.

Here is Default Progress Dialog: As you can see the keyboard is still there even after progress dialog appears. I want to implement the same in Custom Progress Dialog.

Here is Custom Progress Dialog: As you can see, the keyboard gets disappears when the dialog is showing. It feels tedious, every time the user has to open the keyboard.

Here is my code:

public class CustomProgressDialog extends Dialog {
private ImageView imageView;
private Context mContext;

public CustomProgressDialog(Context context) {
    super(context, R.style.DialogTheme);
    mContext = context;
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    WindowManager.LayoutParams wlmp = getWindow().getAttributes();
    wlmp.gravity = Gravity.CENTER_HORIZONTAL;
    getWindow().setAttributes(wlmp);
    setTitle(null);
    setCancelable(true);
    //setOnCancelListener(null);
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(200, 200);
    imageView = new ImageView(context);
    imageView.setBackgroundResource(R.drawable.custom_dialog);
    layout.addView(imageView, params);
    addContentView(layout, params);
}

@Override
public void show() {
    super.show();
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
}

@Override
public void dismiss() {
    super.dismiss();
}
}

Dialog theme in style.xml:

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/actionbar_gradient_endColor</item>
</style>

Any Help will be appreciated.

<EditText android:id="@+id/et_search_category" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@+id/imgClearSearch" 
    android:background="@drawable/rounded_rdt_txt" 
    android:hint="Search here..." 
    android:layout_centerVertical="true" 
    android:singleLine="true" />
like image 514
Ronak Thakkar Avatar asked Oct 14 '17 10:10

Ronak Thakkar


2 Answers

Finally, I come up with the solution:

I saw Default Progress Dialog extends AlertDialog. However, in my code, I was extending Dialog class.

Now I am extending ProgressDialog instead of Dialog and this works like a charm.

Here is the code:

public class MyCustomProgressDialog extends ProgressDialog {
private ImageView imageView;

public MyCustomProgressDialog(Context context) {
    super(context, R.style.TransparentProgressDialog);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wlmp = getWindow().getAttributes();
    wlmp.gravity = Gravity.CENTER;
    getWindow().setAttributes(wlmp);
    setTitle(null);
    setCancelable(false);
    setOnCancelListener(null);

}

public MyCustomProgressDialog(Context context, int theme) {
    super(context, theme);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_custom_progress_dialog);

    imageView = (ImageView) findViewById(R.id.centerImage);
    imageView.setBackgroundResource(R.drawable.custom_dialog);

}

@Override
public void show() {
    super.show();
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
}

@Override
public void dismiss() {
    super.dismiss();
}
}

Layout File:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/centerImage"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_centerInParent="true"
    android:visibility="visible" />
</RelativeLayout>

style.xml:

<style name="TransparentProgressDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

How to call?:

ProgessDialog mProgressDialog = new MyCustomProgressDialog(this);
mProgressDialog.show(); //to show
mProgressDialog.dismiss(); //to dismiss
like image 64
Ronak Thakkar Avatar answered Sep 30 '22 12:09

Ronak Thakkar


1.know about that whether soft keyboard is showing or not .

2.show or hide soft keyboard

Try this .

private Context context;
private boolean isShow = false;

// soft keyboard is show or not
private boolean isSoftShowing() {
    // screen height
    int screenHeight = getWindow().getDecorView().getHeight();
    Rect rect = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    return screenHeight - rect.bottom - getSoftButtonsBarHeight() != 0;
}

private int getSoftButtonsBarHeight() {
    DisplayMetrics metrics = new DisplayMetrics();
    // screen height ,not include navigation
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int usableHeight = metrics.heightPixels;
    // real screen height
    ((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    int realHeight = metrics.heightPixels;
    if (realHeight > usableHeight) {
        return realHeight - usableHeight;
    } else {
        return 0;
    }
}

@Override
public void show() {
    super.show();
    // hideSoftKeyboard
    if(isSoftShowing()){
        ((Activity)context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        isShow = true;
    }
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
}

@Override
public void dismiss() {
    super.dismiss();
    if(isShow){
        ((Activity)context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
like image 26
KeLiuyue Avatar answered Sep 30 '22 12:09

KeLiuyue