How to manually create Android TextField in Processing ?

edited May 2016 in Android Mode

Hi everyone, I'm on a project on Android where I have to input some text in Japanese (with google IME keyboard as an example), and as you probably know, I looked a lot for it on forums, and neither controlP5 TextField nor APwidgets work on Processing 3.x... So I was wondering if there was any possibility of creating a "normal" Java TextField in Processing which could normally get IME keyboard characters (I'm not Java skilled enough for that so I hope you'll be able to help me^^) Thanks forward !

Answers

  • @olivi55=== android has classes (widgets) for that :: textView && EditTextView, with a lot of parameters (scrolling, multilines etc.)...

  • @akenaton thx I think I'll use that !...but as an example, how could I set this textView or EditTextView location in processing ? And since I'm currently using Ketai library to get the keyboard out, how do they manage together ? (I'm really bad for raw java :/ ) Thx forward !

  • edited May 2016

    @olivi55===

    not any idea about ketai (i prefer to work with native android); as for the textView see the code below. Of course you can set a lot of other parameters (margin, background color and so on).

            import android.app.Activity;
            import android.os.Bundle;
            import android.os.Environment;
            import android.view.View;
            import android.widget.TextView;
            import android.view.ViewGroup;
            import   android.graphics.Color;
            import android.widget.FrameLayout;
            import android.view.ViewParent;
            import android.widget.RelativeLayout;
    
    
    
            TextView tv;
            Activity act;
            FrameLayout fl;
    
    
            public void setup(){
              size(600,900);
              orientation(PORTRAIT);
             background(255,0,0);
    
              tv = new TextView(this.getActivity());
              tv.setTextSize(24);
              tv.setTextColor(Color.rgb(20,0,0));
              tv.setText ("How to set an android textfield very basic");
              tv.setX(50);
              tv.setY(400);
              tv.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,
                                               RelativeLayout.LayoutParams.WRAP_CONTENT));
              act = this.getActivity();
    
              fl = (FrameLayout)act.findViewById(0x1000);
    
            }
    
    
            public void draw(){
    
    
            act.runOnUiThread(new Runnable() {
    
            public void run() {
    
    
    
            if(tv.getParent()!=null){
    
                   ((ViewGroup)tv.getParent()).removeView(tv); 
            ; 
    
            }
            fl.addView(tv);
    
    
            }
    
            });
    
            } 
    
  • @akenaton ...wow thx...i'll try to add thi into my project monday...I'll keep you informed ^^

  • @akenaton Hi again, I just tried your program...It works well as TextView but when I want to put an EditText, when I tap on it, it loses focus instantly...I found this but since I don't use xml I don't know how to manage it in processing :/

  • @olivi55=== not sure to understand what you mean.Put your code. Normally when you touch the edit text it opens the soft keyboard input and you can use it. The way for displaying the keyboard can also be customized by code with parameters (SOFT_INPUT_STATE_ALWAYS_VISIBLE and so on)

  • @akenaton I just tried your code too. It launches on my phone but nothing happens in response to screen touches... ?

  • @hudson_m4000=== nothing happens because this is a static textView if you want more (keyboard and so on) you must change the textview for an edit text...

  • @akenaton @hudson_m4000 in fact the program runs, text is here, but when I touch on the EditText, it takes focus, but just after the focus is lost...as it looks it seems the focus is somehow reset in the next run of draw( )...

  • @olivi55===put the code you are using (mine one transformed with edit ext instance instead of textview)

  • edited May 2016

    @akenaton

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.EditText;
    import android.view.ViewGroup;
    import android.graphics.Color;
    import android.widget.FrameLayout;
    import android.view.ViewParent;
    import android.widget.RelativeLayout;
    
    
    
    EditText et;
    Activity act;
    FrameLayout fl;
    
    
    public void setup(){
      fullScreen();//size(600,900);
      orientation(PORTRAIT);
     background(0,50,120);
    
      et = new EditText(this.getActivity());
      et.setTextSize(24);
      et.setTextColor(Color.rgb(20,0,0));
      et.setText ("How to set an android textfield very basic");
      et.setX(50);
      et.setY(400);
      et.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,
                                       RelativeLayout.LayoutParams.WRAP_CONTENT));
      act = this.getActivity();
    
      fl = (FrameLayout)act.findViewById(0x1000);
    
    }
    
    
    public void draw(){
    
    
    act.runOnUiThread(new Runnable() {
    
    public void run() {
    
    
    
    if(et.getParent()!=null){
    
           ((ViewGroup)et.getParent()).removeView(et); 
    ; 
    
    }
    fl.addView(et);
    
    
    }
    
    });
    
    } 
    

    I just took your code and replaced the TextView as an EditText

  • @akenaton ok ... I will fiddle around a bit more ! :)

  • edited May 2016 Answer ✓

    @olivi55== no, it cannot work like this : 1) the parameters for editText and textView are of the same kind but not exactly the same! 2) as you add the textView on the runnable it takes/looses the focus and the keyboard cannot be shown...

    simple snippet code::

            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.widget.Toast;
             import android.os.Looper;
             import android.view.WindowManager;
            import android.view.inputmethod.InputMethodManager;
            import android.os.Bundle;
    
    
             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);
    
    
              //Toast.makeText(//if you want tio call the user to write...
              //          mC,
              //          "entrez un texte dans le champ ci-dessus",
              //          Toast.LENGTH_LONG).show();
    
                }
    
             void setup(){
               size(600,900);
               orientation(LANDSCAPE);
               background(255,0,0);
    
               Looper.prepare();
    
             }
    
             void draw(){
    
    
    
    
               String txt = edit.getText().toString();//i you want to save it or transform it
               println(txt);
    
             }
    
             public void onResume(){
    
               super.onResume();
               //show the softinput keyboard at start
               act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
             }
    
  • ok thx ! I'll try to manage it with that... Thx a lot @akenaton for your patience ^^

  • @akenaton ... sorry something again :/...I managed to change the size of the EditText, but I can't manage to move it..I tried with setMargins, but I get a compiling error :/

  • Answer ✓

    @olivi55===you have to understand what happens (hidden by P5!)

    • parent layout is a fame layout, full screen
    • you add an edit text to it as a child
    • you can set its width & height
    • by default it is in the top left corner of its parent

    So: in order to move it there are different solutions you can try: - most simple:: you add white spaces to the text you enter with setText():: a workaround!!!! - other: you change the params of the parent (fl) adding padding to it (left, top, right, bottom) as integer - other (you have created a relativeLayout for the edit text) so you can add rule to it, which allows you to put the dit text at center, left, right bottom alignment from its parent

    try all of them and choose...Remember also that with the getText method you can display the entered text in a classical P5 way text("", int, int); and just before remove the edit text!!!

  • @akenaton thx again, I'll go on padding fl then !

  • edited May 2016

    @akenaton ...still problems -_- sorry :/ ...I can't manage to add a marging to the relative layout :/...isn't there another way to set an exact pixel position of the EditText relatively to its parent ?

  • @akenaton Line 19 of you latest sample code working with editText, is that valid Processing code? I am getting an error message:

    <a href="/two/profile/Override">@Override</a>

    Also for anybody running that example, you also need to include:

    import android.widget.EditText;

    Kf

  • edited May 2016

    @kfrager===

    this is only a typo when you cut/copy here you have to write only:: @Override, nothing else (erase the other chars)

  • @akenaton, when posting code w/ annotations, add 1 space between the @ & the annotation in order to work around the forum's code glitch. *-:)

  • @GoToLoop====

    ok && excuse me--- i ll try to remember! :)]

  • @akenaton hi again ! I'm trying to place my EditText by adding margins but it doesn't change anything :/...any idea on why it doesn't work ? thx forward ! RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.setMargins(300,300,300,300); edit.setLayoutParams(params);

  • Answer ✓

    Hi @ olivi55 When you add margins, you mean that are you trying to place your editText in your canvas?

    Would this work for you:

    edit.setX(100);
    edit.setY(300);
    

    Kf

  • @kfrajer OMG thx finally why didn't I try this before thx a lot X)

  • @akenaton @kfrajer Hi ! me again, and I hope this time is the last time...in my app I have need to make my EditText .setVisibility(View.GONE) and able to get it back .setVisibility(View.VISIBLE) after...but when I try it I get this error : android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. ...How can I modify this EditText visibility (or just disable it and reenable it after) easily anywhere in my program ? Thx forward again ! ^:)^

  • Answer ✓

    @olivi55=== i have not time enough to explain exactly (code) what to do but i think (without seeing what you have written!!!) the solution is to make a runnable and call there .setVisibility()....Android is very threadSensitive!

  • @akenaton : just perfect thx ! I took back your Runnable syntax from one of your previous posts !...thank you a lot...now I'll now what to do for this error ! It works perfectly now !

Sign In or Register to comment.