Cara menampilkan notifikasi alert di android studio

Tutorial Android

Cara menampilkan notifikasi alert di android studio

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">

<Button

android:id="@+id/Btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="150dp"

android:layout_marginTop="200dp"

android:text="Logout"

android:background="@color/colorAccent"/>

</RelativeLayout>

 

MainActivity.java

package com.example.alertdialogexample;

import androidx.appcompat.app.AlertDialog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.Btn);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("Logout Alert")

.setMessage("Are you sure want to logout ?")

.setCancelable(false)

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this,"Selected Option: YES",Toast.LENGTH_SHORT).show();

}

})

.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this,"Selected Option: No",Toast.LENGTH_SHORT).show();

}

});

//Creating dialog box

AlertDialog dialog = builder.create();

dialog.show();

}

});

}

}

 

 

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *