Search code examples
keyboardtextfieldcodenameone

Codenameone - TextField and Keyboard issue


My problem is that, in android, when the user toggles from a text field to another by touching, the virtual keyboard disappears and reappears each time. How can I ensure that the keyboard is always visible, when the user touches another text field? I tried this with a graddle project (old template) and a maven project template using Android SDK's simulator and a real Android phone. All the same. I used a Y-scrollable box layout as stated in other posts. But nothing has worked.

I'm providing my code samples below.

Old Graddle project:

public class MyApplication {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);
        theme = UIManager.initFirstTheme("/theme");
        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);
        // Pro only feature
        Log.bindCrashProtection(true);
        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        
        Form hi = new Form("Hi World", new BoxLayout(BoxLayout.Y_AXIS));
        hi.getContentPane().setScrollableY(true);
        hi.setScrollableY(true);
        System.out.println(hi.isScrollableY());

        hi.add(new TextField("","1"));
        hi.add(new TextField("","2"));
        hi.add(new TextField("","3"));
        hi.add(new TextField("","4"));
        hi.add(new TextField("","5"));
        hi.add(new TextField("","6"));
        hi.add(new TextField("","7"));
        hi.add(new TextField("","8"));
        hi.add(new TextField("","9"));
        hi.add(new TextField("","10"));
        hi.add(new TextField("","11"));
        hi.add(new TextField("","12"));
        hi.add(new TextField("","13"));
        hi.add(new TextField("","14"));
        hi.add(new TextField("","15"));
        hi.add(new TextField("","16"));
        hi.add(new TextField("","17"));
        hi.add(new TextField("","18"));
        hi.add(new TextField("","19"));
        hi.add(new TextField("","20"));
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}

New Maven project:

public class MyApp extends Lifecycle {
    @Override
    public void runApp() {
        Form hi = new Form("Hi World", BoxLayout.y());
        Button helloButton = new Button("Hello World");
        hi.add(helloButton);
        TextField tf1 = new TextField("","text1");
        TextField tf2 = new TextField("","text2");
        TextField tf3 = new TextField("","text3");
        TextField tf4 = new TextField("","text4");
        hi.add(tf1);
        hi.add(tf2);
        hi.add(tf3);
        hi.add(tf4);
        helloButton.addActionListener(e -> hello());
        hi.getToolbar().addMaterialCommandToSideMenu("Hello Command",
        FontImage.MATERIAL_CHECK, 4, e -> hello());
        hi.show();
    }

    private void hello() {
        Dialog.show("Hello Codename One", "Welcome to Codename One", "OK", null);
    }

}


Solution

  • If you press next the keyboard should stay open, it should also stay open if you scroll. Unfortunately, when you click a new text field through touch we need to commit the previous text field before doing anything else which is why this is happening.

    It might be possible to create an improvement for this special case but it isn't trivial since the code runs on two separate threads (Android Native UI thread and Codename One EDT).