Android: dialog animation stops working when dialog called from fragment - android

Good day. I faced problem, when dialog animation stops working when dialog called from fragment.
First I tried DialogFragment with custom style:
public static class QRDialog extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setCancelable(true);
ImageView im = new ImageView(getActivity());
im.setImageResource(R.drawable.qr_code_placeholder);
im.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
im.setClickable(true);
im.setScaleType(ImageView.ScaleType.FIT_XY);
im.setAdjustViewBounds(true);
im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
dialog.setView(im);
return dialog.create();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDialog().getWindow().getAttributes().windowAnimations = R.style.XSlideAnimation;
}
}
Those XSlideAnimation is simple animation style:
<style name="XSlideAnimation" >
<item name="android:windowEnterAnimation">#android:anim/slide_in_left</item>
<item name="android:windowExitAnimation">#android:anim/slide_out_right</item>
</style>
It didn't work.
Then I tried set custom animation for fragment transaction:
FragmentTransaction tr = getFragmentManager().beginTransaction();
tr.setCustomAnimations(R.anim.in, R.anim.out);
new QRDialog().show(tr, "dialog");
in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="50%"
android:toYDelta="0"
android:duration="500"
/>
</set>
out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="50%"
android:duration="500"
/>
</set>
No success either.
Then I tried just to get new Dialog instance and set animation for it:
final Dialog dialog = new Dialog(getActivity());
ImageView im = new ImageView(getActivity());
im.setImageResource(R.drawable.qr_code_placeholder);
im.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
im.setClickable(true);
im.setScaleType(ImageView.ScaleType.FIT_XY);
im.setAdjustViewBounds(true);
dialog.setContentView(im);
im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.getWindow().getAttributes().windowAnimations = R.style.XSlideAnimation;
dialog.setCancelable(true);
dialog.show();
And animation works perfect BUT only if I call dialog from activity. When I put exact same code in fragment, animation stop working. First approach works from activity too, but not in fragment.
How can I get working animation when creating dialog in fragment?
UPD 16-102017:
I see no answer or comments here so far, but somebody upvoted this issue once. So, just for update - in my case it's just started to work. I did literally nothing with code. Animation just appeared and staid couple days later during tests.

Related

Android - Open dialogue builder in fancy way

I have been using android dialog builder by inflating layouts in it. Everything is working fine and perfect. But now, I want to change the way of opening of the dialog builder. I want open the dialog builder like the swipe card. .i.e. From left to right OR top to bottom etc.
I know stackoverflow is not all about asking questions, but showing some effort at least. But issue is, I am not able to find any examples or clues on it.
Need some advice, or reference to be followed.
Thanks!
Dialog Opening Code:
final Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.prompt_dialoge);
dialog.setTitle("Draw your signature below");
Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
dialog.show();
Add style to your new Dialog() constructor like below.
final Dialog dialog = new Dialog(main.this, R.style.DialogStyle);
dialog.setContentView(R.layout.prompt_dialoge);
dialog.setTitle("Draw your signature below");
Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
dialog.show();
Add this style to your styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogStyle" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
</style>
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_in_right</item>
<item name="android:windowExitAnimation">#anim/slide_out_right</item>
</style>
</resources>
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fillAfter="true"
android:fromXDelta="100%p"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="00%p" />
</set>
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fillAfter="true"
android:fromXDelta="000%p"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="100%p" />
</set>

Enter and Exit Animations not working in Dialog Fragment

