Dialog Box ON aNDROID

Published on January 2017 | Categories: Documents | Downloads: 26 | Comments: 0 | Views: 339
of 22
Download PDF   Embed   Report

Comments

Content

Where is it?

ATTENTION ATTENTION gear.huuah.com has launched. Visit the shop at http://gear.huuah.com/. Lots of Photo Gear at the moment ATTENTION ATTENTION Itsplanned.com is just launched! - Task and project management made easy. Try it for free.
Create your own lists of things to do - arrange the order to do them - move them around - group them No limitations - all free project management - try the free demo before signing up - demo: itsplanned.com

Dialog boxes in Android
Dialog boxes
Showing dialog boxes from an android application can be usefull.

02.04.2010

Follow me on Twitter

Contents
Dialog boxes Info boxes Alert boxes Yes/no boxes

This article will be using a layout like this – 4 buttons with their own function (complete code id listed last in this article):

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Yes/no boxes Loading box Complete code main.xml dialogboxes.java

Related stuff
Android Progress Bar and Thread updating Using TableLayout on Android

Info boxes
An info box is called a Toast on the Android OS. A toast message is a small window that appear in the button half on the device screen. It should only be used as an information service to the user. The window disappears by itself, so as a developer you can not be sure that the user actually sees the message displayed. If you want to user to take action on the popup window, you should use an alert dialog instead – this is covered later on.

Android writing and reading files How to make tabs bookmarkable with jQuery The basics of Android intents and activities

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The Toast message is very easy to create. The most simple way to display a toast is by calling the maketext()-method like this: ... Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show(); ... This will display a box like this:

Further reading
JQuery

Alert boxes
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

TYPO3 Android

pdfcrowd.com

The Alert box should be use when you want to make sure, that the user reads and takes action on the information window. To display the alert window, can be done like this: ... // prepare the alert box AlertDialog.Builder alertbox = new AlertDialog.Builder(this); // set the message to display alertbox.setMessage("This is the alertbox!"); // add a neutral button to the alert box and assign a click listener alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

Android

Topics
Android Development

// click listener on the alert box Howto public void onClick(DialogInterface arg0, int arg1) { // the button was clicked Itsplanned.Com Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show(); } Java }); // show it alertbox.show(); JQuery Misc TemplaVoila

...

This will popup a window like this:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

TYPO3 Wordpress

When is OK button is clicked, it will – in this example – show a toast message like this:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Yes/no boxes
If one button on the alert dialog isn’t enough we can use the setPositiveButton() and setNegativeButton() on the AlertDialog Builder like this: ... // prepare the alert box AlertDialog.Builder alertbox = new AlertDialog.Builder(this); // set the message to display alertbox.setMessage("This is the alertbox!");

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

alertbox.setMessage("This is the alertbox!"); // set a positive/yes button and create a listener alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); }

});

// set a negative/no button and create a listener alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); }

});

...

// display box alertbox.show();

This will show a window like this:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

When either of the buttons is clicked it will run the assigned listener. Here we have clicked the yes button:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Loading box
If you want to display a loading window or some kind of progress alert it can be done with the ProgressDialog like this: ... // prepare the dialog box ProgressDialog dialog = new ProgressDialog(this); // make the progress bar cancelable dialog.setCancelable(true);

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

dialog.setCancelable(true); // set a message text dialog.setMessage("Loading..."); // show it dialog.show();

...

This will popup a progress window like this:

This window will not close by itself. You have to make it complete, by running a code that increment the internal counter on the ProgressDialog, so that the progressdialog can calculate the percentage by itself. For this we should use a thread-based application, that runs some code in the background and from there set the progress bar at the given value. This is not covered in this article though.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Complete code
The small code samples above are shown here in the complete formation, including the main.xml layout.

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" > <Button android:layout_width="wrap_content" <Button android:layout_width="wrap_content" <Button android:layout_width="wrap_content" <Button android:layout_width="wrap_content" </LinearLayout> android:layout_height="wrap_content" android:layout_height="wrap_content" android:layout_height="wrap_content" android:layout_height="wrap_content" android:text="Show android:text="Show android:text="Show android:text="Show Toast" Alert" Yes/No" Progress"

dialogboxes.java
package huuah.dialogboxes; import import import import import import import import import android.app.Activity; android.os.Bundle; android.view.View.OnClickListener; android.view.View; android.widget.Button; android.widget.Toast; android.app.AlertDialog; android.content.DialogInterface; android.app.ProgressDialog;

public class dialogboxes extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

setContentView(R.layout.main); // set a click listener on the toast button Button toast = (Button) findViewById(R.id.toastbtn); toast.setOnClickListener(this); // set a click listener on the alert button Button alert = (Button) findViewById(R.id.alertbtn); alert.setOnClickListener(this); // set a click listener on the yesno button Button yesno = (Button) findViewById(R.id.yesnobtn); yesno.setOnClickListener(this); // set a click listener on the progress button Button progress = (Button) findViewById(R.id.progressbtn); progress.setOnClickListener(this);

}

public void onClick(View view) { // which button is clicked? // the Toast button if (view == findViewById(R.id.toastbtn)) { // display the toast popup window Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show(); } // the Alert button the activated if (view == findViewById(R.id.alertbtn)) { // prepare the alert box AlertDialog.Builder alertbox = new AlertDialog.Builder(this); // set the message to display alertbox.setMessage("This is the alertbox!"); // add a neutral button to the alert box and assign a click listener alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() { // click listener on the alert box public void onClick(DialogInterface arg0, int arg1) { // the button was clicked Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show(); }

});

// show it alertbox.show();

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

}

alertbox.show();

