Saturday, January 1, 2011

Better AlertDialog box in Android

Better AlertDialog box in Android



package com.mytest.android;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyTestActivity extends Activity {
    ProgressDialog progressDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressDialog = new ProgressDialog(this);
        Button buttonClickMe = (Button)findViewById(R.id.button1);
        buttonClickMe.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View arg0) {
                //progressDialog.setMessage("Please wait ... ");
                //progressDialog.show();
                // do stuff here......
                // to cancel progressDialog box
                //progressDialog.cancel();
                // finally show dialog box here
                MyAlert.displayAlert(MyTestActivity.this , "Notification", "You have pressed on me!!!", null, "Ok");
            }
        });
    }
  
}




=================================================


package com.mytest.android;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
public class MyAlert {
    public static void displayAlert(Context cntx ,String title,String message, final String NegButton,final String posButton) {
        AlertDialog.Builder alertBuilder = new Builder( cntx);
        alertBuilder.setTitle(title);
        alertBuilder.setMessage(message);
        if(NegButton != null) {
            alertBuilder.setNegativeButton(NegButton,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { }
            });
        }
       
        if (posButton != null) {
            alertBuilder.setPositiveButton(posButton,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
        }
        AlertDialog alert = alertBuilder.create();
        alert.show();
    }
}




No comments:

Post a Comment