# 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:** 20\
**Page:** 1

<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 11, 2019, 1:26pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/1 "2019-05-11T13:26:28Z")

</div>

Hi,  
sorry for the “idiots guide to the universe” type of question, just grant me the newbie status…  
I want to create a softkey and upon mouse click on it a 5 digit editable number field/widget?) should allow me to enter a number and assign it to a variable and disappear. Most likely using the EditText class.  
I found this, which would do it at a 1st glance…  
[https://code.google.com/archive/p/apwidgets/](https://code.google.com/archive/p/apwidgets/)  
… but its too old and rejected by the verifier.  
I found a lot of XML examples for AndroidStudio and activity files(??) , but nothing in a pure processing IDE.  
Could someone help me out, please?  
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 11, 2019, 2:11pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/2 "2019-05-11T14:11:22Z")

</div>

@lve0200 ===

I think i have already put code (on this forum or on the previous one) for an edittext in P5 ide; yet the principle is exactly the same than for a button or every android widget: you get the root view (frame Layout) created by P5, create a runnable and inside it add your edit text.

---

<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 11, 2019, 2:29pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/3 "2019-05-11T14:29:19Z")

</div>

Hi,  
do you have a link?  
That’s too abstract 4me.  
I have some simple code, but it does not compile 😟

```auto
import android.R;
import android.widget.EditText;
EditText editText1 = (EditText) findViewById(R.id.editText1);
and the xml in the data dir:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
  
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="numberDecimal" />
</RelativeLayout>

```

Error: The global variable “editText1” does not exist

---

<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 11, 2019, 4:01pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/4 "2019-05-11T16:01:28Z")

</div>

@lve0200 ===  
no, that is not the right way with P5which ignores what is android.R && of course your code cannot run.You cannot “find” your editText which does not exist in xml (as it exists with AS); you have to create it by code and add it to the root view. Hint:

```auto
Context mC;
Activity act;
@Override 
    public void onStart() {
        super.onStart();
         
        act = this.getActivity();
        mC= act.getApplicationContext();
(editText) edit = new EditText(mC);

//----------then you give it a layout
//---------then you add it to the rootView (frameLayout) (create a runnable for that)

}

```

