0% found this document useful (0 votes)
6 views7 pages

AlertDialogApp Assignment

The document outlines an Android assignment to create an app called AlertDialogApp that includes an alert dialog and back button functionality. It provides step-by-step instructions for setting up the project, implementing a button to show an alert dialog, and confirming exit on back press. Additionally, it includes optional navigation confirmation to a second activity with a dialog prompt before proceeding.

Uploaded by

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

AlertDialogApp Assignment

The document outlines an Android assignment to create an app called AlertDialogApp that includes an alert dialog and back button functionality. It provides step-by-step instructions for setting up the project, implementing a button to show an alert dialog, and confirming exit on back press. Additionally, it includes optional navigation confirmation to a second activity with a dialog prompt before proceeding.

Uploaded by

rinkupawar697
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

📱 Android Assignment: Alert Dialog and Back Button

✅ Task 1: Alert Dialog

a. Create a New Project

 Project Name: AlertDialogApp

 Language: Java (or Kotlin if preferred)

 Minimum SDK: API 21+

b. Layout with a Button

res/layout/activity_main.xml

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

<RelativeLayout xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="24dp">

<Button

android:id="@+id/showDialogButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Show Alert Dialog"

android:layout_centerInParent="true"/>

</RelativeLayout>

c. Show a Basic Alert Dialog

[Link]

package [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

Button showDialogButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_main);

showDialogButton = findViewById([Link]);

[Link](new [Link]() {

@Override

public void onClick(View v) {

showAlertDialog();

});

private void showAlertDialog() {

[Link] builder = new [Link]([Link]);

[Link]("Sample Alert")

.setMessage("Do you want to proceed?")

.setPositiveButton("Yes", new [Link]() {


public void onClick(DialogInterface dialog, int id) {

// Handle Yes

})

.setNegativeButton("No", new [Link]() {

public void onClick(DialogInterface dialog, int id) {

// Handle No

[Link]();

})

.setNeutralButton("Cancel", new [Link]() {

public void onClick(DialogInterface dialog, int id) {

// Handle Cancel

[Link]();

});

AlertDialog dialog = [Link]();

[Link]();

✅ Output:

When the user clicks the button, a dialog appears with Yes, No, and Cancel buttons.

✅ Task 2: Handle Back Button with Alert Dialog

a–e. Confirm Exit on Back Press

Update [Link] by overriding onBackPressed():

@Override
public void onBackPressed() {

[Link] builder = new [Link](this);

[Link]("Exit Confirmation")

.setMessage("Are you sure you want to exit?")

.setPositiveButton("Exit", new [Link]() {

public void onClick(DialogInterface dialog, int id) {

finish(); // Exit the app

})

.setNegativeButton("Cancel", new [Link]() {

public void onClick(DialogInterface dialog, int id) {

[Link]();

});

AlertDialog exitDialog = [Link]();

[Link]();

✅ Output:

When the back button is pressed, a dialog appears to confirm exit.

🟡 Task 3 (Optional): Confirm Before Navigation

Let’s say you have a second activity called SecondActivity.

a–d. Navigate with Confirmation

Add a new activity [Link] and link from MainActivity using a button.

Update activity_main.xml:

<Button

android:id="@+id/navigateButton"
android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Go to Second Activity"

android:layout_below="@id/showDialogButton"

android:layout_marginTop="20dp"

android:layout_centerHorizontal="true"/>

Update [Link]:

Button navigateButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

...

navigateButton = findViewById([Link]);

[Link](new [Link]() {

@Override

public void onClick(View v) {

confirmNavigation();

});

private void confirmNavigation() {

[Link] builder = new [Link]([Link]);

[Link]("Navigate Away?")

.setMessage("Are you sure you want to go to the next screen?")

.setPositiveButton("Yes", new [Link]() {

public void onClick(DialogInterface dialog, int id) {


startActivity(new Intent([Link], [Link]));

})

.setNegativeButton("No", new [Link]() {

public void onClick(DialogInterface dialog, int id) {

[Link]();

});

[Link]().show();

[Link]:

package [Link];

import [Link];

import [Link];

public class SecondActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_second);

Create activity_second.xml with a simple TextView.

📄 Final Notes for Submission

 Test all features on emulator or device


 Include all .java files, .xml files, and screenshots if required

 Save your document as AlertDialogApp_Assignment.docx or PDF

You might also like