Home » Android

Working with Dialog (part-2) in Android

In this article, we are going to learn how to initialize a dialog and some of its pre-defined methods in Android?
Submitted by Manu Jemini, on December 10, 2017

Prerequisite: Working with Dialog (part-1) in Android

A dialog is a very easy way to show any texts, form, listview etc. The dialog provides very simple techniques to set it. For this tutorial to work, just create a new project with any name. You will get a default activity namely MainActivity, you can make use of it.

Starting with your XML file, drag, and drop Button, from the widget section. There is no implication about the parent layout type. Make your layout the way you want it. The button is taken because it will give us to show it on SetOnClickListener().

The Second thing is your JAVA file. Now in the java file, we’ve got to make a variable of type Button along side with AlertDialog.Builder and Context. Now what we would do is to set the button with an event listener. Now this event listener will make a new dialog and an EditText and two general purpose buttons.

This little implementation shows us clearly on how we should make more things on the same activity as the activity is still the same.

Button → click → Dialog → Info → ok/cancel → Button

Now, this is fairly possible to use Intent and move to the other activity, after getting the data.

After doing everything up, your code should look like this. Connect an Android device or run an ADV and then build your project to see the result.

Java file:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity
    AlertDialog.Builder ad=null;
    Button B;
    Context ctx=this;
    protected void onCreate(Bundle savedInstanceState)


        B.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ad=new AlertDialog.Builder(ctx);
                ad.setTitle("Include Help");
                ad.setMessage("Your Course name");
                EditText E=new EditText(ctx);
                ad.setNegativeButton("Cancel",new Cancel_Click());
                ad.setPositiveButton("OK",new Ok_Click());
class Cancel_Click implements DialogInterface.OnClickListener{

        public void onClick(DialogInterface dialog, int which) {

class Ok_Click implements DialogInterface.OnClickListener{

    public void onClick(DialogInterface dialog, int which) {




XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"

        android:hint="Click here to see dialog box"
        android:layout_marginRight="90dp" />

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



dialog box example 2 in Android

dialog box example 2 in Android

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.