I have checked all the Stack Overflow Q/A on this, still can't find a solution.
Here are the files:
DialogFragment.java
package app.com.thetechnocafe.mealsquickie.Dialogs;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import app.com.thetechnocafe.mealsquickie.R;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by gurleensethi on 26/01/17.
*/
public class NewCategoryDialog extends DialogFragment {
#BindView(R.id.category_name_text_input_layout)
TextInputLayout mCategoryNameTextInputLayout;
#BindView(R.id.category_name_text_input_edit_text)
TextInputEditText mCategoryNameTextInputEditText;
#BindView(R.id.cancel_button)
Button mCancelButton;
#BindView(R.id.add_button)
Button mAddButton;
private OnAddCategoryListener mListener;
//Interface for callbacks
public interface OnAddCategoryListener {
void onCategoryAdded(String category);
}
//Instance method
public static NewCategoryDialog getInstance() {
return new NewCategoryDialog();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//Inflate the custom dialog
View root = LayoutInflater.from(getContext()).inflate(R.layout.dialog_new_category, container, false);
ButterKnife.bind(this, root);
setEventListeners();
//Set properties
getDialog().requestWindowFeature(STYLE_NO_TITLE);
setCancelable(false);
return root;
}
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
private void setEventListeners() {
mCancelButton.setOnClickListener(view -> getDialog().dismiss());
mAddButton.setOnClickListener(view -> validateAndSubmitFields());
}
private void validateAndSubmitFields() {
if (mListener != null) {
//Remove all the already existing errors
mCategoryNameTextInputLayout.setErrorEnabled(false);
String category = mCategoryNameTextInputEditText.getText().toString();
if (category.equals("")) {
mCategoryNameTextInputLayout.setError(getString(R.string.category_name_cannot_be_empty));
return;
}
mListener.onCategoryAdded(category);
} else {
Toast.makeText(getContext(), "No Listener attached for adding new category. Please contact the developer.", Toast.LENGTH_SHORT).show();
}
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
return dialog;
}
}
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0%" />
</set>
Showing the dialog:
DialogFragment dialog = NewCategoryDialog.getInstance();
dialog.show(getFragmentManager(), DIALOG_NEW_CATEGORY_TAG);
I have tried both getAttributes().windowAnimations and setWindowAnimations(), and have also tried it putting it in onActivityCreated, onCreateDialog, onCreateView, but it doesn't seem to work.
No matter which the solutions you pick you might have had the same problem as me.
I need to UNINSTALL the game from my development device before installing the new version for the changes to take effect.
I am not sure why but I guess it has to do with the optimized deployment on Android studio not recognizing the changes.
Try it as this.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getDialog().getWindow() != null) {
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
}
}
In your onStart of the fragment,
do the following:
// safety check
if (getDialog() == null) {
return;
}
// set the animations to use on showing and hiding the dialog
getDialog().getWindow().setWindowAnimations(
R.style.dialog_animation_slide);
The style set above should be defined like:
<style
name="dialog_animation_slide" >
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slid_down</item>
</style>
Be sure to put your slide_up.xml and slide_down.xml in res/anim directory
Do remove dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation; from onCreateDialog()
Let me know if this works for you!!!
If nothing work, maybe the device has animations deactivated from the developer options. restore default options and check.
Animations are a bit tricky when it comes to DialogFragment.
When you need to change the visibility or position of views in your layout, you should include subtle animations to help the user understand how the UI is changing.
So in order to achieve that lets take in mind that DialogFragment is a wrapper for the Dialog class, you should set a theme to your base Dialog to get the animation you want:
public class CustomDialogFragment extends DialogFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Set a theme on the dialog builder constructor!
AlertDialog.Builder builder =
new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );
builder
.setTitle( "Your title" )
.setMessage( "Your message" )
.setPositiveButton( "OK" , new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0%" />
</set>
styles.xml
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
</style>
<style name="MyAnimation.Window" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/slide_down</item>
<item name="android:windowExitAnimation">#anim/slide_up</item>
</style>
And that's all... Hope it helps. That is thanks to this answer: Show DialogFragment with animation growing from a point

DialogFragment enter animation not fires on first show

I set my own animations for dialog window enter but it not fires when I call .show() first time after activity created.
At the gif below you can see that first time animation is drop down and only second time it is slide up that I set.
DialogFragment:
public class DateChooseDialog extends DialogFragment {
public DateChooseDialog() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style.DialogFullScreen);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_date_choose, container);
}
#Override
public void onResume() {
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.BOTTOM;
params.windowAnimations = R.style.DialogSlideAnimation;
getDialog().getWindow().setAttributes(params);
super.onResume();
}
}
Animations:
<style name="DialogSlideAnimation" parent="Theme.AppCompat.Dialog">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_down</item>
</style>
<!-- slide_up -->
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromYDelta="0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%"
/>
<!-- slide_down -->
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromYDelta="0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%"
/>
This is how I show dialog:
DateChooseDialog dateChooseDialog = new DateChooseDialog();
dateChooseDialog.show(getFragmentManager(), "date_choose");
Problem is all about keyboard. It resizes your whole window and while keyboard disappears with slide down animation, your whole window resizes it self through sliding down.
Please add this lines to your manifest.
<activity (Your current activity)
...
android:windowSoftInputMode="adjustPan">
</activity>
With these lines, your keyboard will overlay window and it wont resize.
Next challange is detect keyboard is opened to delay animation if neccessary.
Good luck there

Animation List simply deosn't start

