Friday, June 24, 2011

Simple Dialogs and Popups in Android

Following Article shows creating a Simple Dialogs and Pop ups in an Android Application.

Following are the Three Types of Dialogs illustrated in this Sample Application.
  • Alert Confirmation
  • Dialog with Select List
  • Progress Dialog
Following is the basic screenshot of the Application:


From the Above Screen Shot, on Clicking the each of the buttons i.e. Alert, Select List and Progress Dialog, their corresponding Dialogs are displayed.

Following is the Screen Showing Alert Confirmation Dialog:


Following is the Screen Showing Select List Dialog:


Following is the Screen Showing Progress Dialog:



Start with creating a Sample Application in Android (in Eclipse off course ) and following is the respective layout and Activity code for the Application:

Layout Code i.e. main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/hello"/>
    
<Button 
android:text="Show Alert Sample" 
android:id="@+id/btnAlertSample" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
<Button 
android:text="Show List Dialog Sample" 
android:id="@+id/btnListSample" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
<Button 
android:text="Show Progressbar Sample" 
android:id="@+id/btnProgress" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
</LinearLayout>

Following is the Activity Code for the Application (DialogSample.java):

package com.mayuri.dialogs.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; 
import android.widget.Toast;
import android.view.View.OnClickListener;
public class DialogSample extends Activity {
Button btnAlert, btnList, btnProgress;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
      btnAlert = (Button)this.findViewById(R.id.btnAlertSample);
      btnList = (Button)this.findViewById(R.id.btnListSample);
      btnProgress = (Button)this.findViewById(R.id.btnProgress);     
       
      btnAlert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(DialogSample.this);
builder.setMessage("Are you sure you want to exit?")
      .setCancelable(false)
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
              DialogSample.this.finish();
          }
      })
      .setNegativeButton("No", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
               dialog.cancel();
          }
      });
AlertDialog alert = builder.create();
alert.show();
}
});       
   btnList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final CharSequence[] items = {"Mango", "Orange", "Banana"};
AlertDialog.Builder listBuilder = new AlertDialog.Builder(DialogSample.this);
listBuilder.setTitle("Select A Fruit");
listBuilder.setItems(items, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int item) {
       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
   }
});
AlertDialog alertList = listBuilder.create();
alertList.show();
}
}); 
       
   btnProgress.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog progressDialog = ProgressDialog.show(DialogSample.this, "Progress Dialog Example",                      "Please wait...", true);
}
});   

    }
}


Above code is very simple as onClick listeners is added to each of the button and the corresponding dialog is called.

I hope this Article was useful.

That's All from my end for now.

Happy Coding :-))

~Mayuri


3 comments:

Chirag Shah said...

When i click on no button of dialog box,it doesn't work as i want.
I do it for delete data from SQLite.But when I click on no of dialog box ,it deletes the data rather then cancel the dialog box.
Pls rpl me for this query.
And yes this code is very helpful to me.

Rishi Verma said...

Helpful .. thanks

srinivas.malla said...

hai i have problem with camera app ie i want to store image in sdcard of android phone by taking the photo from android cam can you send the code details pls

Animated Container in Flutter

Please check this flutter video tutorial for detailed example and implementation of Animated Container in Flutter.