as for the link sorry but i posted a snippet on this or previous forum and you can search for it.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [May 12, 2019, 2:03am UTC](https://discourse.processing.org/t/softkey-edittext/11129/5 "2019-05-12T02:03:37Z")

</div>

[TextView](https://forum.processing.org/two/discussion/comment/35865/#Comment_35865) and [EditText](https://forum.processing.org/two/discussion/comment/69600/#Comment_69600).

It is possible to use xml but I am not sure if it is documented for Processing Android.

Kf

---

<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 13, 2019, 10:22am UTC](https://discourse.processing.org/t/softkey-edittext/11129/6 "2019-05-13T10:22:58Z")

</div>

@kfrajer == That is interesting yet how is it possible ??? Where do you put your xml?Data?Assets? How can you add them to the fragment without creating them by code??? - of course that is supposing that you are using only P5; using AS+P5 is #…

---

<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 13, 2019, 10:44am UTC](https://discourse.processing.org/t/softkey-edittext/11129/7 "2019-05-13T10:44:15Z")

</div>

Dears,  
please allow me to remind you, that I am a bloody beginner. I meanwhile installed AS and I got the processing tutorial to run, but then I got stuck in the complexity of Android XML UI and the fact that “Navigation”, which I probably would need to swipe between screens doesn’t work, due to a bug of contradicting libs in AS( the internet is full of this bug reports).  
Hence, I still prefer, solving my requirement (see 1st post) in processing.  
EditText is a standard functionality. Be it w/ XML, or in pure code.  
Could someone be so kind, publishing a small code example, how to? Just a “Hello world” screen and an EditText popup in processing??, PLEASE!  
Thanks!!!  
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 13, 2019, 11:01am UTC](https://discourse.processing.org/t/softkey-edittext/11129/8 "2019-05-13T11:01:57Z")

</div>

@lve0200 === if you are a beginner leave AS and start with P5; as for an edit text i have already posted: you have to create it by code, which means you have to create (onstart()) by code the editext object, its params, its layout, then add it to the root view= that is the guide line then i dont see any interest (for you!) to put here a complete working code, ready to cut/copy: try and i ll help.

---

<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 13, 2019, 11:38am UTC](https://discourse.processing.org/t/softkey-edittext/11129/9 "2019-05-13T11:38:01Z")

</div>

OK Teacher!  
found your old post. Will bite the bullet.  
[https://forum.processing.org/two/discussion/16539/how-to-manually-create-android-textfield-in-processing](https://forum.processing.org/two/discussion/16539/how-to-manually-create-android-textfield-in-processing)  
It also elaborates on EditText.  
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 13, 2019, 12:45pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/10 "2019-05-13T12:45:17Z")

</div>

@Ive0200 ===  
not teacher; android developer by job… and artist…

---

<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 13, 2019, 1:19pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/11 "2019-05-13T13:19:19Z")

</div>

OK, sorry to bother, I appreciate your time!  
I created a tab in the program I’m porting to Android with this:

```auto
import android.app.Activity;
 import android.content.Context;
 import android.widget.FrameLayout;
 import android.widget.RelativeLayout;
 import android.text.Editable;
import android.graphics.Color;
 import android.os.Looper;
 import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;
import android.widget.EditText; 
 
 EditText edit;
 Activity act;
 Context mC;
 FrameLayout fl;
 
 @Override
    public void onStart() {
        super.onStart();
 
        act = this.getActivity();
        mC= act.getApplicationContext();
        //show or hide the keyboard at start
         act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        edit = new EditText(mC);
        edit.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,
                                   RelativeLayout.LayoutParams.MATCH_PARENT
                                   ));
                                   edit.setHint(" YOU CAN WRITE!");
 
            edit.setTextColor(Color.rgb(200,0,0));
            edit.setBackgroundColor(Color.WHITE);
            edit.requestFocus();
 
 
  fl = (FrameLayout)act.findViewById(0x1000);
  fl.addView(edit);
    }

```

This is the related part in my “main”

```auto
public void onResume(){
 
   super.onResume();
   //show the softinput keyboard at start
   act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 }

void setup(){
size (1280,800,P2D); // size 
orientation(LANDSCAPE);
background(255,0,0);
Looper.prepare();
 some more not related statements init my var etc. }

```

I have nothing in draw(), yet.  
Code compiles w/out errors.  
At runtime I get:  
FATAL EXCEPTION: main  
Process: processing.test.miniefis, PID: 14815  
java.lang.RuntimeException: Unable to start activity ComponentInfo{processing.test.miniefis/processing.test.miniefis.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.FrameLayout.addView(android.view.View)’ on a null object reference  
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)  
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  
at android.app.ActivityThread.-wrap11(Unknown Source:0)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  
at android.os.Handler.dispatchMessage(Handler.java:105)  
at android.os.Looper.loop(Looper.java:164)  
at android.app.ActivityThread.main(ActivityThread.java:6541)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.FrameLayout.addView(android.view.View)’ on a null object reference  
at processing.test.miniefis.MiniEFIS.onStart(MiniEFIS.java:589)  
and a lot more…  
Rgds  
hk  
PS: 2b more clear,  
of course I have a full fledged draw() routine(see screen shot), but there’s nothing yet in for EditText. What I actually want to achieve is editing the small 1013 in the lower right part.  
RGDS

 ![efis](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c660dab0f8c7a4290fc127b6101a15b4db94e377.jpeg)

---

<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 13, 2019, 5:06pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/12 "2019-05-13T17:06:28Z")

</div>

@akenaton  
Reading the doc further, I found

## Getting state of application

We can obtain a reference to the main [activity](https://developer.android.com/reference/android/app/Activity) or [context](https://developer.android.com/reference/android/content/Context) containing the sketch using the getActivity() and getContext() functions:

```auto
import android.app.Activity;
import android.content.Context;

void setup() {
  Activity activity = getActivity();
}
                

```

The activity will be available only for sketches run as regular or VR apps, since wallpapers and watch faces are services that are not associated to an activity. getContext() should return the context of the application or service in any situation.

```auto
import android.content.Context;

void setup() {
  Context context = getContext();
}
                

```

The difference to your code example is that gertContext and getActivity are in the setup func and not in OnStart ?!  
Could that be the problem??  
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 14, 2019, 8:08am UTC](https://discourse.processing.org/t/softkey-edittext/11129/13 "2019-05-14T08:08:06Z")

</div>

@lve0200 ====  
the error is not what you say : it s about the frameLayou created by P5 that i get with an hardcoded number (it was right 2 years ago) but as for now (where that is a generated number on run time) you have to change this part of the code writing  
`fl = (FrameLayout)act.getWindow().getDecorView().getRootView();`

---

<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 14, 2019, 2:21pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/14 "2019-05-14T14:21:48Z")

</div>

Thank you, its sort of working now, but I still got 2 hickups:

