We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I needed to display alerts in processing android so I got some code from another post and boiled it down to this
import android.app.AlertDialog;
String alertMessage;
void setup() {
alertMessage = "This is the message";
createAlert();
}
void createAlert() {
runOnUiThread(new Runnable() {
@ Override
public void run() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Alert");
alertDialog.setMessage(alertMessage);
alertDialog.setPositiveButton("OK", null);
alertDialog.show();
}
}
);
}
Which works great but now to display some text in an alert I have to change the alertMessage and then call the function, there must be a way to turn it into a function that takes in a string, something like this createAlert("This is the message");
but I cant figure out how to do it with the UiThread being inside the function.
Any advice?
Answers
Something like what ketai does: http://ketai.org/reference/ui/ketaialertdialog/
You can always explore their source code. On the side, possible relevant code, specially if you are looking for the missing imports:
https://forum.processing.org/two/discussion/3715/i-d-like-to-have-simple-alertdialog-in-my-processing-android-sketch
which are (some of them redundant):
Kf
@schotsl===
before trying to answer your question, are you sure that the code you have put can run?
@akenaton @kfrajer Im sorry I forgot to add the import, the only import you need for this code to run is
import android.app.AlertDialog;
. Then the code will runCode runs... confirmed!
Kf
@kfrajer===
sorry; as for me line 12 fires: The function "runOnUiThread(Runnable(){})" does not exist s" ;and in order to get rid of it i must add act.run..... (act = Activity getActivity) which seems right as you 're in a fragment.
@shotsl=== not sure to understand your question; yet have a look to the code below which runs well for me:
@akenaton It's weird that it works for me and @kfrajer but not for you right? Thanks for the improvement anyway :P
My question was If I could turn createAlert into a function that requests a string,
createAlert("This is the message");
instead of (what im currently doing):Altough now I release I could just create a function with something like this
This does feel a bit dodgy though..