Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android L Circular Reveal on a Dialog

I'm trying to create a circular reveal on a Dialog. I would think that you would be able to get the View in the onCreate of the Dialog, hide it, and then reveal it using ViewAnimationUtils.createCircularReveal.

I'm having issues getting the View due to a requestWindowFeature() error, which I can't seem to resolve.

How exactly do you apply the Material Design Animations to Dialogs?

EDIT

I have successfully hidden the dialog by applying the code here to the dismiss() method. But when I try to do the circular reveal to show, the view stays invisible.

like image 469
tyler Avatar asked Aug 15 '14 18:08

tyler


People also ask

How do you create a circular reveal effect when starting a new activity?

createCircularReveal() method enables you to animate a clipping circle to reveal or hide a view. To reveal a previously invisible view using this effect: // previously invisible view View myView = findViewById(R. id.

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

What is Alpha animation in Android?

An Alpha Animation is animation that controls the alpha level of an object, i.e. fading it in and out.


1 Answers

Here is the solution:

The Fragment

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        if(rootView != null){
            rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    rootView.findViewById(R.id.btn_reveal).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showDialog();
                        }
                    });
                }
            });
        }

        return rootView;
    }


    private void showDialog(){
        final View dialogView = View.inflate(getActivity(), R.layout.dialog_view, null);

        AlertDialog.Builder builder =  new AlertDialog.Builder(getActivity());
        builder.setView(dialogView)
               .setCancelable(false);


        final AlertDialog dialog = builder.create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                revealShow(dialogView, true, null);
            }
        });
        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                revealShow(dialogView, false, dialog);
            }
        });
        dialogView.findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                revealShow(dialogView, false, dialog);
            }
        });


        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();

    }

    private void revealShow(View rootView, boolean reveal, final AlertDialog dialog){
        final View view = rootView.findViewById(R.id.reveal_view);
        int w = view.getWidth();
        int h = view.getHeight();
        float maxRadius = (float) Math.sqrt(w * w / 4 + h * h / 4);

        if(reveal){
            Animator revealAnimator = ViewAnimationUtils.createCircularReveal(view,
                    w / 2, h / 2, 0, maxRadius);

            view.setVisibility(View.VISIBLE);
            revealAnimator.start();

        } else {

            Animator anim =
                    ViewAnimationUtils.createCircularReveal(view, w / 2, h / 2, maxRadius, 0);

            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    dialog.dismiss();
                    view.setVisibility(View.INVISIBLE);

                }
            });

            anim.start();
        }

    }
}

And the XML for the dialog_view:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/transparent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reveal_view"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:background="@color/theme_primary"
    android:visibility="invisible">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="First Name"
        android:layout_marginTop="20dp"
        android:textColor="@android:color/white"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Last Name"
        android:textColor="@android:color/white"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Additional Info"
        android:textColor="@android:color/white"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:textColor="@android:color/white"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:textColor="@android:color/white"/>

     </RadioGroup>

       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            android:textColor="@android:color/white"
            android:background="?android:attr/selectableItemBackground"/>
        <Button
            android:id="@+id/btn_save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save"
            android:textColor="@android:color/white"
            android:background="?android:attr/selectableItemBackground"/>
        </LinearLayout>
   </LinearLayout>

like image 82
Tinashe Avatar answered Oct 28 '22 06:10

Tinashe