Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to create a Custom Alert Dialog in Android

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







We Create a Custom Alert Dialog Box with 2 Edit Text and 2 buttons on it. Positive Button, Negative 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="Custom Dialog"  
     android:id="@+id/CustomDialog"/>  
 </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 cdialog;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_dialog_box);  
     cdialog = findViewById(R.id.CustomDialog);  
     cdialog.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         customDialog("Our Title", "Our Custom Dialog Message");  
       }  
     });  
   }  
   private void customDialog(String Title,String Message)  
   {  
     Context context = DialogBox.this;  
     LinearLayout layout = new LinearLayout(context);  
     layout.setOrientation(LinearLayout.VERTICAL);  
     final EditText et1 = new EditText(context);  
     final EditText et2 = new EditText(context);  
     layout.addView(et1);  
     layout.addView(et2);  
     et1.setHint("Edit Text 1");  
     et2.setHint("Edit Text 2");  
     et1.setInputType(InputType.TYPE_CLASS_NUMBER);  
     final AlertDialog.Builder builder = new AlertDialog.Builder(DialogBox.this);  
     builder.setTitle(Title)  
         .setMessage(Message)  
         .setView(layout)  
         .setPositiveButton("+ve", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             Toast.makeText(DialogBox.this, et1.getText().toString()+" "+et2.getText().toString(), Toast.LENGTH_SHORT).show();  
           }  
         })  
         .setNegativeButton("-ve", new DialogInterface.OnClickListener() {  
           @Override  
           public void onClick(DialogInterface dialogInterface, int i) {  
             builder.setCancelable(true);  
           }  
         })  
         .show();  
   }  
 }  

Now Run your app, and when you click on a button your dialog box appears with 2 EditText and 2 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