Dialog Window Example
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Dialog Window Example

If you want to get a confirmation from the user and at that time if you need to display a dialg window then you can define a method in the base class that overrides the onCreateDialog() protected method.

Here is an example of dialog window, follow the steps -

  1. First of all create a new project and name it Dialog.
     
  2. Now you have to add the following statements in the activity_main.xml
    <?xml version=”1.0” encoding=”utf-8”?>
    <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
    android:orientation=”vertical”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent” >
    <TextView
    android:layout_width=”fill_parent”
    android:layout_height=”wrap_content”
    android:text=”@string/hello” />
    <Button
    android:id=”@+id/btn_dialog”
    android:layout_width=”fill_parent”
    android:layout_height=”wrap_content”
    android:text=”Click to display a dialog” />
    </LinearLayout>

     
  3. Now you have to add the following statements in the MainActivity.java

    package com.example.Dialog;
    import android.app.Activity;
    import android.os.Bundle;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    public class MainActivity extends Activity {
    CharSequence[] items = { “Google”, “Apple”, “Microsoft” };
    boolean[] itemsChecked = new boolean [items.length];
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.btn_dialog);
    btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    showDialog(0);
    }
    });
    }
    @Override
    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0:
    return new AlertDialog.Builder(this)
    .setIcon(R.drawable.icon)
    .setTitle(“This is a dialog with some simple text...”)
    .setPositiveButton(“OK”, new
    DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,
    int whichButton)
    {
    Toast.makeText(getBaseContext(),
    “OK clicked!”, Toast.LENGTH_SHORT).show();
    }
    })
    .setNegativeButton(“Cancel”, new
    DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,
    int whichButton)
    {
    Toast.makeText(getBaseContext(),
    “Cancel clicked!”, Toast.LENGTH_SHORT).show();
    }
    }) 
    .setMultiChoiceItems(items, itemsChecked, new
    DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which,
    boolean isChecked) {
    Toast.makeText(getBaseContext(),
    items[which] + (isChecked ? “ checked!”:
    “ unchecked!”),
    Toast.LENGTH_SHORT).show();
    }
    }
    )
    .create();
    }
    return null;
    }
    }

     

 

 

← Preview

Hiding Activity Title

Next →

Progress Dialog Example

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App