1. The edit screen always stays on, if I press DONE, or return, I never see my graphics screen. I tried to change it like this  
//show or hide the keyboard at start  
act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\_INPUT\_STATE\_ALWAYS\_HIDDEN);  
unfortunately with no effect. In the best of all possible worlds, the EditText input should appear at the beginning, disappear after I entered a 4 digit number and reappear on request, when getText is executed. In my current SW there is no getText, yet.
2. I tried to change the keyboard to a num/phone pad like this:  
edit.setInputType(InputType.TYPE\_NUMBER\_VARIATION\_NORMAL);  
The effect is, that I have no keyboard at all.

---

<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 14, 2019, 2:44pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/15 "2019-05-14T14:44:59Z")

</div>

Found that number keyb.  
edit.setInputType(InputType.TYPE\_CLASS\_NUMBER);  
still stuck on the edit screen  
hk

---

<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 14, 2019, 8:28pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/16 "2019-05-14T20:28:06Z")

</div>

Hi, me again,  
I got a little bit further on, but I’m still confused!  
I changed the code as following:

```auto
public void onStart() {
super.onStart();
act = this.getActivity();
mC= act.getApplicationContext();
        //show or hide the keyboard at start
        act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        edit = new EditText(mC);
        edit.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
                                   RelativeLayout.LayoutParams.WRAP_CONTENT));
        edit.setTextColor(Color.rgb(200,0,0));
        edit.setBackgroundColor(Color.WHITE);
        edit.setHint("Enter Altimeter Setting");
// edit.setText("1013");
        edit.setInputType(InputType.TYPE_CLASS_NUMBER);
        edit.requestFocus();
        
  fl = (FrameLayout)act.getWindow().getDecorView().getRootView();
  fl.addView(edit);

```

That gives me an initial screen “Enter Altimeter” and clicking done, I see a part of my drawing, but for God sake, I cannot get rid of the white sub-window (1013). I guess it is all about focus, but then I have already my next question, if I manage to “unfocus” the editText window, how can I eventually get it back? On Start/resume is on Start and not “When needed”??  
Sorry, questions over questions…  
Good night  
hk

 ![Screenshot_1557865297](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7a7cff17add593b78289c9965b82e953b8d58c30.png) ![Screenshot_1557865287](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b56c4585eed904cfcac1d1dde33e6bd3249afe7a.png)

---

<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 14, 2019, 9:13pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/17 "2019-05-14T21:13:41Z")

</div>

I think, what I’m really after is a Dialog.

---

<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 14, 2019, 10:03pm UTC](https://discourse.processing.org/t/softkey-edittext/11129/18 "2019-05-14T22:03:29Z")

</div>

Sorry, if I keep talking to myself, and changing subjects.  
I tried to move to a dialog. I think I’ m almost there, but of course, I ran into another problem.  
This is my dialog code. compiles OK

```auto
private void showAddItemDialog(Context c) {
    final EditText taskEditText = new EditText(c);
    AlertDialog dialog = new AlertDialog.Builder(c)
        .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 mouseClicked(){
  println("Click");
  showAddItemDialog(this.getActivity());
}

void draw() {
some code here

```

The problem is, it never “clicks” apparently the mouseClicked function doesn’t work and I don’t know, if I missed out on something, or if it is simply related to the emulator. I have certainly effects there, because my location service (getting GPS data) does also not run on the emulator, but runs on a real device.  
RGDS  
hk

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [May 15, 2019, 6:56am UTC](https://discourse.processing.org/t/softkey-edittext/11129/19 "2019-05-15T06:56:30Z")

</div>

Try `mousePressed()` instead.

Kf

---

<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:24am UTC](https://discourse.processing.org/t/softkey-edittext/11129/20 "2019-05-15T10:24:23Z")

</div>

```auto
@lve0200 ====
as for an alertdialog:

``

```

`

```auto
String alertMessage = "";
 import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Looper;
import android.app.Activity;
import android.content.DialogInterface;
Activity act;

void setup() {
  size(1024,768);
  orientation(LANDSCAPE);
  act=this.getActivity();

```

Looper.prepare();

}

```auto

void draw(){
 
}
 
void createAlert() {
  act.runOnUiThread(new Runnable() {
    //@ Override
      public void run() {
       
      AlertDialog.Builder builder = new AlertDialog.Builder(act);
      AlertDialog alertDialog = builder.create();
      alertDialog.setTitle("Altimeter");
      alertDialog.setMessage(getMessage());
      alertDialog.setButton("add", new DialogInterface.OnClickListener() { 
  public void onClick(DialogInterface dialog, int which) {
  
 
}}); 
      alertDialog.show();
    
}
}

```

);

```auto
}

 void mousePressed(){
    setMessage("set altimeter");
    createAlert();
  }

void setMessage(String mess){
  
  alertMessage = mess;
}

String getMessage(){
  
  return alertMessage;
}

```

```auto

```

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