0% found this document useful (0 votes)
4 views2 pages

Alert Message

The document contains code for an Android application that displays a simple alert dialog when a button is clicked. The MainActivity.java file sets up the button and its click listener, while the activity.xml file defines the layout with the button. Additionally, string.xml provides the application name and button text resources.

Uploaded by

jitinjayarajnair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Alert Message

The document contains code for an Android application that displays a simple alert dialog when a button is clicked. The MainActivity.java file sets up the button and its click listener, while the activity.xml file defines the layout with the button. Additionally, string.xml provides the application name and button text resources.

Uploaded by

jitinjayarajnair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment no:

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:

You might also like