A Dialog is a small window that prompts the user to a decision or enters additional information.
Sometimes in your application, if you wanted to ask the user about taking a decision between yes or no in the response to any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.you need to make an object of AlertDialogBuilder which an inner class of AlertDialog.
Sometimes in your application, if you wanted to ask the user about taking a decision between yes or no in the response to any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.you need to make an object of AlertDialogBuilder which an inner class of AlertDialog.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);Now you have to set the positive (yes) or negative (no) button using the object of the AlertDialogBuilder class
alertDialogBuilder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) alertDialogBuilder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)
Here is the Code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Alert Dialog Demo"
android:textSize="30sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="42dp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:text="Android Is Simples"
android:textColor="#fc1214"
android:textSize="30sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:contentDescription="@null"
android:src="@drawable/android" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Alert Dialog"
android:id="@+id/button"
android:background="#fc1214"
android:textColor="#fff"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:onClick="open"
android:layout_marginTop="90dp"/>
</RelativeLayout>
Here is the Code of MainActivity.java
package com.mahesh.androidissimples.alertdialogdemo;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage("Are you sure You wanted t make decision");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You clicked yes button", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
No comments:
Post a Comment