Saturday 21 September 2013

Android : Alert Box

In This Tutorial We Will Study How To Implement Simple Alert Box .
You Can Implement Every Widget With AlertBox. In Simple Alertbox We Will Show Simple Message In Alertbox With Its Simple Functions.

1. Create New Android Application Project. Name It Alertbox.
2. Now Go To Your activity_main.Xml File. Alertbox -> res --> layout --> activity_main.xml.
3. Add One Button To it By Using Which We Will Show Alertbox.
4. Or Replace Your activity_main.xml code with Below Code.

activity_main.xml.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Show My Alert Box" />

</RelativeLayout>

5. Now Go To Your MainActivity.java file . Alertbox -->src-->package-->MainActivity.java

6. Replace MainActivity.java code with below code.

MainActivity.java.


package com.example.alertbox;

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

public class MainActivity extends Activity {

       final Context context = this;
       private Button button1;

       public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              button1 = (Button) findViewById(R.id.button1);

      
              button1.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View arg0) {

                           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                         context);

                           // set title
                           alertDialogBuilder.setTitle("My Alert Title");

                           // set dialog message
                           alertDialogBuilder
                                         .setMessage("Click yes to exit!")
                                         .setCancelable(false)
                                         .setPositiveButton("Yes",
                                                       new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
                                                                    
                                                                    
                                                                    
                                                                     MainActivity.this.finish();
                                                              }
                                                       })
.setNegativeButton("No",
         new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
                                                                    
    dialog.cancel();
                                                              }
                                                       });

                           // create alert dialog
                           AlertDialog alertDialog = alertDialogBuilder.create();

                           // show it
                           alertDialog.show();

                     }

              });

       }    
  }




7. Now Compile And Run Your Application For Alertbox

My Output Screen Is As Below.






Now Make Your Alertbox.

All The Best :)
 

No comments:

Post a Comment