7

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.

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
Gurleen Sethi
  • 2,299
  • 4
  • 18
  • 39

5 Answers5

2

Try it as this.

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (getDialog().getWindow() != null) {
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
        }
    }
Deneb Chorny
  • 311
  • 2
  • 14
1

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

ravi
  • 743
  • 4
  • 26
0

If nothing work, maybe the device has animations deactivated from the developer options. restore default options and check.

Driss Bounouar
  • 2,852
  • 28
  • 45
0

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

Alex Rivas
  • 79
  • 1
  • 7
0

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.

Erik Melkersson
  • 660
  • 5
  • 16