Quick MFC style message box

Quickly share your Android Code Snippets here...

Quick MFC style message box

Postby soeet » Mon Feb 09, 2009 2:02 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3.  
  4. import android.app.AlertDialog;
  5.  
  6. import android.content.Context;
  7.  
  8.  
  9.  
  10. public class lib {
  11.  
  12.  
  13.  
  14.         public static void AfxMessageBox(String message, Activity actv) {
  15.  
  16.                 AfxMessageBox(message, actv, "Alert");
  17.  
  18.         }
  19.  
  20.        
  21.  
  22.         public static void AfxMessageBox(String message, Activity actv, String title) {
  23.  
  24.                 final String lMessage = message;
  25.  
  26.                 final Context lContext = (Context)actv;
  27.  
  28.                 final String lTitle = title;
  29.  
  30.                 actv.runOnUiThread(new Runnable() {
  31.  
  32.                         public void run() {
  33.  
  34.                                 errorMessageString(lMessage, lContext, lTitle);
  35.  
  36.                         }
  37.  
  38.                 });
  39.  
  40.         }
  41.  
  42.        
  43.  
  44.         private static void errorMessageString(String message, Context cxt, String title) {
  45.  
  46.                 AlertDialog.Builder builder=new AlertDialog.Builder(cxt);
  47.  
  48.                
  49.  
  50.                 builder
  51.  
  52.                         .setTitle(title)
  53.  
  54.                         .setMessage(message)
  55.  
  56.                         .setPositiveButton("OK", null)
  57.  
  58.                         .show();
  59.  
  60.         }
  61.  
  62. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


call from your activity

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. lib.AfxMessageBox("Debug 1", this);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Non-modal of course! :)
soeet
Freshman
Freshman
 
Posts: 2
Joined: Mon Feb 09, 2009 1:57 am

Top

Postby MrSnowflake » Mon Feb 09, 2009 12:32 pm

That would work indeed and actually looks quite handy. But you could better use the onCreateDialog() and onPrepareDialog() in Activity and use showDialog() accordingly. You should then override showDialog() to not only take the id of the MessageBox (if there's only one possible dialogbox which enables you to easilly handle messages, then this id could be left out) and the message itself. In onPrepareDialog() you then update the string which will be shown in the dialog created in onCreateDialog().

This way, it's more Android, yet should be as easy to use as your proposition, and the main advantage is that Android handles the configuration change, so the dialog will stay visible if you change orientation.

Though I just looked it up, onCreateDialog() and onPrepareDialog() are protected members, so the use of this Android based approach would require more input from the developer, so that makes it less attractive, though, this implementation could be done using 1 line for each on..Dialog().

Hope you get what I'm trying to say :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests