We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'd like to have simple AlertDialog window with OK confirm button in my processing sketch for Android target...
I've found example code here : http://forum.processing.org/one/topic/android-pop-up-windows#25080000001725349.html
but it doesn't work. It seems that fix is not that hard but is beyond my newbie knowledge in Android field... There is a hint how to solve this but I don't know how to use it (it's commented)...
My code :
//Import required classes import android.app.AlertDialog; import android.content.DialogInterface;
// Activity activity;
void setup() {
// activity=(Activity) this.context;
// activity.runOnUiThread(Runnable r) ;
//Create the AlertDialog
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
//Set the title
alertDialog.setTitle("Alert");
//Set the message
alertDialog.setMessage("This is an AlertDialog!");
//Add the OK button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//The OK button has been clicked
}});
//Display the AlertDialog
alertDialog.show();
}
void draw() {
}
I get this error :
FATAL EXCEPTION: Animation Thread java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:121) at android.app.Dialog.(Dialog.java:101) at android.app.AlertDialog.(AlertDialog.java:67) at android.app.AlertDialog$Builder.create(AlertDialog.java:800) at processing.test.popup_test.popup_test.setup(popup_test.java:35) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:1019)
Would kindly ask for example code that works or any other help or pointer...
Thanks in advance, regards, Bulek.
Answers
You must run the code on the UI thread - it looks like you have an attempt to do this commented out... try this instead:
Hi,
thanks for help. With some slight corrections this works now... I guess in this way I could use also some more widgets from Android world.
Can I do that? If yes, how would I get information back - for instance if I use Radio Button - how I can read selected value ?
Thanks in advance, regards, Bulek.
Current code :
You cannot integrate widgets directly into the layout of your sketch. However, it may be possible to have popup windows that contain radio buttons, similar to the alert dialog above. The use largely depends on how you choose to implement these widgets.
Pop up is fine for my need... I guess it is pretty similar to above example with AlertDialog. But I still don't know how to get data back from widget - for instance if I run radio button in pop up - but how do I get information from widget back (for instance if radio button is checked or not) ?
If there is any example, I'd love to study it ...
Thanks in advance, regards, Bulek.
Again, the solution depends on your implementation. Generally, you have a reference to the UI elements in the popup as you are constructing it - there, you would attach listeners to detect changes. There are many tutorials available, such as the official Android one (Note: You will want to ignore the XML, but the ideas are still the same...).
I got same problem and gave up , thank you for information
Hi,
I'm trying and I think I'm quite near the solution... I'm adding choices to dialog (have found a way to add positive and negative buttons).. Now I get error :
Any advice how to finish that ?
The source code is :
It looks like you want
AlertDialog.Builder
instead of anAlertDialog
- this allows you to specify parameters (non-xml) for the creation of anAlertDialog
. Indeed,setItems()
is anAlertDialog.Builder
function! The Android Developers Dialogs reference has a decent example of how to do this.Hi,
thanks for info. Now I got past that error, but there is still something missing.... Message now doesn't appear, only empty rectangle without any text or single-choice list...
Can you please help me to get this working ?
I haven't tested your code yet, but one thing that stands out is how you mix the
AlertDialog
andAlertDialog.Builder
methods. Typically, you send all of the information to the builder, and then you don't touch the actualAlertDialog
that it gives you other than to show it (see lines 29 and 36 in your code for a comparison). Some of the functions don't necessarily have an exact match, but the API is still very similar. There is more information at the dialogs link I provided above.Hi,
thanks for response and info.
I've successfully created simple OK/Cancel dialog with message without using Builder - only AlertDialog.
And then I've tried to expand then code also for other types of AlertDialogs (single choice, multi-choice, ...), but it seems I have to keep those in separate functions (some are using AlertDialog only, others using AlertDialog.Builder).
Will check this in more details,
thanks.
Bulek.
I am just learning how to use Activities in Processing, Tried using Android Developers page, but the confuse me a lot by using XML files. (L try hard tough). Suggest me some links, if you know where I should start from.
` //Import required classes import android.app.AlertDialog; import android.content.DialogInterface; import android.app.Activity;
Activity act;
AlertDialog alertDialog;
void setup() { act = this.getActivity(); //Create the AlertDialog alertDialog = new AlertDialog.Builder(this).create(); }
//Empty draw()... void draw() {}
void mousePressed() { showpopup(); }
void showpopup() { //Run code on the UI thread... act.runOnUiThread(new Runnable() { // @ Override public void run() { //Create the alert dialog createAlertDialog(); } }); }
//Function for creating alert dialog //We need the separate function so that "this" behaves properly void createAlertDialog() {
//Set the title alertDialog.setTitle("Alert"); //Set the message alertDialog.setMessage("This is an AlertDialog!"); //Add the OK button alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //The OK button has been clicked }}); //Display the AlertDialog alertDialog.show(); } `
The above code belongs to calsign I have just tweaked it a bit. I still get the error
` 1. ERROR in C:\Users\BPAS\AppData\Local\Temp\android5786700555396990168sketch\src\processing\test\notifypopup\NotifyPOPUP.java (at line 35) alertDialog = new AlertDialog.Builder(this).create(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor AlertDialog.Builder(NotifyPOPUP) is undefined
1 problem (1 error)
BUILD FAILED C:\Users\BPAS\AppData\Local\Temp\android5786700555396990168sketch\build.xml:15: The following error occurred while executing this line: C:\Users\BPAS\AppData\Local\Temp\android5786700555396990168sketch\build.xml:28: Compile failed; see the compiler error output for details.
Total time: 1 second `
I tried hard solving it on my own, but I was not able to do it. So brought it to you guys. Please help .
@Aswinth_Raj===
There are 3 errors in your code + 1 problem
problem is that your ok button can do nothing till now, though it seems is doing
Thanks for the guidance, But I only got one error and one problem when I Ran it n my device. I have shown it below '---------- 1. ERROR in C:\Users\BPAS\AppData\Local\Temp\android8028517109403082643sketch\src\processing\test\notifypopup\NotifyPOPUP.java (at line 35) alertDialog = new AlertDialog.Builder(my).create(); ^^
my cannot be resolved to a variable
1 problem (1 error)
BUILD FAILED C:\Users\BPAS\AppData\Local\Temp\android8028517109403082643sketch\build.xml:15: The following error occurred while executing this line: C:\Users\BPAS\AppData\Local\Temp\android8028517109403082643sketch\build.xml:28: Compile failed; see the compiler error output for details.
Total time: 1 second '
Can I get an example program which is working
@Aswinth_Raj===
you get only 1 error because the error is at the very beginning of compiler! and the error is "my cannot be resolved to a variable"... i guess that using my post you have put "my" instead of "this"! but i was thinking (&& refering) to others code example i have given to yo; in these examples i have an "act" variable which is an instance of the Activity class!!! - Look at these examples!