// the yesno button is clicked if (view == findViewById(R.id.yesnobtn)) { // prepare the alert box AlertDialog.Builder alertbox = new AlertDialog.Builder(this); // set the message to display alertbox.setMessage("This is the alertbox!"); // set a positive/yes button and create a listener alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); }

});

// set a negative/no button and create a listener alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); }

});

}

// display box alertbox.show();

// progress button clicked if (view == findViewById(R.id.progressbtn)) { // prepare the dialog box ProgressDialog dialog = new ProgressDialog(this); // make the progress bar cancelable dialog.setCancelable(true); // set a message text dialog.setMessage("Loading..."); // show it dialog.show();

} }

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

}

}

I hope this will the useful. If you want to read more on this subject, then take a look at some of the Android books out there

18 Responses to “Dialog boxes in Android”
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

1.

Ruwan Says:
april 25th, 2010 at 17:23

thanks man , this helped !!!

2.

zire Says:
juni 20th, 2010 at 22:06

Thank you very much for those samples. I search for a while and this is the best and simplest .

3.

Arshad Says:
se pte m be r 13th, 2010 at 03:35

Thanks a lot man,Very nice examples with codes. Peace.

4.

rosebeat Says:
se pte m be r 14th, 2010 at 08:49

I get an error message as Application has stopped unexpectedly and I have to force close it. Why is that????

5.

David Carral Says:
nove m be r 2nd, 2010 at 16:41

Thanks a lot, very very nice, easy, simple & good examples, you saved my ass!!

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

6.

Afonso Lage Says:
nove m be r 3rd, 2010 at 21:26

Man, I’m so glad with this post. Very nice example and this SS help us a lot. Thx agin. Att, Afonso Lage.

7.

Priyanka Says:
nove m be r 17th, 2010 at 14:38

Thanks.. Very good and helpful..

8.

Rob Says:
fe bruar 23rd, 2011 at 01:51

Fantastic examples, straight to the point, just what i needed to implement a alert dialog with ok button. The toast example is very helpful too

9.

Malagas Says:
m arts 13th, 2011 at 22:00

This is just excellent. I’ve tried some examples of dialogs and button events but they didn’t work well. This works fine!!! Thanks for your example.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

10.

Malagas Says:
m arts 13th, 2011 at 22:19

Now watch the results: Here is a custom dialog, from a XML file layout The main.xml: Now the costum_dialog.xml : (English error :S) and strings.xml: CustomDialogEx App! Custom_Dialog_Ex finally CustomDialogEx.java : package com.example.custom_diag; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.View.OnClickListener; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.app.ProgressDialog; public class CustomDialogEx extends Activity implements OnClickListener{

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

/** Called when the activity is first created. */ static final int CUSTOM_DIAG = 10; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* Context mContext = getApplicationContext(); Dialog dialog = new Dialog(mContext); dialog.setOwnerActivity(this); dialog.setContentView(R.layout.costum_dialog); dialog.setTitle(“Custom Dialog”); TextView text = (TextView) dialog.findViewById(R.id.text); text.setText(“Hello, this is a custom dialog!”); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.icon); */ Button btn_diag = (Button) findViewById(R.id.button1); btn_diag.setOnClickListener(this); /* btn_diag.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), “Bosta”, Toast.LENGTH_SHORT).show(); //showDialog(CUSTOM_DIAG); } }); */ // dialog.show(); }

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

@Override public void onClick(View v) { // TODO Auto-generated method stub if(v == findViewById( R.id.button1)) { //Context mContext = getApplicationContext(); Dialog dialog = new Dialog(this); //dialog.setOwnerActivity(this); dialog.setContentView(R.layout.costum_dialog); dialog.setTitle(“Custom Dialog”); TextView text = (TextView) dialog.findViewById(R.id.text); text.setText(“Hello, this is a custom dialog!”); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.icon); dialog.show(); } } /* @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub switch (id) { case CUSTOM_DIAG: Toast.makeText(getApplicationContext(), “Bosta”, Toast.LENGTH_LONG).show(); break; default: break; }

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

return super.onCreateDialog(id); } */ } 11. Android Custom Dialog Example | Malagas Home Says:
m arts 14th, 2011 at 18:22

[...] event OnClick from a Button, here’s an example that i have made with the major help from this DialogBox [...]

12.

Android Says:
m arts 15th, 2011 at 12:09

nice, but if you like to see AlertDialog alone in detail means see http://android-codesexamples.blogspot.com/2011/03/how-to-display-alertdialog-and.html

13.

Satish Says:
m arts 28th, 2011 at 13:08

Very nice dude! Very helpful for learners. Keep it up!

14.

Akshay Says:
m aj 20th, 2011 at 13:20

Hi Huuha, Its a nice tutorial.Again i got help from your example.Thanks plz continue writing……….

15.

Roche Says:
juli 18th, 2011 at 10:59

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Thank you for this example. Hope you continue writing useful examples for android developers. More power to you.

16.

dixit Says:
juli 30th, 2011 at 22:15

Hello great work really nice tutorial….

17.

Andrew Says:
august 5th, 2011 at 06:53

Great stuff, I’ve been going insane trying to figure alertboxes out and this tut has answered all of my questions! Easily the best alertbox example out there, thank you very much!!! 18. Android Custom Dialog Example | Malagas Dev Says:
nove m be r 2nd, 2011 at 14:27

[...] a Button, here’s an example of a Custom Dialog that i have made with the major help from this DialogBox [...]

Leave a Reply
Nam e (re quire d)

Mail (will not be publishe d) (re quire d)

W e bsite

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Submit Comment

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close