Re: [android-developers] Re: Modal Dialogs: the definitive solution
On 2/13/10, kamran Manzoor wrote: > > /// > Salam to All > I am trying to built the login screen, during this process i have a problem > that > package com.net.login; > > import android.app.Activity; > import android.content.DialogInterface; > import android.content.DialogInterface.OnClickListener; > import android.os.Bundle; > import android.view.View; > import android.widget.Button; > import android.widget.EditText; > import android.widget.TextView; > > public class login extends Activity { > /** Called when the activity is first created. */ > String username , password; > Button login; > TextView tv = new TextView(this); > EditText name = new EditText(this); > EditText pass =new EditText(this); > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > // > > // > name = (EditText)findViewById(R.id.user_name); > pass = (EditText)findViewById(R.id.password); > login = (Button)this.findViewById(R.id.login); > > username = name.getText().toString(); > password = pass.getText().toString(); > login.setOnClickListener((android.view.View.OnClickListener) > loginListener); > } > private OnClickListener loginListener = new OnClickListener() > { > public void onClick(View v) > { > if (username == "kami" && password == "kami") > { > //TextView tv = new TextView(this); > tv.setText(" Login is Proved"); > setContentView(tv); > > //System.out.println("login proved"); > } > else > { > // TextView tv = new TextView(this); > tv.setText(" Passwod is incorrect "); > setContentView(tv); > > } > //System.out.println("login proved"); > } > @Override > public void onClick(DialogInterface dialog, int which) { >// TODO Auto-generated method stub > > } > > }; > > } > /// > > emulator is stopped forcely -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
/// Salam to All I am trying to built the login screen, during this process i have a problem that package com.net.login; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class login extends Activity { /** Called when the activity is first created. */ String username , password; Button login; TextView tv = new TextView(this); EditText name = new EditText(this); EditText pass =new EditText(this); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // // name = (EditText)findViewById(R.id.user_name); pass = (EditText)findViewById(R.id.password); login = (Button)this.findViewById(R.id.login); username = name.getText().toString(); password = pass.getText().toString(); login.setOnClickListener((android.view.View.OnClickListener) loginListener); } private OnClickListener loginListener = new OnClickListener() { public void onClick(View v) { if (username == "kami" && password == "kami") { //TextView tv = new TextView(this); tv.setText(" Login is Proved"); setContentView(tv); //System.out.println("login proved"); } else { // TextView tv = new TextView(this); tv.setText(" Passwod is incorrect "); setContentView(tv); } //System.out.println("login proved"); } @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }; } /// -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
You probably want to pass the view in (or just the handler). If your thread is updating some part of the UI, it would make sense to hand it the view it is associated with. As far as race conditions... at the end of the day, there is some thread that is going to draw that view hierarchy, and if you are not in that thread, you are going to be racing with it. Enqueueing a message in its handler allows you to avoid those races, by propagating your state to it in a controlled way: you update the state in your thread, deliver it on a handler, and the state gets set in another thread. If the state changes before the other thread gets a chance to set it, well you will be sending another message/runnable anyway with the new state that is guaranteed to execute after whatever previous one you set. That is the pattern, but you can make it more complicated. For example if you have a lot of state you can protect it with a lock. Every time you access it, acquire the lock. When you modify it, lock, modify, send a message to the view just that it has changed. When the view gets the message, it acquires the lock and does whatever is appropriate with the lock held. It will always setting down to having the most recent state. On Thu, Feb 11, 2010 at 11:49 AM, Berek wrote: > How do you get a View from the current window (Activity?) using this > method in a worker thread (not GUI)? And, even if you did, there is > still a window (of execution) between the time you get the view and > the time the runnable runs. The Activity may have changed in that > window of execution. > > On Feb 11, 12:49 pm, Dianne Hackborn wrote: > > On Tue, Feb 9, 2010 at 9:39 AM, Frank Weiss wrote: > > > The conundrum on Android is that user code can't block in a UI thread > and > > > non-UI threads can't manipulate the UI. > > > > It is almost trivial to, from another thread, schedule a Runnable on the > > window's thread to do work on it: > > > > void myThreadDoingStuff() { > > // ... > > final int viewVisibility = somethingWeComputed; > > // > > final View view = getSomeViewInWindow(); > > view.getHandler().post(new Runnable() { > > public void run() { > > view.setVisibility(viewVisibility); > > } > > }); > > // ... > > > > } > > > > -- > > Dianne Hackborn > > Android framework engineer > > hack...@android.com > > > > 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 android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
Thanks, i'll try that. regards guich -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
How do you get a View from the current window (Activity?) using this method in a worker thread (not GUI)? And, even if you did, there is still a window (of execution) between the time you get the view and the time the runnable runs. The Activity may have changed in that window of execution. On Feb 11, 12:49 pm, Dianne Hackborn wrote: > On Tue, Feb 9, 2010 at 9:39 AM, Frank Weiss wrote: > > The conundrum on Android is that user code can't block in a UI thread and > > non-UI threads can't manipulate the UI. > > It is almost trivial to, from another thread, schedule a Runnable on the > window's thread to do work on it: > > void myThreadDoingStuff() { > // ... > final int viewVisibility = somethingWeComputed; > // > final View view = getSomeViewInWindow(); > view.getHandler().post(new Runnable() { > public void run() { > view.setVisibility(viewVisibility); > } > }); > // ... > > } > > -- > Dianne Hackborn > Android framework engineer > hack...@android.com > > 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
On Tue, Feb 9, 2010 at 9:39 AM, Frank Weiss wrote: > The conundrum on Android is that user code can't block in a UI thread and > non-UI threads can't manipulate the UI. > It is almost trivial to, from another thread, schedule a Runnable on the window's thread to do work on it: void myThreadDoingStuff() { // ... final int viewVisibility = somethingWeComputed; // final View view = getSomeViewInWindow(); view.getHandler().post(new Runnable() { public void run() { view.setVisibility(viewVisibility); } }); // ... } -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
On Thu, Feb 11, 2010 at 3:49 AM, guich wrote: > I also found that calling the alert from the VM works fine. However, > calling from the SurfaceView will freeze the application. So, i > changed the method to wait a few seconds and then quit if its being > called from a View. > Um. You should probably sit down and understand the threading going on in your program, and have it to the correct things based on that, rather that using timeouts (with a few seconds!) to work around bugs in it. If you are saying that "calling from the SurfaceView" means you are trying to do your block dialog hack from a callback on SurfaceView, then yes this will deadlock, because you are doing this on the main thread, because all of these callbacks are defined to happen on the thread running the containing window, which for normal windows is the main thread. So just don't do that. -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
I also found that calling the alert from the VM works fine. However, calling from the SurfaceView will freeze the application. So, i changed the method to wait a few seconds and then quit if its being called from a View. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
The conundrum on Android is that user code can't block in a UI thread and non-UI threads can't manipulate the UI. I can image that some code is just written so procedurally that blocking Alert calls is the straight-forward way to do it. On the other hand, breaking down a long procedure into shorter methods usually solves the problem. Perhaps if someone could write a coroutine library for Android we wouldn't have to explain this over and over and developers who want to or have to write long blockable procedures will be happy. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
My application is not a standard application. Is a virtual machine. This is the schema: totalcross application <-> totalcross vm <-> native code <-> dalvik vm <-> totalcross' android classes When the TotalCross application calls Vm.alert("hi!"), it is expecting that the call will resume ONLY after the alert is closed. I didn't find another way to do this. regards guich -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
On Tue, Feb 9, 2010 at 4:20 AM, guich wrote: > The big problem is that i really need to block the application from > the caller. > And why do you "really" need to block the application? - TreKing - Chicago transit tracking app for Android-powered devices http://sites.google.com/site/rezmobileapps/treking -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Modal Dialogs: the definitive solution
Just curious what "caller" means in that context? -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
Hi! The big problem is that i really need to block the application from the caller. Based on Dianne's suggestion, i'm now also making alertShowing = false; in the onPause method. regards guich -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
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 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(0x); canvas.drawRect(0,0,scrW,scrH,p); p.setColor(0xFFAA); int ly = fh+fh/2; canvas.drawRect(0,0,scrW,ly,p); // draw caption in black p.setColor(0xFF00); 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Modal Dialogs: the definitive solution
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 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(0x); > canvas.drawRect(0,0,scrW,scrH,p); > p.setColor(0xFFAA); > int ly = fh+fh/2; > canvas.drawRect(0,0,scrW,ly,p); > // draw caption in black > p.setColor(0xFF00); > 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: modal dialogs
More than being accepted, it's the only way. If you do blocking operations in the main thread, you will soon give the user an "Application Not Responding" dialog and they will kill you. Now you -can- create another thread and run a dialog window thread... but any thread running UI has the same "do not block for a significant amount of time" rule, and thus result in ANRs if it is not responding so the UI is responsive to the user. On Nov 7, 3:00 pm, Jason Proctor <[EMAIL PROTECTED]> wrote: > thanks for the response. > > ok so that's the accepted way to do it. great. > > >Fine. Yes, the only way to get a response from a button is via the > >onClick() handler. So yes, you use that to start up a thread and put > >up a progress dialog, then close it when the thread has done its long > >transaction. > > >Or not have the progress dialog - then the rest of the UI is still > >active while the other stuff is going on in the background, if that's > >what you prefer. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com 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 -~--~~~~--~~--~--~---
[android-developers] Re: modal dialogs
Fine. Yes, the only way to get a response from a button is via the onClick() handler. So yes, you use that to start up a thread and put up a progress dialog, then close it when the thread has done its long transaction. Or not have the progress dialog - then the rest of the UI is still active while the other stuff is going on in the background, if that's what you prefer. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com 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 -~--~~~~--~~--~--~---
[android-developers] Re: modal dialogs
Eh, blocking on user input is rarely the way to do things. Your activity has to be able to respond to other events - such as being killed or put to sleep when the phone rings. A mobile phone is an event driven environment - so Google built Android that way. If I read your post right, I'd be doing anything that's a long server transaction in a thread, and if the user must wait for that, I'd present them with a progress dialog, which is dismissed when the thread completes (i.e. posts via a Runnable). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com 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 -~--~~~~--~~--~--~---