Alert Dialog :
Alert Dialog is used to display a pop up or dialog in the Activity. It is generally used to ask question from a
user with the help of popup dialog at certain event.
Creating an Alert Dialog is very easy in Android
with the use of AlertDialog class.
Create a new project in Eclipse : File => New => Android
Application Project and give a package name as com.javalanguageprogramming.alertdialogdemo.
Copy the code of activity_main.xml file as shown below :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/alertButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Get Alert Dialog"
android:layout_gravity="center"
android:layout_marginLeft="50dp"
/>
</LinearLayout>
In this file we are creating a button which show a
dialog when clicked and ask from user that you want to exit from application or
not.
Copy the code of MainActivity.java as shown below :
package com.javalanguageprogramming.alertdialogdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button alertButton;
Builder dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize button variable
alertButton = (Button) findViewById(R.id.alertButton);
// initialize alert dialog
dialog = new AlertDialog.Builder(MainActivity.this);
//if button is clicked than set the alert dialog.
alertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// call the alertDialogSetup method when button is clicked
alertDialogSetup();
}
});
}
private void alertDialogSetup() {
// set a title
dialog.setTitle("Exit Application");
// set a message
dialog.setMessage("Are you sure?");
// set positive button with OnClickListener for positive response
dialog.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Exit application when user click on yes
System.exit(0);
}
});
// set negative button with OnClickListener for negative response
dialog.setNegativeButton("NO", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//cancel the dialog on negative button with the use of DialogInterface reference
dialog.cancel();
}
});
//there is one more button in alert dialog in the center called neutral button
dialog.setNeutralButton("Neutral", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// make a toast that neutral button is clicked
Toast.makeText(MainActivity.this, "Neutral button is clicked", Toast.LENGTH_LONG).show();
}
});
//you can use image with the text in the dialog
dialog.setIcon(R.drawable.ic_launcher);
// Now show the dialog on the activity
dialog.show();
}
}
In this Activity, we are creating three buttons (Positive => Yes, Negative => No, Neutral => Cancel) in the dialog. You can create single button
or two buttons dialog.
Thank u for Sharing a good and useful information. Nice blog
ReplyDeleteHadoop Training in Hyderabad
Java Training in Hyderabad
Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDeletepython Training in Bangalore | python Training in Bangalore
I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
thanks for sharing nice article visit Best Java Programming Tutorials and Courses
ReplyDeleteif you're looking for the best Advance Java Institute in Noida , APTRON Solutions is the ultimate choice. With our unwavering commitment to your success, expert faculty, comprehensive curriculum, and industry connections, we are your partner in advancing your Java programming skills and launching a successful career in the IT industry.
ReplyDelete