banner



How To Create Comment Box In Android

Android | Alert Dialog Box and How to create it

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed.

Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.
Alert Dialog code has three methods:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?

Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

  • setTitle() method for displaying the Alert Dialog box Title
  • setMessage() method for displaying the message
  • setIcon() method is use to set the icon on Alert dialog box.

Then we add the two Button, setPositiveButton and setNegativeButton to our Alert Dialog Box as shown below.

Example:


Below are the steps for Creating the Alert Dialog Android Application:

  • Step 1: Create a new project. After that, you have java and XML file.

  • Step 2: Open your XML file and then add TextView for message as shown below (you can change it accordingly).

  • Step 3: Now, open up the activity java file. After, on create method declaration, the onbackpressed method is called when you click the back button of your device.
  • Step 4: Create the object of Builder class Using AlertDialog.Builder. Now, set the Title, message.
  • Step 5: In a builder object set the positive Button now gives the button name and add the OnClickListener of DialogInterface. Same as with the negative Button, at last, create the Alert dialog Box with builder object and then show the Alert Dialog.
  • Step 6: Now if positive button press finish the app goto outside from the app if negative then finish the dialog box
  • Step 7: Now run it and then press the back button. After that click Yes or No Button.

    The complete code of MainActivity.java or activity_main.xml of Alert Dialog is given below:

    activity_main.xml

    <? xml version = "1.0" encoding = "utf-8" ?>

    < RelativeLayout

    android:layout_width = "match_parent"

    android:layout_height = "match_parent"

    tools:context = ".MainActivity" >

    < TextView

    android:layout_width = "wrap_content"

    android:layout_height = "wrap_content"

    android:text = "Press The Back Button of Your Phone."

    android:textStyle = "bold"

    android:textSize = "30dp"

    android:gravity = "center_horizontal"

    android:layout_marginTop = "180dp"

    />

    </ RelativeLayout >

    MainActivity.java

    package org.geeksforgeeks.navedmalik.alertdialog;

    import android.content.DialogInterface;

    import android.support.v7.app.AlertDialog;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

    super .onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    }

    @Override

    public void onBackPressed()

    {

    AlertDialog.Builder builder

    = new AlertDialog

    .Builder(MainActivity. this );

    builder.setMessage( "Do you want to exit ?" );

    builder.setTitle( "Alert !" );

    builder.setCancelable( false );

    builder

    .setPositiveButton(

    "Yes" ,

    new DialogInterface

    .OnClickListener() {

    @Override

    public void onClick(DialogInterface dialog,

    int which)

    {

    finish();

    }

    });

    builder

    .setNegativeButton(

    "No" ,

    new DialogInterface

    .OnClickListener() {

    @Override

    public void onClick(DialogInterface dialog,

    int which)

    {

    dialog.cancel();

    }

    });

    AlertDialog alertDialog = builder.create();

    alertDialog.show();

    }

    }


    Output:

How To Create Comment Box In Android

Source: https://www.geeksforgeeks.org/android-alert-dialog-box-and-how-to-create-it/

Posted by: frittsfeellen.blogspot.com

0 Response to "How To Create Comment Box In Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel