Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to create a Simple Alert Dialog Box in Android

In this, We create a Simple Alert Dialog Box in the Android app.




We Create an Alert Dialog Box with 3 buttons on it. Positive Button, Negative Button, and a Neutral button. You can use one or two-button also according to your requirements.

 So, Firstly we need to create a Button in our XML which will open an Alert Dialog box.

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.ConstraintLayout 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"  
   tools:context=".DialogBox">  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Simple Dialog"  
     android:id="@+id/SimpleDialog"/>  
 </android.support.constraint.ConstraintLayout>  

Then in java file create a button variable and find it's Id and then create an Alert Dialog box in button on click listener.

MainActivity.java
 package studio.harpreet.sampleproject;  
 import android.content.Context;  
 import android.content.DialogInterface;  
 import android.support.v7.app.AlertDialog;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.text.InputType;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.LinearLayout;  
 import android.widget.Toast;  
 public class DialogBox extends AppCompatActivity {  
 Button sdialog;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_dialog_box);  
     sdialog = findViewById(R.id.SimpleDialog);  
     sdialog.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         sdialog("Our Title", "Our Dialog Box Message");  
       }  
     });  
   }  
   private void sdialog(String Title,String Message) {  
     AlertDialog.Builder builder = new AlertDialog.Builder(DialogBox.this);  
     builder.setTitle(Title)  
         .setMessage(Message)  
         .setNegativeButton("-ve", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             Toast.makeText(DialogBox.this, "Negative Button", Toast.LENGTH_SHORT).show();  
           }  
         })  
         .setPositiveButton("+ve", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             Toast.makeText(DialogBox.this, "Positive Button", Toast.LENGTH_SHORT).show();  
           }  
         })  
         .setNeutralButton("neutral", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             Toast.makeText(DialogBox.this, "This is Neutral Button", Toast.LENGTH_SHORT).show();  
           }  
         })  
         .show();  
   }  
 }  


Now Run your app, and when you click on a button your dialog box appears with 3 buttons you created for your Dialog Box.


 
Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments