Thursday, 9 May 2013

Custom Animation Dialog in Android

Hello friends today i post sample code of custom animation dialog. i every app there will be thread is used for loading data from server or Database, here is the post which you can create custom dialog for your application with its themes.


Create dialog.xml in res folder :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/loader1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/pharma" />

</RelativeLayout>        

Create progress_anim.xml in res-anim folder :

<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite" />


if you want loading dialog in particular activity.Here is the class file in this case :

public class MainActivity extends Activity
{

       @Override
       protected void onCreate(Bundle savedInstanceState)
       {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.dialog);
             
              ImageView imageView=(ImageView) findViewById(R.id.imageView1);
             
              Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.progress_anim);
              a.setDuration(1000);
              imageView.startAnimation(a);
             
              a.setInterpolator(new Interpolator()
              {
                  private final int frameCount = 8;

                  @Override
                  public float getInterpolation(float input)
                  {
                      return (float)Math.floor(input*frameCount)/frameCount;
                  }
              });
       }

if you want loading dialog with whole application with thread/AsyncTask .Here is the class file: it has different constructor for different look of dialog.

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.ImageView;

public class LodingDialog extends Dialog
{
       private Dialog mpd = null;
       private LayoutInflater lyt_Inflater = null;
       public LodingDialog(Context context)
       {     
              super(context);
              LodingDialog1(context,"Loading...",false);
       }


       public LodingDialog(Context context,String LoadingText)
       {
              super(context);
              LodingDialog1(context,LoadingText,false);

       }

       public LodingDialog(Context context,String LoadingText, boolean cancelable)
       {
              super(context);
              LodingDialog1(context,LoadingText,cancelable);
       }

       public void LodingDialog1(Context context,String LoadingText, boolean cancelable)
       {
              lyt_Inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

              final View view_lyt = lyt_Inflater.inflate(     R.layout.dialognull);

              ImageView imageView = (ImageView) view_lyt.findViewById(R.id.imageView1);
              Animation a = AnimationUtils.loadAnimation(context, R.anim.progress_anim);
              a.setDuration(2000);
              imageView.startAnimation(a);

              a.setInterpolator(new Interpolator()
              {
                     private final int frameCount = 8;

                     @Override
                     public float getInterpolation(float input)
                     {
                           return (float)Math.floor(input*frameCount)/frameCount;
                     }
              });
              mpd = new Dialog(context, R.style.ThemeDialogCustom);
              mpd.setContentView(view_lyt);
              mpd.setCancelable(cancelable);
              mpd.show();
       }

       public void hide()
       {

              if (mpd != null) {
                     if (mpd.isShowing())
                     {
                           mpd.dismiss();
                     }
              }

       }
}


style.xml:

<resources>

    <style name="ThemeDialogCustom">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowBackground">@color/transparent_color</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>

</resources>


colors.xml:

<resources>
    <color name="transparent_color">#00000000</color> 
</resources>