How to present a simple alert message in java?

5Ssoftwaresafety
5S JAVA AND KOTLIN LEARNING
3 min readOct 15, 2020

In this section we are going to how to give alert message in java and create simple java alert dialog.

simple alert message in java

This example demonstrates how to use the simple alert message in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to use alert message in android?

Step 2 — “Add the following code to res/layout/activity_main.xml.“

I worked in simple relative linear layout method for only give basic example.

In the below code, we have declared TextView (@+id/textView) and write text (android:text=”Press the back to alert box”)

LAYOUT FILE CODE IN XML:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yourname.yourandroidstuido.MainActivity"
>
android:padding="16dp"
android:orientation="vertical"
>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="Press the back to alert box"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:textSize="20sp"
/>

<TextView
android:id="@+id/textViewJavaLearn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="300dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING"
/>


</LinearLayout>

Third Step “MainActivity.java” java file − Add the following code to src/MainActivity.java

JAVA CODE IN MAIN ACTIVITY:

Android AlertDialog is composed of three regions: title, content area and action buttons.

Android AlertDialog is the subclass of Dialog class.

package com.example.andy.myapplication;import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

//following method is automatically called when back button is pressed
@Override
public void onBackPressed() {
showAlertDialog();
}

private void showAlertDialog() {
//init alert dialog
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Exit");
builder.setMessage("Are you sure you want to leave?");
//set listeners for dialog buttons
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//finish the activity
finish();
}
});

builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//dialog gone
dialog.dismiss();
}
});

//create the alert dialog and show it
builder.create().show();
}


}

As shown, don’t forget to call methods, click alt + enter for implement methods.

Review Application

Fourth Step Results and How to Work AlertDialog !

AlertView for Android in JAVA
Click the YES to Come Back Menu
AlertView for Android in JAVA

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.

AlertDialog in JAVA ANDROID
AlertDialog in JAVA ANDROID

If you have any question write comments I will reply.

Have a nice day.

If you need Google AdMob Alternatives you can check out Start Ad Platfrom.

https://portal.start.io/#/signup?referredby=69e578e9-ec0c-24b3-a74e-f58373fac2ef&preferredsite=pub&source=directURL

Regards

5S JAVA AND KOTLIN LEARNING

--

--