What is Dialog:-
A dialog is a small window that prompts the user to make a
decision or enter additional information. A dialog does not fill the screen and
is normally used for modal events that require users to take an action before
they can proceed.
in Android Application Development There are Five Types of Dialog Boxes There are
1.Alert Dialog
2.Progress Dialog
3.Custom Dialog
4.Date Picker Dialog
5.Time Picker Dialog
1.Alert Dialog
Some times in your application,
if you wanted to ask the user about taking a decision between yes or no in
response of any particular action taken by the user, by remaining in the same
activity and without changing the screen, you can use Alert Dialog.
activity_main.xml
----------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:background="#7af0f0"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exit" android:id="@+id/button" android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity.Java
---------------------------
package com.android.anil.alertdialogbox; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button exitbtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); exitbtn=(Button)findViewById(R.id.button); exitbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("Exit"); builder.setMessage("are you sure do you want exit the app"); builder.setIcon(R.mipmap.ic_launcher); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); Toast.makeText(getApplicationContext(), "App is Closed", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "App is Not Closed", Toast.LENGTH_LONG).show(); } }); AlertDialog alertDialog =builder.create(); alertDialog.show(); } }); } }
Output is:
--------------------
No comments:
Post a Comment