Alert Dialog in Android Studio

How to Show Alert Dialog in Android Studio

Demonstration of Alert Dialog in Android Studio

1. XML File: activity_main.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:id="@+id/main"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnAlert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:text="Show Alert Box"/>

</LinearLayout>

2. Java File: MainActivity.java

package com.tecvipul.alertdialog;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    Button btnAlert;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        btnAlert = findViewById(R.id.btnAlert);

        btnAlert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Simple Alert Dialog");
                builder.setMessage("This is alert dialog box");
                builder.setPositiveButton("OK", null);
                builder.setNegativeButton("Cancel", null);

                builder.show();
            }
        });

    }
}

What is an Alert Dialog in Android Studio?

An Alert Dialog in Android is a small window that prompts the user to make a decision or enter additional information. It does not fill the screen and is normally used for messages, warnings, or confirmations. It can have a title, a message, and action buttons like OK or Cancel.

How Does It Work?

In Android, you use the AlertDialog.Builder class to create an alert dialog. This builder pattern allows you to set the dialog's title, message, and actions before showing it to the user.

Detailed Explanation of the Code:

  • XML File: The XML file contains a simple LinearLayout with a centered button. The button has the text "Show Alert Box".
  • Java File:
    • We define a Button object btnAlert and initialize it using findViewById.
    • An OnClickListener is set on the button. When clicked, it creates an AlertDialog.Builder instance.
    • We set the dialog’s title and message using setTitle and setMessage.
    • We add two buttons: setPositiveButton for OK and setNegativeButton for Cancel, both with no additional actions (hence null).
    • Finally, builder.show() displays the alert dialog on the screen.

Conclusion

This is a simple demonstration of how to show an alert dialog in Android Studio. Alert dialogs are very useful for prompting user decisions or showing important messages. You can customize them further by adding input fields, checkboxes, or custom layouts to suit your app’s needs.





 

Post a Comment

0 Comments