Wednesday, 14 August 2013

Iphone like Progress Dialog In Android Using ProgressBar

Today we will see I phone like Progress Dialog with progress bar,
and here is simple example.





create a class As   CustomizeDialogIphone.java 


package com.example.iphoneprogressbar;

import android.app.Dialog;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class CustomizeDialogIphone extends Dialog  {
public Button okButton;
       Context mContext;
       TextView mTitle = null;
       TextView mMessage = null;
       View v = null;
       public ProgressBar progressBar;

       public CustomizeDialogIphone(Context context) {
              super(context);
              mContext = context;
              /** 'Window.FEATURE_NO_TITLE' - Used to hide the mTitle */
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              /** Design the dialog in main.xml file */
              setContentView(R.layout.iphone_progressdailog);
              v = getWindow().getDecorView();
              v.setBackgroundResource(android.R.color.transparent);
              mTitle = (TextView) findViewById(R.id.dialogTitle);
              mMessage = (TextView) findViewById(R.id.dialogMessage);
              okButton = (Button) findViewById(R.id.OkButton);
              //okButton.setOnClickListener(this);
       //     okButton.setText("Cancel");

              progressBar = (ProgressBar) findViewById(R.id.progressBar1);

       }

       /**
        * Background Async Task to show custom progressbar
        * */

//     @Override
//     public void onClick(View v) {
//            /** When OK Button is clicked, dismiss the dialog */
//            if (v == okButton)
//                   dismiss();
//     }

       @Override
       public void setTitle(CharSequence title) {
              super.setTitle(title);
              mTitle.setText(title);
       }

       @Override
       public void setTitle(int titleId) {
              super.setTitle(titleId);
              mTitle.setText(mContext.getResources().getString(titleId));
       }

       /**
        * Set the message text for this dialog's window.
        *
        * @param message
        *            - The new message to display in the title.
        */
       public void setMessage(CharSequence message) {
              mMessage.setText(message);
              mMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
       }

       /**
        * Set the message text for this dialog's window. The text is retrieved from
        * the resources with the supplied identifier.
        *
        * @param messageId
        *            - the message's text resource identifier <br>
        * @see <b>Note : if resourceID wrong application may get crash.</b><br>
        *      Exception has not handle.
        */
       public void setMessage(int messageId) {
              mMessage.setText(mContext.getResources().getString(messageId));
              mMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
       }
}

create Resource as iphone_progressdailog.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="0dip"
    android:background="@drawable/alert_bg"
    android:orientation="vertical"
    android:paddingBottom="15dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >

    <TextView
        android:id="@+id/dialogTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:background="#00000000"
        android:gravity="center"
        android:text=""
        android:textColor="#fff"
        android:textSize="22sp"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/dialogMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialogTitle"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:maxLines="10"
        android:padding="10dip"
        android:scrollbars="vertical"
        android:text=""
        android:textColor="#fff"
        android:textSize="18sp" >
    </TextView>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="@style/CustomProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialogMessage"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/OkButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:background="@drawable/button"
        android:text="OK"
        android:textColor="#FFFFFF" >
    </Button>

</RelativeLayout>




<?xml version="1.0" encoding="UTF-8"?>
<!--
      @author : @alexduhem
                blog.sakaroz.com

-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />

            <gradient
                android:angle="270"
                android:centerColor="#ffdddddd"
                android:centerY="0.50"
                android:endColor="#ffffffff"
                android:startColor="#ffffffff" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="90"
                    android:endColor="#771997e1"
                    android:startColor="#770e75af" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="90"
                    android:endColor="#ff1997e1"
                    android:startColor="#ff0e75af" />
            </shape>
        </clip>
    </item>

</layer-list>



button Selector 


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"/>
 <!-- pressed -->
    <item android:drawable="@drawable/button_bg_pressed" android:state_focused="true"/>
 <!-- focused -->
    <item android:drawable="@drawable/button_bg_normal"/>
 <!-- default -->

</selector>


alert_bg

button_bg_normal

button_bg_pressed




you can download complete example from Here 



No comments:

Post a Comment