Android Dialogs - Simple AlertDialog

android data sorting

An AlertDialog is a subclass of a Dialog that can display one, two or three buttons.

Normally these dialogs don't require any layout.

In this example we will look at a simple alert dialog with title and message.

Let's go.

==

1. Create Project

In your android studio create an empty activity. If you are not sure how to do that check this tutorial.

In our generated project we will have one class: MainActivity and one layout activity_main.xml.

2. activity_main.xml

This is our layout. We add a button that when clicked will show us the alertdialog. We also have a TextView to act as our header label.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="info.camposha.mysimplealertdialog.MainActivity">

    <TextView
        android:id="@+id/headerLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:fontFamily="casual"
        android:text="Android Simple AlertDialog"
        android:textAllCaps="true"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/showAlertID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

        />

</RelativeLayout>

3. MainActivity.java

We have one class MainActivity.java.

package info.camposha.mysimplealertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/*
Android AlertDialog Example
 */
public class MainActivity extends Activity {
    /*
    ONCREATE METHOD
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //REFERENCE BUTTON AND SHOW IT.
        Button showBtn=findViewById(R.id.showAlertID);
        showBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAlert();
            }
        });
    }
    /*
    CREATE AN ALERT DIALOG AND SHOW IT
     */
    private void showAlert()
    {
        //INSTANTIATE ALERTDIALOG BUILDER
        AlertDialog.Builder myBuilder=new AlertDialog.Builder(this);
        //SET PROPERTIES USING METHOD CHAINING
        myBuilder.setTitle(R.string.my_title).setMessage(R.string.my_message);
        //CREATE DIALOG
        AlertDialog myDialog=myBuilder.create();
        //SHOW DIALOG
        myDialog.show();
    }
}

NOTES:

  1. First we specify the package name for our application.
  2. Then add our imports using the import keyword.
  3. Then create a class called MainActivity that inherits from android.app.Activity.
  4. Then we create a method called showAlert() that will show our alert dialog.

    • In this method first we instantiate the AlertDialog.Builder class, passing it the context via the constructor.
    • Then set the title of our dialog via the setTitle() method.Then set the message using the setMessage() method.
    • Then create our AlertDialog using the create() method defined in the AlertDialog.Builder class.
    • Then show our Dialog via the show() method.
  5. Then we override the onCreate() method of our Activity, passing in a Bundle object.
  6. We invoke the setContentView() to inflate our activity_main.xml layout.
  7. We reference our button, listen to its onclick event and invoke the showAlert() method.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post