@lve0200 ====
or, using your code (some modifs: creating a runnable)
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.widget.EditText;
import android.os.Looper;
String et = "";
Context ctx;
Activity act;
void setup(){
size(800,600);
orientation(LANDSCAPE);
act = this.getActivity();
ctx = this.getActivity().getApplicationContext();
showAddItemDialog();
Looper.prepare();
}
private void showAddItemDialog() {
this.getActivity().runOnUiThread(new Runnable() {
//@ Override
public void run() {
final EditText taskEditText = new EditText(ctx);
AlertDialog dialog = new AlertDialog.Builder(act)
.setTitle("Altimeter")
.setMessage("Set Altimeter")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
et = String.valueOf(taskEditText.getText());
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
};
void mouseReleased(){
println("Click");
showAddItemDialog();
}
void draw() {
}
doesn’t work, crash after click or already in setup, if the showAddItemDialog statement is in there.
puh,
I’m programming since 1976, nowadays in VisualStudio C,C++, Cross for UNIX, Windows, MS R-open and I tell you, I have never come across something like Android!
here’s my code, now: compiles OK, runtime error attached:
It must be mouseReleased, mousePressed also doesn’t do anything.
Activity act;
Context mC;
FrameLayout fl;
String et;
private void showAddItemDialog() {
this.getActivity().runOnUiThread(new Runnable() {
//@ Override
public void run() {
final EditText taskEditText = new EditText(mC);
AlertDialog dialog = new AlertDialog.Builder(mC)
.setTitle("Altimeter")
.setMessage("Set Altimeter")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
et = String.valueOf(taskEditText.getText());
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
}
void mouseReleased(){
println("Click");
showAddItemDialog();
}
void setup(){ // Partie graphiques fixe
size (1024,768,P2D); // size
orientation(LANDSCAPE);
background(255,0,0);
act = this.getActivity();
mC = this.getActivity().getApplicationContext();
Looper.prepare();
Click
FATAL EXCEPTION: main
Process: processing.test.miniefis, PID: 27376
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:580)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:306)
at processing.test.miniefis.MiniEFIS$1.run(MiniEFIS.java:286)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
OpenGL error 1285 at bot beginDraw(): out of memory
@lve0200 ===
i have tried my code with Samsung tablet 4.4.2: it works; try to copy it without changing nothing, run && tell me
tested also with android 6.0 + Nexus 7: it works
No, it doesn’t! Unless totally blind, I copied your code 100% OK.
I found the explanations, why it doesn’t work, but I am unable to understand it.
Please look here:
https://stackoverflow.com/questions/5796611/dialog-throwing-unable-to-add-window-token-null-is-not-for-an-application-wi
Hence I added this line of code:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
I get a warning:
TYPE_SYSTEM_ALERT is depreciated, but it works now, after I also granted the App permission: SYSTEM_ALERT_WINDOW
´´´
Just the permission is not enough, this new code line is necessary.
Here the complete module:
private void showAddItemDialog() {
this.getActivity().runOnUiThread(new Runnable() {
//@ Override
public void run() {
final EditText QNH_Edit = new EditText(mC);
AlertDialog dialog = new AlertDialog.Builder(mC)
.setTitle("Altimeter")
.setMessage("Set Altimeter")
.setView(QNH_Edit)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
et = String.valueOf(QNH_Edit.getText());
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
});
}
what should I do wrt. the depreciated FLAG?
Testing now on a LG g Pad 7 device w/ Android 5.
@lve0200 === as for the deprecated flag you can use instead TYPE_APPLICATION_OVERLAY
(but requires API 26)
as for the permission it remains (weird!) the same
i see that you are working with 5; that is not a problem but think that after (6.0) permissions have to be required on runtime (Manifest does not work); so you have to write some if else statement getting the build and add methods for getting permissions on runtime if…
yep, I’ll find that out, just help me with this:
How can I keep the screen alive, not going to sleep?
RGDS
hk
@lve0200 ===
another flag that you add to the window:
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
( be prudent with this flag && at least get rid of it when the user leaves: you must clear()it)
Hi,
I don’t really know, how and where to put this. I tried this in setup() and in draw()
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
and got always that:
FATAL EXCEPTION: GLThread 332
Process: processing.test.miniefis, PID: 6614
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
One of my issues is, that 99% of all example in the internet use R.layout.activity_main
@lve0200 ===
that is absolutely normal: the view was created by MainActivity thread and so you cannot modify it without creating a runnable:
import android.view.WindowManager;
void setup(){
size(800,600);
orientation(PORTRAIT);
KeepScreen();
}
private void KeepScreen() {
this.getActivity().runOnUiThread(new Runnable() {
public void run() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}