Android Example to Create AlertDialog Box

      No Comments on Android Example to Create AlertDialog Box

activity_main.xml

Drag the three button from the pallete.Now the layout file will look like this:-

<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:layout_marginTop="36dp"
        android:text="1st" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="26dp"
        android:text="2nd" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_centerVertical="true"
        android:text="3rd" />

</RelativeLayout>

MainActivity.java

package com.example.scanfcode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MainActivity extends Activity 
{
  Button b1,b2,b3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);

        b1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder bul1 = new AlertDialog.Builder(MainActivity.this);
                bul1.setTitle("Scanfcode");
                bul1.setMessage("Welcome to scanfcode");
                bul1.setPositiveButton("OK",new DialogInterface.OnClickListener()
                {
                            public void onClick(DialogInterface d1,int i1) 
                            {
                                Toast.makeText(getApplicationContext(),"Yes",Toast.LENGTH_LONG).show();
                            }
                        });
                bul1.show();
            }
        });
        
        
        b2.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder bul1 = new AlertDialog.Builder( MainActivity.this);
                bul1.setTitle("Scanfcode");
                bul1.setMessage("Welcome to Scanfcode");
                bul1.setNegativeButton("NO",new DialogInterface.OnClickListener()
                {
                            public void onClick(DialogInterface d1,int i1)
                  {
                         Toast.makeText(getApplicationContext(),"No",Toast.LENGTH_LONG).show();
                  }
                        });
                
                bul1.setPositiveButton("YES",new DialogInterface.OnClickListener()
                {
                            public void onClick(DialogInterface d1,int i1) 
                            {
                                Toast.makeText(getApplicationContext(),"Yes",Toast.LENGTH_LONG).show();
                            }
                        });
                bul1.show();
            }
        });
        
        b3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                AlertDialog.Builder bul1 = new AlertDialog.Builder(MainActivity.this);
                bul1.setTitle("Scanfcode");
                bul1.setMessage("Welcome to scanfcode");
                bul1.setNegativeButton("NO",new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface d1,int i1)
                            {
                                Toast.makeText(getApplicationContext(),"No",Toast.LENGTH_LONG).show();
                            }
                        });
                
                bul1.setPositiveButton("YES",new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface d1,int i1) 
                            {
                                Toast.makeText(getApplicationContext(),"Yes",Toast.LENGTH_LONG).show();
                            }
                        });
                
                bul1.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface d1,int i1) 
                            {
                                Toast.makeText(getApplicationContext(),"Cancel",Toast.LENGTH_LONG).show();
                            }
                        });

                bul1.show();
            }
        });
    }
}

Output of First Button:-

Output of Second Button:-

OUTPUT Of Third button:-

Leave a Reply

Your email address will not be published. Required fields are marked *