Using alert dialog in android

buraknalpara.com

Why we use alert dialog in android?

We use alert dialog in android when we want our users to make a decision on something. For example we have an information displaying on view and our application has an ability to delete it. However users can click delete button by mistake and we don’t want our users to delete their data accidentally. What can we do in this kind of situations: We can use alert dialog of course.

How to implement alert dialog?

Let’s create an android application to write an alert dialog example. Just create an empty solution and prepare an application which contains one textbox and one button. Our application will look like something below.

android alert dialog design
alert dialog design

It is simple design because we don’t care about design right now.

What we are going to do with this application;

When we click “CHANGE TEXT” button the text on the textbox will change to “buraknalpara.com” after getting confirmation from user via alert dialog.

Here is the code of res/layout/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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="23dp"
        android:layout_marginEnd="6dp"
        android:layout_marginStart="6dp"
        android:layout_marginTop="16dp"
        android:text="Text to be changed"
        app:layout_constraintBottom_toTopOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="53dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:srcCompat="@drawable/logo" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="184dp"
        android:onClick="changeText"
        android:text="Change Text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</android.support.constraint.ConstraintLayout>

 

Here is the code of src/MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
    }

    public void changeText(View view){
        AlertDialog.Builder alert_builder = new AlertDialog.Builder(MainActivity.this); // Create alert builder
        alert_builder.setTitle("Change Text"); //Give the Title of alert dialog
        alert_builder.setMessage("Are you sure about changing the text of text box"); //Message that we want to display to user
        alert_builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { //What should happen if user click positive button. it is "Yes" in our case.
            @Override
            public void onClick(DialogInterface dialog, int which) {
                textView.setText("buraknalpara.com");
            }
        });
        alert_builder.setNegativeButton("No", new DialogInterface.OnClickListener() { //What should happen if user click negative button. it is "No" in our case.
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "Operation cancelled", Toast.LENGTH_SHORT).show(); //display toast message for cancelling the operation
            }
        });

        alert_builder.show(); //Show the alert dialog
    }
}

 

Now it is time to run our application. You can run it either on virtual device or on your own android machine. After application has opened, click “CHANGE TEXT” button and you will see below screen.

android alert dialog
alert button display

and after clicking “YES” on that dialog you should see the text of the text box on the screen changed to “buraknalpara.com”. If you did everything correct you will see something similar to below image.

Result for alert dialog

You can referĀ https://github.com/brknlpr/androidalertdialog for source code of this sample application.

HBN

5 (100%) 10 votes

You May Also Like

About the Author: HBN

--Senior QA Engineer-- --Intermediate Web Developer-- --Junior Android Developer--

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *