Wednesday, October 12, 2011

Custom ProgressDilaog like iPhone

        We use Android default ProgressDialog  when we run any background operation to indicate user to wait till the operation finished. So that user will not be able to select anything on the screen till ProgressDialog dismiss but the ProressDialog can only be shown in the center of the Screen. We can't set its position and also we can't remove its default background (white boarder) .

   The alternative is that we can use ProgressBar but we have to prevent user interaction during the Progress shown. Here is the code how we can achieve this..

 Create a layout xml named popup_example.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ProgressBar
android:id="@android:id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading Content"
android:layout_margin="10dip"
android:textColor="#FFFFFF"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>


Then we have to use this layout in our Activity as follows..
Create a Activity names CustomDialog


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class CustomDialog
{
private static Dialog mDialog = null;

public CustomDialog()
{
// do Nothing
}
public static void showProgressDialog(Context mContext, String text, boolean cancellable)
{
removeDialog();

mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar);

LayoutInflater mInflater = LayoutInflater.from(mContext);
View layout = mInflater.inflate(R.layout.popup_example, null);
mDialog.setContentView(layout);

TextView mTextView = (TextView) layout.findViewById(R.id.text);

if (text.equals(""))
mTextView.setVisibility(View.GONE);
else
mTextView.setText(text);

mDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
{
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_SEARCH:
return true;
}
return false;

}
});

mDialog.setCancelable(false);

mDialog.show();
}

public static void removeDialog(){

if (mDialog != null)
mDialog.dismiss();
}
}

To use this CustomDialog we have to write this in our Activity 

CustomDialog _progress = new CustomDialog();
_progress.showProgressDialog(this,"Please Wait.... ",false);


Happy Coding.....

1 comment:

  1. ロンドンオリンピックはもうすぐ開幕!アイホンで試合を見ることができて、嬉しい。電車の中でも、学校の通学道でも、アイホンを出して、試合を見たくなるでしょう。その時は可愛いiPhone ブランド ケースでアイホンを可愛くして、人目を引きたいでしょう。オススメしたいのは、当ショップの超目玉商品のバービー iphoneケースです。それに、ブランドの上品感を見せたいのなら、Tory Burch iphone ケースが絶好品!

    ReplyDelete