[TinyTut] - Displaying a Messagebox

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] - Displaying a Messagebox

Postby plusminus » Sun Nov 18, 2007 2:35 pm

[TinyTut] - Displaying a Messagebox


What you learn: You will learn how easy it is, to display a MessageBox (showAlert(...)).

Difficulty: 0 of 5 :roll:

What it will look like:
Image


Description
This has been a very common question, as in "normal" Java a MessageBox was displayed with code like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. JOptionPane.showMessageDialog(...);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

in Android you can do almost the same with the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. showAlert("A funny title", "MessageBoxes rule extremely!", "Hit Me!", false);
  2. // Where the definition of the function is (simplest of 4 different):
  3. showAlert(CharSequence title, CharSequence message, CharSequence buttonText, boolean cancelable)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Tue Nov 20, 2007 12:19 am, edited 3 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby sumet » Mon Nov 19, 2007 5:16 pm

I like this way it vary ease.
thank you
sumet
Freshman
Freshman
 
Posts: 5
Joined: Sat Nov 17, 2007 6:02 pm

showAlert is non-blocking

Postby ponta » Fri Nov 23, 2007 9:59 pm

The showAlert method is non-blocking so the alert dialog that appears on the screen does not stay there. It appears and disappears quickly. Isn't there a modal dialog alert in Android?
ponta
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 23, 2007 9:51 pm

Re: showAlert is non-blocking

Postby plusminus » Sat Nov 24, 2007 12:05 pm

ponta wrote:The showAlert method is non-blocking so the alert dialog that appears on the screen does not stay there. It appears and disappears quickly. Isn't there a modal dialog alert in Android?


Completely blocking Dialogs are not possible in Android (as far as I know). Think of what would happen if there is an incoming Phone-Call... :!: :?: :!:

You could do an easy workaround by starting an (sub)Activity, which can return some values (that tut is next).

[Edit:]

I did that tutorial, take a look here: (click)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Then what's the purpose of buttons?

Postby ponta » Sat Nov 24, 2007 8:14 pm

Thanks for your reply, plusminus.

If the alert is non-blocking, then what's the purpose of having buttons? The users won't have any chance to click on them! The point on the need to process the incoming call seems logical, but I thought the alert would block the owning activity (the one that opened the alert), not other activities such as incoming calls. I guess I'm still not comfortable with the Android way of thinking...

Your tutorial on starting subactivities is helpful. Thanks.
ponta
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 23, 2007 9:51 pm

Postby plusminus » Sat Nov 24, 2007 8:45 pm

Hello ponta,

no problem :)

:idea: showAlert(...) got many overloads, like you can pass an OnClickListener() and/or an OnCancelListener() to the showAlert(...) to react on those ButtonClicks :!:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. showAlert(CharSequence title, CharSequence message, CharSequence buttonText, OnClickListener buttonListener, boolean cancelable, OnCancelListener cancelListener)
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


But i still not have seen an "real" parent-blocking-dialog :? (except that Activity-workaroung).
I'll inform you if I find sth.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby XterierK » Mon Nov 26, 2007 7:25 am

Its nice being able to understand what I'm looking at, thanks!
Limited Imagination = Impossible!
XterierK
Freshman
Freshman
 
Posts: 4
Joined: Mon Nov 26, 2007 7:22 am

Postby omega_z(x) » Mon Nov 26, 2007 11:23 pm

intresting... :D

so it is 50% programming and 50% android programming :lol:

so anyway, (I know only Pascal and C++) I understand the syntax :twisted:

it is there any way to programming in C++ ? or I should take a look on Java syntax ? (oance i've learned pseudocode is easyer to understand te programming)

{somehow still looks like C++}

i will give it a try to see what I get 8)

I would have a BIG question: The all programming stuff (Eclipse + Android SDK) will work the same in other partition of my PC ? ([C:Windows] [D:User Files] [E:Free Space]) I want to install it in E
Image
Image
Image
omega_z(x)
Freshman
Freshman
 
Posts: 3
Joined: Mon Nov 26, 2007 11:02 pm

Postby plusminus » Mon Nov 26, 2007 11:41 pm

Hello

yes there is a way of running C++ on the Android-Emulator ( have a look here ).

C++ and JAVA syntax are almost the same,so it is your choice ;) I would (do) prefer JAVA, because it is about 10^10 times nicer and more comfortable to code than C++ :)

Wont open and wounds here ;)

The SDK on your pc is just a folder you unzip the downloaded SDK to. As you only have to referr to it in the Eclipse-Preferences there should be no problem.
Also Eclipse should not be worried if you move or reinstall it, as the settings ( not the plugins(like ADT), be their settings too ;) ) are saved somewhere like in the "C:/user/<loginname>/Local_Data/Eclipse". The Emulator-Image is also saved in a folder on 'C:' ( C:UsersplusminusAppDataLocalAndroiduserdata.img).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

showAlert overload

Postby bushpig » Fri Feb 01, 2008 6:01 pm

Plusminus,
You touched on the showAlert(...) overloads, one of which has an OnClickListener().

Where/How would you define the listener in your code (as this is where the code would do the work one user clicked the button). Looked all through the Android documentation but couldn't find the answer. Thanks!
bushpig
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Feb 01, 2008 5:40 pm

Postby plusminus » Fri Feb 01, 2008 7:11 pm

Hello bushpig,

I prefer using anonymous Classes for things like a OnClickListener doing not much, because it is just used in that one single place and no other class need to know that that OnClickListener-code exists.
Code would be like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.content.DialogInterface;
  2.                 // ...
  3.                 // ...
  4.                 showAlert("tiel", "message", "buttonText", new DialogInterface.OnClickListener(){
  5.                         @Override
  6.                         public void onClick(DialogInterface arg0, int arg1) {
  7.                                 // Do some onClick-work...
  8.                         }
  9.                 }, false, null);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Defining more than one Anonymous-Class within one function-call is not really nice code-style (but works , of course). So then I would define them still very close...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Sun Feb 03, 2008 2:45 pm

This is handy to know how to do. Can we alter the appearance of the messagebox? (Change the color, move the button...) Thanks
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Sun Feb 03, 2008 9:24 pm

ramgraph1 wrote:This is handy to know how to do. Can we alter the appearance of the messagebox? (Change the color, move the button...) Thanks


I haven't had a look on that yet... but there is always a way ;)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

showalert looks different on m5

Postby drhu00 » Sun Mar 09, 2008 2:50 am

plusminus
Have you notice that showAlert looks different on m5 than this page. Ex, the button is too big and there is no hidden line separate the title and message.

If I want a similar one in this page, how should I do?
drhu00
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Feb 12, 2008 6:41 pm

Postby plusminus » Sun Mar 09, 2008 12:31 pm

Hello drhu00,

Remember: for the success of the Android platform, all applications should have a similar look and feel.

One thing that could work is to use the Light-Theme, which currently still holds some UI-Elements of the old UI.
I don't know whether it also has impact on the appereance of the Alert.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setTheme(android.R.style.Theme_Light);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: Google [Bot] and 4 guests