Tuesday, February 15, 2011

How to create Alert boxes


There are four type of alert boxes are available. Here I describe how to create simple alert box.

package com.alt; // package name

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

public class alertBox extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AlertDialog.Builder altDialog= new AlertDialog.Builder(this);
altDialog.setMessage("Alert Dialog Message"); // here add your message
altDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Ok button Clicked ", Toast.LENGTH_LONG).show();
}
});
altDialog.show();

}
}


After Ok button Press it will display below screen.



Sameeraa4ever

1 comment: