Android Dialogs are a little awkward to handle because (long story short) clicking on one of the ok/cancel/etc buttons auto-dismisses the dialog. so you can't validate while the dialog is up, you have to re-show the dialog.

after the 10th dialog i found this to be a pain so i wrote a wrapper which wraps AlertDialog and calls a listener to (a) validate the dialog so that the OK button can be enabled appropriately (b) handle a click on UI elements such as buttons or checkboxes (c) handle OK and Cancel actions.

since writing this wrapper, i have found dialogs to be a pleasure, as opposed to a bit of a pain! and hence i recommend doing something similar :-)



You must be absolutely sure that the 'alert(Strin msg)' runs in a
different thread than the UI-thread.

Why do it this way?

You can just call showDialog(int dialogID) in your activity.
Then in the onClickListener of this dialog, you can implement the
onClick method to handle the case when the user clicks OK, do what
needs to be done and dismiss the dialog.

On Feb 8, 5:02 pm, guich <[email protected]> wrote:
 Hi,

 I decided to share the solution i found to the problem of Android not
 supporting real modal dialogs.

 The solution was to draw the dialog by myself. :-( Well, at least
 worked fine, besides the problem that its a bit ugly. :-)

 Here's the code i used:

    static boolean alertShowing;

    public static void alert(String msg)
    {
       int scrW=instance.getWidth(),scrH=instance.getHeight();
       alertShowing = true;
       final int fh = 18;
       // note: we have to draw the dialog on screen, otherwise if we
 use Dialog, the application just quits
       String[] msgs = msg.split("\n");
       Canvas canvas = surfHolder.lockCanvas();
       // fill with white
       Paint p = new Paint();
       p.setColor(0xFFFFFFFF);
       canvas.drawRect(0,0,scrW,scrH,p);
       p.setColor(0xFFAAAAAA);
       int ly = fh+fh/2;
       canvas.drawRect(0,0,scrW,ly,p);
       // draw caption in black
       p.setColor(0xFF000000);
       p.setStyle(Paint.Style.STROKE);
       canvas.drawRect(0,0,scrW-1,scrH-1,p);
       p.setAntiAlias(true);
       p.setTextSize(fh);
       p.setTypeface(Typeface.DEFAULT_BOLD);
       p.setTextAlign(Paint.Align.CENTER);
       canvas.drawText("ALERT",scrW/2,fh+fh/4,p);
       canvas.drawLine(0,ly,scrW,ly,p);
       // draw message
       int maxy = scrH-fh*2;
       p.setTypeface(Typeface.DEFAULT);
       for (int i =0,y=fh*3; i < msgs.length && y < maxy; i++, y += fh
 +2)
          canvas.drawText(msgs[i], scrW/2, y,p);

       String ok = "Ok";
       int tx = scrW/2;
       int ty = maxy+ly;
       canvas.drawText(ok,tx,ty,p);
       Rect bounds = new Rect();
       p.getTextBounds(ok, 0, ok.length(), bounds);
       int ww = bounds.width();
       int hh = bounds.height();
       canvas.drawRect(tx-ww,ty-hh-3,tx+ww,ty+3,p);

       surfHolder.unlockCanvasAndPost(canvas);
       while (alertShowing)
          try {Thread.sleep(200);} catch (Exception e) {}
    }

    public boolean onTouchEvent(MotionEvent event)
    {
       if (alertShowing)
       {
          alertShowing = false;
          return true;
       }
    }

 HTH.

    guich
    http://www.totalcross.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
jason.vp.engineering.particle

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to