Alert Message
Alert Message
MainActivity.java
package com.example.myapplication; // Change this to match your project
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button showAlertButton = findViewById(R.id.showAlertButton);
showAlertButton.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert Message")
.setMessage("This is a simple alert message in Android.")
.setCancelable(false)
.setPositiveButton("OK", (dialog, which) -> dialog.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
});
}
}
activity.xml:
<?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"
android:gravity="center"
android:padding="20dp">
<Button
android:id="@+id/showAlertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_alert_button"
android:textSize="18sp"
android:padding="10dp"/>
</LinearLayout>
string.xml:
<resources>
<string name="app_name">My Application</string>
<string name="show_alert_button">Show Alert</string>
</resources>
Output: