Custom Dialog Boxes in Android Example

A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interrupt the user and to perform short tasks that directly relate to the application in progress . The default dialog example I have already posted before.Here’s how a custom dialog is created.

The Dialog class is the base class for creating dialogs.

Types of default dialog  boxes are:

If we wish to create a customized dialog box then we can do so in the following way:

Step 1. Create a xml layout for the dialog box and a transparent background is preferable in order to show the Activity screen underneath it.

dialog_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_dialog" >

    <ImageView
        android:id="@+id/dialogImgView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/mid_box" />

    <ImageButton
        android:id="@+id/ImgBtn2_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/ImgBtn1_id"
        android:background="@drawable/female_selector" />

</RelativeLayout>

 

Step2. Add the following code for dialog box in oncreate(),onclick() etc based on where you want the dialog box to  pop. In the given example the dialog box appears  as soon as the activity starts .Hence, the code is placed in oncreate() .

public void onCreate(Bundle savedInstanceState)
	 {
	        super.onCreate(savedInstanceState);
		  setContentView(R.layout.play_menu_screen);//setting layout foractivity

              //put your activity related code here

		Dialog();   //function call for dialog box

	 }

	 /*code for creating a custom dialog box  */

	 private void Dialog()
 {    String gender;

// creating a dialog object and specifying the activity it pops on .
// R.style.FullHeightDialog specifies that dialog box has height
// equivalent to screen height

final Dialog dialog = new Dialog(PlayActivity.this,R.style.FullHeightDialog);

	dialog.setContentView(R.layout.dialog_screen);//setting the dialog xml layout

		/* adding action when image buttons of dialog are clicked */
			dialog.findViewById(R.id.ImgBtn_id).setOnClickListener(
					new OnClickListener() {
						public void onClick(View v) {
						//put your code here
						dialog.dismiss();//closes the dialog box
						}
					});

			dialog.show();//pops the dialog box

		}

This way we can create a customized dialog box .Why use the boring default dialog boxes when we can create our own.

Screenshot:

Custom Dialog

Custom Dialog

Advertisements

Progress Dialog in android

ProgressDialog is used for progress and  a text message .When we want the user to wait while some processing is taking place ,then we can use these dialogs. Example when some downloading is taking place we show a ProgressDialog until its complete.

It has two styles :

STYLE_HORIZONTAL-Creates a ProgressDialog with a horizontal progress bar.

STYLE_SPINNER-Creates a ProgressDialog with a circular, spinning progress bar.

Syntax:

ProgressDialog progressDialog;
ProgressDialog.show(  context,  "Title  of dialog",  "Loading (message)”); progressDialog.setCancelable(false);//can’t be cancelled on back press
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(100); // sets progress
incrementProgressBy(100);//int value used to progress ahead
progressDialog.show();//show dialog

We can implement a ProgressDialog using the follwing  three ways :

1. Using with handler

public class MainActivity extends Activity {
   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
        final ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         progressDialog.setCancelable(false);
progressDialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
 @Override

  public void run() {
// this code runs after 4 sec
progressDialog.dismiss();                                                                                   }
}, 4000);
}
}

2. Using thread

public class SecondActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog dialog=new ProgressDialog(SecondActivity.this);
dialog.setTitle("Loading..");
dialog.setMessage("Downloading source..");
dialog.setProgress(100);
dialog.setMax(5000);
dialog.show();
Thread splashThread = new Thread() {
@Override
public void run()
{
try
{
sleep(5000);
} catch (Exception e)

{
e.printStackTrace();
}
finally {
finish();
}
}
};          /** END OF THREAD */
splashThread.start();
}
}

3. Using async tasks

private class Operation extends AsyncTask<
String, Void, String>
{           @Override
 protected String doInBackground(String... params)
{ // code to be executed in background thread
for(int i=0;i<5;i++) {

try {
Thread.sleep(1000);
} catch (InterruptedException e)
                 {      // TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
           }
@Override
protected void onPostExecute(String result)
 { // runs on UI thread and updated UI after executing doInBackground
progressDialog.dismiss();
           }
@Override
           protected void onPreExecute() {
 ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
               progressDialog.show();
           }
           @Override
protected void onProgressUpdate(Void... values) {
 // runs on UI thread and starts first
           }

}

Advantageof using handler is that it does’nt interfere with UI thread and are better than threads.

Whereas, Async Tasks are suitable for  long task having to communicate with main thread.

Hence , now you can implement ProgressDialog in three different ways  .You can also refer to ProgressDialogDemo project attached as reference.

ProgressDialogDemo

Screenshot: