Showing posts with label Alert Dialog in Android. Show all posts
Showing posts with label Alert Dialog in Android. Show all posts

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!

Monday, December 5, 2011

AlertBox in Android (Basic)

This is the simple and very basic Tutorial for Android Alert Box Dialog.
Just create alert box and you can call it whenever you want in app

so let's try this small app

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

 AlertBoxActivity.java

package com.rdc;

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

public class AlertBoxActivity extends Activity {   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //call alert box code to display dialog
        alertbox("Alert Box","Your Application has been started..");
    }
   
    //create alert dialog box
    protected void alertbox(String title, String mymessage) 
    { 
    new AlertDialog.Builder(this) 
       .setMessage(mymessage) 
      .setTitle(title) 
       .setCancelable(true) 
       .setNeutralButton("OK", 
          new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton){} 
          }) 
       .show(); 
    }
}

main.xml



AndroidManifest.xml





 
 
 
 





The output Screen will be like this..

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

cheers!!

 I'd love to hear your thoughts!