Luis A. Florit
Luis A. Florit

Reputation: 2609

Setting animation and no frame for AlertDialog

I'm struggling with this for hours now, sorry if this is a stupid question.

I want to open an AlertDialog (dimming the background) with an animation. The dialog view is a WebView. I tried two ways:

1) With a xml style AnimatedDialog:

<style name="AnimatedDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/AnimatedDialogAnimation</item>
</style>

calling in the code

 builder = new AlertDialog.Builder(context, R.style.AnimatedDialog);

The problem with this approach is that the dialog has an ugly frame, probably because the parent="@android:style/Theme.Dialog" thing is wrong, but I couldn't find which is the correct one for an AlertDialog. So I tried this:

2) Through a WindowManager.LayoutParams:

    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
    lp.windowAnimations = R.style.AnimatedDialog;
    dialog.getWindow().setAttributes(lp);

The problem with this now is that the dialog is not animated (why??).

In addition, in both cases, the dialog 'flashes' briefly before shown, in other words, I can see it being 'build'. Maybe because of the WebView?

Can someone guide me how to do this properly?

Thanks!!

Upvotes: 0

Views: 2690

Answers (2)

Emre Kilinc Arslan
Emre Kilinc Arslan

Reputation: 2189

here is my Custom Animated Alertdialog

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.MyDialogAnimationTheme);
    builder.setCancelable(true);
    LayoutInflater layoutInflater = getActivity().getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.alert_can_continue,null);
    relativeLayout = (RelativeLayout) view.findViewById(R.id.call_layout_holder);
    relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });
    positive = (TextView) view.findViewById(R.id.tv_positive);
    positive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    negative = (TextView) view.findViewById(R.id.tv_negative);
    negative.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    builder.setView(view);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

and this is styles.xml

 <style name="MyDialogAnimationTheme" parent="Base.Theme.AppCompat.Dialog.Alert">
    <item name="android:windowAnimationStyle">@style/MyDialogAnimation.Window</item>
    </style>
<style name="MyDialogAnimation.Window" parent="@android:style/Animation.Dialog">
        <item name="android:windowEnterAnimation">@anim/anim_in</item>
        <item name="android:windowExitAnimation">@anim/anim_out</item>
    </style>

Upvotes: 1

Luis A. Florit
Luis A. Florit

Reputation: 2609

After lots of work, I figured out how to do it. In this way, I'm not touching the default AlertDialog theme.

    ...
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setIcon(R.drawable.ic_launcher).setView(myMsg);
    AlertDialog dialog = builder.create();

    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
    lp.windowAnimations = R.style.AnimateDialog;

    if (fillscreen) {
        lp.height = LayoutParams.FILL_PARENT;
        lp.width = LayoutParams.FILL_PARENT;
    } else { 
        lp.height = LayoutParams.WRAP_CONTENT;
        lp.width = LayoutParams.WRAP_CONTENT;
    }
    if (fullscreenpref)
        lp.flags = android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN |
                   android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;

    dialog.show();
    dialog.getWindow().setAttributes(lp);
}

If I invert the last two lines, the dialog crashes when changing the orientation, because of this: How to handle screen orientation change when progress dialog and background thread active?

Still, the dialog 'flickers' slightly while it opens. I have no idea why.

Upvotes: 0

Related Questions