Pages

Chapter 12: Alert Dialog in Android

Alert Dialog In Android Example

In this chapter we will learn to implement Android AlertDialog with a simple example in Android Studio. 

Alert dialog is a pop up window which appears if a user wants to set something or to show something. for example if a user wants to delete, if he hits the delete button the confirm pop up appears to delete or not. Its a pop up window to make a decision.

Example:
  • Create a new project and name it as AlertDialogExample in the Android Studio.
  • Design a button in main layout.
File: 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:gravity="center"
    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="com.example.hss_24.alertdialogexample.MainActivity">

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

</RelativeLayout>
  • Do the following code in main java file.
File: MainActivity.java:
import android.app.AlertDialog;
import android.content.DialogInterface;
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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button alert_button = (Button) findViewById(R.id.btn_alert);

        alert_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // build alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Delete");
                builder.setMessage("Are you sure you want to delete?");
                builder.setCancelable(true);

                // Create alert dialog yes button
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "You clicked yes", Toast.LENGTH_SHORT).show();
                    }
                });

                // Create alert dialog no button
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "You clicked no", Toast.LENGTH_SHORT).show();
                    }
                });

                // Create alert dialog and show
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }

        });


    }
}

  • Run the App and enjoy the output.
Output:






Download this project here




Thank you...
Please like and share...




Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment