I am trying to add animation to alert dialog when it is dismiss when user touch outside of it. by default when it is touched outside, the dialog just fades out or disappears. Is there a way to override the default dimiss or cancel? I was able to add animation when a button is pressed and when it call but cannot figure out how to add the animation to the default close action when it is touched outside. please help. Thanks in advance
I tried but it did not work
alert.setOnCancelListener()
alert.setOnDismissListener()
here is my code:
View rl = getActivity().findViewById(R.id.map) ;
AlertDialog.Builder alert = new
AlertDialog.Builder(rl.getContext(), R.style.CFDialog);
LayoutInflater inflater =
getActivity().getLayoutInflater();
View v =
inflater.inflate(R.layout.dialog_footer_layout, null);
Animation transition_in_view =
AnimationUtils.loadAnimation(rl.getContext(),
R.anim.alert_present);
Animation transition_out_view =
AnimationUtils.loadAnimation(rl.getContext(),
R.anim.alert_dismiss);
//customer animation appearance
v.setAnimation( transition_in_view );
v.startAnimation( transition_in_view );
alert.setView(v);
alert.setOnCancelListener(new
DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface
dialogInterface) {
v.startAnimation(transition_out_view);
}
});
alert.setOnDismissListener()
Button button =
v.findViewById(R.id.configuration_toggle_button);
AlertDialog helpDialog = alert.create();
button.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
v.startAnimation(transition_out_view);
transition_out_view.setAnimationListener(new
Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation
animation) {
}
@Override
public void onAnimationEnd(Animation
animation) {
helpDialog.dismiss();
}
@Override
public void onAnimationRepeat(Animation
animation) {
}
});
}
});
helpDialog.setOnCancelListener(new
DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface
dialogInterface) {
v.startAnimation(transition_out_view);
return;
}
});
helpDialog.setOnDismissListener(new
DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface
dialogInterface) {
v.startAnimation(transition_out_view);
return;
}
});
// Hide after some seconds
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (helpDialog.isShowing()) {
v.startAnimation(transition_out_view);
transition_out_view.setAnimationListener(new
Animation.AnimationListener() {
@Override
public void
onAnimationStart(Animation animation) {
}
@Override
public void
onAnimationEnd(Animation animation) {
helpDialog.dismiss();
}
@Override
public void
onAnimationRepeat(Animation animation) {
}
});
}
}
};
alert.setOnDismissListener(new
DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
handler.removeCallbacks(runnable);
}
});
handler.postDelayed(runnable, 5000);
helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp =
helpDialog.getWindow().getAttributes();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
wmlp.gravity = Gravity.TOP;
wmlp.y = 200; //y position
helpDialog.show();
} else {
// if (Build.VERSION.SDK_INT <=
Build.VERSION_CODES.O)
wmlp.gravity = Gravity.TOP;
wmlp.y = 180; //y position
helpDialog.show();
}
Here is the style to the dialog:
<style name="CFDialog" parent="Theme.AppCompat.Translucent">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<item name="colorPrimaryDark">@color/transparent</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>