Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to show Yes No dialog on Android

Posted on October 10, 2017August 4, 2020 by Habibie

To show a Yes No dialog in Android, first create a new project, drag and drop a button to your layout file. Then, use this source code for your Main Activity file:

package com.zofiakreasi.yesnodialog;

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.Toast;

public class MainActivity extends AppCompatActivity {

    Button myButton;
    AlertDialog.Builder myYesNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myButton = (Button) findViewById(R.id.button);

        myButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                showYesNo();
            }
        });

        myYesNo = new AlertDialog.Builder(this);
        myYesNo.setTitle("Yes No Dialog Example");
        myYesNo.setMessage("Please choose between Yes and No.");
        myYesNo.setIcon(android.R.drawable.ic_dialog_alert);
        myYesNo.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "You have chosen Yes.", Toast.LENGTH_SHORT).show();
            }
        });
        myYesNo.setNegativeButton("No", null);

    }

    public void showYesNo(){
        myYesNo.show();
    }
}

Here is the screenshot:

Post Views: 396
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme