Monday, December 12, 2011

AlertBox in Android (Advance)

This is also simple Tutorial for Android Alert Box Dialog.

Few things we can do with this that cann't with Basic Tutorial like

  • We are adding the alert icon to Dialog box.
  • It shows the alert message as Basic does.
  • We can make decision whether cancel alert message or do some task

okay! so let's try this small app

 -------------------------------------------
App Name: AlertBoxAdvance
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: AlertBoxAdvance
-------------------------------------------

project structure is look like this


Note: you need to save below alert icon and put into drawable folder


AlertBoxAdvance.java
package com.rdc;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlertBoxAdvance extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
        
    //create advance Alert dialog box
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit this App?")
       .setCancelable(false)
       .setTitle("AlertBox")
       .setIcon(R.drawable.alert)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //finish the current activity
               AlertBoxAdvance.this.finish();
           }
        })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
              //cancel the dialog box 
             dialog.cancel();
            }
        });
       AlertDialog alert = builder.create();
       alert.show();
        
    }
}

main.xml



AndroidManifest.xml

 

 
  
   
   
   
   
  

 


The output Screen will be like this..

You can download the complete source code zip file here : AlertBoxAdvance

cheers!!

I'd love to hear your thoughts!

No comments:

Post a Comment