# Softkey & EditText

**URL:** <https://discourse.processing.org/t/softkey-edittext/11129>\
**Category:** Processing for Android\
**Created:** [May 11, 2019, 1:26pm UTC](https://discourse.processing.org/t/softkey-edittext/11129 "2019-05-11T13:26:28Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 15, 2019, 10:55am UTC](https://discourse.processing.org/t/softkey-edittext/11129/21 "2019-05-15T10:55:29Z")

</div>

@lve0200 ====  
or, using your code (some modifs: creating a runnable)

```auto
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() {

}

```

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 15, 2019, 12:42pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/22 "2019-05-15T12:42:18Z")

</div>

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.

```auto
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();

```

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

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 15, 2019, 2:46pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/23 "2019-05-15T14:46:59Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 15, 2019, 3:16pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/24 "2019-05-15T15:16:50Z")

</div>

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](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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 15, 2019, 5:42pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/25 "2019-05-15T17:42:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 15, 2019, 5:57pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/26 "2019-05-15T17:57:04Z")

</div>

yep, I’ll find that out, just help me with this:  
How can I keep the screen alive, not going to sleep?  
RGDS  
hk

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 16, 2019, 8:18am UTC](https://discourse.processing.org/t/softkey-edittext/11129/27 "2019-05-16T08:18:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 16, 2019, 8:42am UTC](https://discourse.processing.org/t/softkey-edittext/11129/28 "2019-05-16T08:42:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 16, 2019, 8:59am UTC](https://discourse.processing.org/t/softkey-edittext/11129/29 "2019-05-16T08:59:59Z")

</div>

@lve0200 ===  
that is absolutely normal: the view was created by MainActivity thread and so you cannot modify it without creating a runnable:

```auto
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);
      }
  });
}

```

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 16, 2019, 3:48pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/30 "2019-05-16T15:48:15Z")

</div>

Thanks,  
with your beneficial HELP, everything is working to expectations, now.  
I admire your patience, WRT. to Android and all your Forum work!  
I leave you in peace, now( for a little while 😉😉😉  
Best Regards and good luck for your own projects  
hk

[Previous page](https://discourse.processing.org/t/softkey-edittext/11129.md?page=1)
