info@thistimebd.com

Sunday 24th of November 06:21:02am

Alert Dialog android source code

package com.thistimebd.web.alertdialog;


import android.content.DialogInterface;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private Button exitButton;

    private AlertDialog.Builder alertDialogBuilder ;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        exitButton=(Button) findViewById(R.id.buttonId);

        exitButton.setOnClickListener(this);

    }


    @Override

    public void onClick(View v) {

        alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

        //for setting this title

        alertDialogBuilder.setTitle(R.string.title_text);


        //for setting message


        alertDialogBuilder.setMessage(R.string.message_text);


        //for setting icon


        alertDialogBuilder.setIcon(R.drawable.question);


        alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                //exit

                finish();

            }

        });

        alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

              dialog.cancel();

                //  Toast.makeText(MainActivity.this, "you have clicked on button", Toast.LENGTH_SHORT).show();

            }

        });


        alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(MainActivity.this, "you have clicked on Cancel button", Toast.LENGTH_SHORT).show();

            }

        });



        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();


    }


    }


string.xml



<resources>

    <string name="app_name">AlertDialog</string>

    <string name="button_text">Exit</string>

    <string name="title_text">Alert title</string>

    <string name="message_text">Do you want to exit?</string>

</resources>


activity.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    tools:context="com.bdstall.web.alertdialog.MainActivity">


    <Button

        android:id="@+id/buttonId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/button_text"

        style="@style/Widget.AppCompat.Button.Colored"


        />


</LinearLayout>