MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="185dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="220dp"
android:layout_marginEnd="160dp"
android:layout_marginBottom="8dp"
android:text="Alert"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.007" />
</android.support.constraint.ConstraintLayout>
MAINACTIVITY.JAVA
package com.example.alertdialogexample;
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.Button;
import android.widget.ViewAnimator;
import java.net.InterfaceAddress;
public class MainActivity extends AppCompatActivity {
private static Button button_sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onButtonClickListener();
}
public void onButtonClickListener()
{
button_sub=(Button)findViewById(R.id.button);
button_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_builder=new AlertDialog.Builder(MainActivity.this);
a_builder.setMessage("Do you want to close this apps!!!").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert =a_builder.create();
alert.setTitle("Alert !!!");
alert.show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="185dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="220dp"
android:layout_marginEnd="160dp"
android:layout_marginBottom="8dp"
android:text="Alert"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.007" />
</android.support.constraint.ConstraintLayout>
MAINACTIVITY.JAVA
package com.example.alertdialogexample;
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.Button;
import android.widget.ViewAnimator;
import java.net.InterfaceAddress;
public class MainActivity extends AppCompatActivity {
private static Button button_sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onButtonClickListener();
}
public void onButtonClickListener()
{
button_sub=(Button)findViewById(R.id.button);
button_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_builder=new AlertDialog.Builder(MainActivity.this);
a_builder.setMessage("Do you want to close this apps!!!").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert =a_builder.create();
alert.setTitle("Alert !!!");
alert.show();
}
});
}
}
If we are click on the no button then Alert app is not will be close.
click on the yes button then close this app.
0 comments:
Post a Comment