Softkey & EditText

#1

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

#2

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

#3

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

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

#4

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

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.

#5

TextView and EditText.

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

Kf

#6

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

#7

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

#8

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

#9

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
It also elaborates on EditText.
hk

#10

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

#11

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

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”

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

#12

@akenaton
Reading the doc further, I found

Getting state of application

We can obtain a reference to the main activity or context containing the sketch using the getActivity() and getContext() functions:

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.

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

#13

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

#14

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.
#15

Found that number keyb.
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
still stuck on the edit screen
hk

#16

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

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

#17

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

#18

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

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

#19

Try mousePressed() instead.

Kf

#20
@lve0200 ====
as for an alertdialog:


``

`

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

}


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();
    
}
}

);

}

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

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

String getMessage(){
  
  return alertMessage;
}