I'm working on a project which should have a progressdialog. But since i didn't find an easy way to style a progressdialog, i was thinking, that the easiest way is to create a custom dialog class with it's own style, and a frame by frame animation on it, and show it instead. Here is my code:
The xml layout for the dialog:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialog_background"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="#+id/loaddialog_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/loadanimation"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/loaddialog_text"
style="#style/SmallText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my dialog class:
public class LoadDialog extends Dialog
{
private TextView message;
ImageView image;
AnimationDrawable animation;
public LoadDialog(Context context)
{
super(context, R.style.Dialog);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.load_dialog);
message = (TextView) findViewById(R.id.loaddialog_text);
image = (ImageView) findViewById(R.id.loaddialog_animation);
image.setBackgroundResource(R.drawable.loadanimation);
animation = (AnimationDrawable) image.getBackground();
}
public void setText(String msg)
{
message.setText(msg);
}
#Override
public void show()
{
super.show();
animation.start();
}
#Override
public void dismiss()
{
animation.stop();
super.dismiss();
}
}
and the animation resource:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
<item android:drawable="#drawable/loadbar_01" android:duration="50" />
<item android:drawable="#drawable/loadbar_02" android:duration="50" />
<item android:drawable="#drawable/loadbar_03" android:duration="50"/>
</animation-list>
The problem is that the animation doesn't starts, when i try to show it. I know, there are a lots of topics about this problem, but here are the things, that i have tried:
Put the dialogs show() in different parts of my activity: onResume() and onCreate().
onWindowFocusChanged() is not an option, since i want to show a dialog. dialog shown ->focus change, dialog dismissed->focus change ->infinite amount of dialogs shown.
I have tried animation.setCallback(image), animation.setVisible(true, true), animation.invalidateSelf() none of them works.
I have tried image.post() and put a runnable in there as a parameter, and in the run method starting the animation, no luck.
At this point i'm starting to run out of options. The I'm only trying to show 3 images changing until the dialog is dismissed. Please if you know, what am i doing wrong, or any alternative, let me know!
Thanks in advance!
Change android:oneshot="true" to android:oneshot="false" so that it repeats. It should be currently only doing one cycle and since your show time is 50, that is really fast. I would also change that to maybe 200 or so..
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" >
<item android:drawable="#drawable/loadbar_01" android:duration="200" />
<item android:drawable="#drawable/loadbar_02" android:duration="200" />
<item android:drawable="#drawable/loadbar_03" android:duration="200"/>
</animation-list>
Also, you are setting the image in your xml layout, but animated the background, so you need to either change the xml layout to this:
<ImageView android:id="#+id/loaddialog_animation" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/loadanimation"
android:layout_marginRight="10dp" />
or change you code to get the src file rather than the background, like this:
image = (ImageView) findViewById(R.id.loaddialog_animation);
image.set(R.drawable.loadanimation);
animation = (AnimationDrawable) image.getDrawable();
Faced the same issue today. Placing the animation.start() in the onShow() method of OnShowListener did the trick for me.
public class CustomProgressDialog extends Dialog
{
ImageView progressImage;
AnimationDrawable frameAnimation;
public CustomProgressDialog(Context context)
{
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_progress);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressImage = (ImageView) findViewById(R.id.custom_progress_image);
progressImage.setBackgroundResource(R.anim.progress_anim);
frameAnimation = (AnimationDrawable) progressImage.getBackground();
OnShowListener osl = new OnShowListener()
{
#Override
public void onShow(DialogInterface dialog)
{
frameAnimation.start();
}
};
setOnShowListener(osl);
OnDismissListener odl = new OnDismissListener()
{
#Override
public void onDismiss(DialogInterface dialog)
{
frameAnimation.stop();
}
};
setOnDismissListener(odl);
}
}

Android animation not playing

I am trying to get a frame by frame animation to run on android 2.2 in a 2.2 vm machine using eclipse and i cannot for the life of me get it to run. It just sits there on the first frame of the animation.
Here is my activity code:
public class SpriteAnimationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView lView = (ImageView) findViewById(R.id.imageView1);
AnimationDrawable lAnimation = (AnimationDrawable)lView.getBackground();
lAnimation.start();
}
}
Here is my animation list xml file:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="#drawable/a1" android:duration="100" />
<item android:drawable="#drawable/a2" android:duration="100" />
<item android:drawable="#drawable/a3" android:duration="100" />
</animation-list>
I am using the imageView widget on my main screen to display the image by setting the background. I tried to follow the guide here http://developer.android.com/guide/topics/graphics/drawable-animation.html but I can't seem to get it to work
Here you can get help:
Simple tween animation example
Android animation example on Google Code
You have to start animation forcefully...
ImageView lView = (ImageView) findViewById(R.id.imageView1);
lView.setBackgroundResource(R.drawable.my_animation_list);
final AnimationDrawable lAnimation = (AnimationDrawable)lView.getBackground();
// Button btn=
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lAnimation.start();
}
});
You have to start the animation manually through button click listener or use the onWindowFocusChange listener like below
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
lAnimation.start();
}
}

Categories

Resources