Android Custom Alert Dialog

                                     Android Custom Alert Dialog

 1) AlertDialog is a small window that appears or pop up in front of your application.
 2) It gets the screen focus and able to accept user interaction.
 3) Dialogs are normally used for notifications that should interrupt the user and to perform short tasks.

Step 1:

Create a new project as CustomAlertDialog.
Copy  the below coding and paste it into your main.xml file.

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

  <Button
        android:id="@+id/btn_alert"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Alert Dialog" />

</LinearLayout>

Step 2:

Copy the below code and paste into your java file(CustomAlertDialog.java).


package com.custom.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustomDialog extends Activity {
    Button buttonAlert;

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

        buttonAlert = (Button) this.findViewById(R.id.btn_alert);
        buttonAlert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(CustomDialog.this);
                alertBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()                            {
                            public void onClick(DialogInterface dialog, int id) {
                                // If press the OK button if finishes the dialog.
                             }
                        });

                AlertDialog alert = alertBuilder.create();
           
                alert.setIcon(R.drawable.ic_launcher);
                alert.setTitle("Alert Title");
                alert.setMessage("Alert Message");
                alert.show();
            }
        });
    }
}



Full Source code for CustomAlertDialog

Comments

Popular posts from this blog

Share Data Across Application in Android

Android Naming Conventions

MD5 Encryption in Android