Note this could only possibly work if your drawing (and blocking) code is
not running in the main UI thread, in which case you could just as well have
it ask the main thread to show the dialog, and have this thread block
waiting for the user to respond to it.  (And of course you need to deal with
the user leaving your app when it is in a state like this, and the system
kindly asking you to free up your windows and surfaces and stuff, and
actually doing that for it instead of sitting there stuck.)

On Mon, Feb 8, 2010 at 2: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]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
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