Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • 3 Steps Guide to Customize Alert Dialog with Dynamic Buttons

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 694
    Comment on it

    In this android tutorial blog, we are going to discuss customizing Alert Dialog with Dynamic Buttons.  Before we start the tutorial and the coding, it is first immensely important to comprehend what is android dialog. Android dialog is identical to the regular pop-up window that is used to draw attention to important information or to perform an action. It is a simple technique by which users are alerted about the errors or the choices required. One can easily customize this box by using the AlertDialog object of the Java SDK.

    To customize the Alert Dialog in Android, follow these 3 easy steps:

    1. Create project.

    2. Create layout file with name promtpt.xml and design dialog box.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/userInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="this is Textview in Custom Dialog Box." />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:id="@+id/btnAdd1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="Go for Movie" />
    
            <Button
                android:id="@+id/btnAdd2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="Go for Lunch." />
    
        </LinearLayout>
    </LinearLayout>

     

    3. Open MainActivity.java and add below code to method from where you want to open dialog.

      LayoutInflater layoutInflater = LayoutInflater.from(this);
            View promptView = layoutInflater.inflate(R.layout.promtpt, null);
    
            final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    
    
            Button btn1 = (Button) promptView.findViewById(R.id.btnAdd1);
    
            Button btn2 = (Button) promptView.findViewById(R.id.btnAdd2);
    
            btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                    // btnAdd1 has been clicked
    
                }
            });
    
            btn2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // btnAdd2 has been clicked
    
                }
            });
    
            alertDialog.setView(promptView);
    
            alertDialog.show();
    


    With this, we are going to end this tutorial. If there is something more you want to know, you can drop a message in the comment section below.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: