1

I want to display animation in dialog.

when the dialog is opened the animation start, i want only animation and text "Loading..." without title ,buttons or background to the dialog but alpha black to the backwards layout.

My Layout code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff111214">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
    <ImageView
        android:id="@+id/loader_image"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/loader"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/wait"
        android:layout_below="@id/loader_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp"
        android:textColor="#ffffff"/>
    </RelativeLayout>
</RelativeLayout>

My animation code:

    <rotate android:duration="500"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="55"
    android:repeatMode="restart"
    android:startOffset="0"
    android:toDegrees="360"/>
sandrstar
  • 12,503
  • 8
  • 58
  • 65
Daniel Mashraki
  • 262
  • 3
  • 11

1 Answers1

0

The right way is to use DialogFragment (there's one in Support Library also, so shouldn't be impossible even if you want to support old targets). Here's quick sample:

public class ProgressDialogFragment extends DialogFragment {

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

        setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_progress_dialog, container, false);

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();

        final View progressItem = getView().findViewById(R.id.loader_image);

        progressItem.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.rotation));
    }

    @Override
    public void onPause() {
        super.onPause();

        final View progressItem = getView().findViewById(R.id.loader_image);

        progressItem.clearAnimation();
    }
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#88111214">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
        <ImageView
            android:id="@+id/loader_image"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_centerHorizontal="true"
            android:src="@android:drawable/ic_delete"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wait"
            android:layout_below="@id/loader_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="13dp"
            android:textColor="#ffffff"/>
    </RelativeLayout>
</RelativeLayout>

Activity:

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (savedInstanceState == null) {
            final ProgressDialogFragment dialogFragment = new ProgressDialogFragment();

            dialogFragment.show(getFragmentManager(), "ProgressDialog");
        }
    }
}

Result is:

enter image description here

Another way is to use outdated Dialog, set your own layout, theme (like here dialog transparent background) and create some wrapper class to control the animation. It's error-prone, because code structure will not be clear and there might be some issues with Resume / Pause. I would recommend to use first way.

Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65