Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter and Exit Animations not working in Dialog Fragment

Tags:

android

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.

like image 606
Gurleen Sethi Avatar asked Jan 26 '17 08:01

Gurleen Sethi


People also ask

Is dialog fragment deprecated?

This method is deprecated. Called to do initial creation of a fragment. Override to build your own custom Dialog container.

How to show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is the difference between dialog and dialog fragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

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.

like image 177
Erik Melkersson Avatar answered Nov 09 '22 01:11

Erik Melkersson