Thursday, 2 October 2014

Create TransparentProgressDialog in Android


 TransparentProgressDialog::

import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class TransparentProgressDialog  extends Dialog {
   
private ImageView iv;
   
public TransparentProgressDialog(Context context, int resourceIdOfImage) {
    super(context, R.style.TransparentProgressDialog);
        WindowManager.LayoutParams wlmp = getWindow().getAttributes();
        wlmp.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wlmp);
    setTitle(null);
    setCancelable(false);
    setOnCancelListener(null);
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    iv = new ImageView(context);
    iv.setImageResource(resourceIdOfImage);
    layout.addView(iv, params);
    addContentView(layout, params);
}
   
@Override
public void show() {
    super.show();
    RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(3000);
    iv.setAnimation(anim);
    iv.startAnimation(anim);
}
}


style.TransparentProgressDialog


    <!--  Transparent dialog -->
    <style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>





Friday, 3 January 2014

Android Progress Dialog example

ProgressDialog:

Android ProgressDialog is a dialog box/dialog window which shows the progress of a task. The progress dialog will be used if the we want the user to wait till the task complete. This is a extension of AlertDialog. (To know more about AlertDialog in Andrid, visit this page AlertDialog example). ProgressDialog is almost same as ProgressBar with the exception that this is displayed as a dialog box. This will display the progress in numbers.


Difference between ProgressBar and ProgressDialog:
The difference between ProgressBar and ProgressDialog is given in the another tutorial. View it now.

Important things about ProgressDialog:

setMessage(); method is used to show the message to the user. Example: Loading... (or) Please wait....
setTitle(); method is used to set a title to the dialog box.
setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); is used to show the horizontal progress bar in the dialog box.
setProgressStyle(ProgressDialog.STYLE_SPINNER); is used to show the circle/spinning progress bar in the dialog box.
setMax(); method is used to set the maximum value.
getProgress(); method is used to get the current progress value in numbers.

How to create:

ProgressDialog can be created programatically using the ProgressDialog class instance as shown below;
ProgressDialog progressDoalog=new ProgressDialog(MainActivity.this);
Here MainActivity.this is the activity class.

Example program:

In this example program I am creating a ProgressDialog and increment its value by 1 using the progressDoalog.incrementProgressBy(1); method. handle.sendMessage(handle.obtainMessage()) activates the Handler in which we’ve written the code for incrementing the Progress bar.
I made the thread to sleep for 200 milliseconds after every increment to show the progress slowly.

Android layout xml file: activity_mail.xml


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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ProgressDialog example" />

</RelativeLayout>


Activity class: MainActivity.java:


package com.example.progressdialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Android ProgressDialog example
 * @author Prabu Dhanapal
 * @version 1.0
 * @since SEP 01 2013
 *
 */
public class MainActivity extends Activity {
 Button button;
 ProgressDialog progressDoalog;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      progressDoalog = new ProgressDialog(MainActivity.this);
      progressDoalog.setMax(100);
      progressDoalog.setMessage("Its loading....");
      progressDoalog.setTitle("ProgressDialog bar example");
      progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progressDoalog.show();
      new Thread(new Runnable() {
        @Override
        public void run() {
                        try {
     while (progressDoalog.getProgress() <= progressDoalog
    .getMax()) {
       Thread.sleep(200);
       handle.sendMessage(handle.obtainMessage());
       if (progressDoalog.getProgress() == progressDoalog
    .getMax()) {
         progressDoalog.dismiss();
       }
     }
          } catch (Exception e) {
     e.printStackTrace();
   }
        }
      }).start();
    }

    Handler handle = new Handler() {
    @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        progressDoalog.incrementProgressBy(1);
      }
    };
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items 
                //to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }


}