0% found this document useful (0 votes)
1 views4 pages

Create and Display AlertDialog_ProgressDialog

The document provides code for an Android application that demonstrates the creation and display of an AlertDialog and a ProgressDialog. It includes a layout file (activity_main.xml) and a MainActivity.java file that sets up buttons to trigger these dialogs. The ProgressDialog shows a loading message for 10 seconds, while the AlertDialog prompts the user with a Yes/No question and displays a corresponding Toast message based on the user's choice.

Uploaded by

Siddarth Sriram
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)
1 views4 pages

Create and Display AlertDialog_ProgressDialog

The document provides code for an Android application that demonstrates the creation and display of an AlertDialog and a ProgressDialog. It includes a layout file (activity_main.xml) and a MainActivity.java file that sets up buttons to trigger these dialogs. The ProgressDialog shows a loading message for 10 seconds, while the AlertDialog prompts the user with a Yes/No question and displays a corresponding Toast message based on the user's choice.

Uploaded by

Siddarth Sriram
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/ 4

Create and display AlertDialog and ProgressDialog

activity_main.xml (Design Layout)

MainActivity.java

package com.example.alertprogressdialog;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


Button btnclose,bprogress;
AlertDialog.Builder builder;
ProgressDialog progressDialog;
TextView counter1;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
btnclose=(Button)findViewById(R.id.btn_close);
builder = new AlertDialog.Builder(this);
bprogress= (Button)findViewById(R.id.btn_progress);
counter1=(TextView)findViewById(R.id.textView);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
bprogress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading..."); // Setting Message
progressDialog.setTitle("ProgressDialog"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
}).start();

//CountDownTimer Method
/* new CountDownTimer(30000,1000){
@Override
public void onTick(long millisUntilFinished) {
counter1.setText(String.valueOf(counter));
counter++;
}

@Override
public void onFinish() {
counter1.setText("FINISH!!!");
}
}.start();*/
}
});
btnclose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder.setMessage("Alert Dialog").setTitle("Android Alert Dialog");
builder.setMessage("Do You Want to Continue Y/N ?").setCancelable(false).setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();
Toast.makeText(getApplicationContext(),"You have choose YES icon for
AlertBox",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"You have choose NO icon for
AlertBox",Toast.LENGTH_SHORT).show();

}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
}
});
}
}

Output:

You might also like