1
\$\begingroup\$

I'm writing simple chat-app using Unity. The problem with Unity's default implementation of soft-keyboard visibility is that it hides itself when i'm clicking outside of keyboard panel. What i want is that keyboard stays always shown until i explicitly tells otherwise.

For a couple of days i tried to find similar questions but none of them helped me. I have very little experience in "native" android development (i.e. Java). So far i managed to create simple native plugin for Unity. I tried to manually open keyboard with this code:

imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

The result was fine, except the keyboard had InputType set to TYPE_CLASS_NUMBER. For my app i needed TYPE_CLASS_TEXT. So i tried to research how to set InputType and stumbled on fact that this property can only be changed inside EditText object. As i'm writing my app in Unity, i had no way of finding my InputField's inside native code. So i tried to create EditText inside my native plugin. I followed the logic that if i create custom EditText, place it inside main layout and set visibility to NONE or make background transparent, then i can set focus to that element and be fine:

FrameLayout myLayout = activity.findViewById(android.R.id.content);
focusedText = new EditText(context);
focusedText.setFocusable(true);
focusedText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
myLayout.addView(focusedText);
focusedText.setFocusableInTouchMode(true);
focusedText.requestFocus();
focusedText.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
imm.showSoftInput(focusedText, InputMethodManager.SHOW_FORCED);

The end result was a mess. Soft keyboard behaviour became undesirable. In order for keyboard to show, i first needed to select InputField, then i would see transparent EditText be created in the middle of my layout with all input characters visible. Even if all worked fine, it still looks like a wacky hack.

Does anyone stumbled across similar issues?

\$\endgroup\$
1
\$\begingroup\$

Forcing the keyboard to stay open may not be the best method as on some mobiles the keys will take up a good portion of the available screen space. If a user cannot un-focus the board it could lead to a bad experience for them.

With that suggestion out of the way you do not have to write a custom plugin to achieve the behavior you want. You can set up a boolean to store when you are completed with the keyboard(enter key or some other means). That way you can keep it in Focus until you are done by utilizing the scenes EventSystem.

using UnityEngine.EventSystems;
using UnityEngine.UI;
public class YourClass : MonoBehaviour

public InputField inputField;
bool completedInput = false;

For example you can set the focus using

 EventSystem.current.SetSelectedGameObject(inputField.gameObject, null);

you can auto open the keyboard if needed with

inputField.ActivateInputField();

in combination with a completed boolean you could keep the board open by resetting the focus to the input if someone clicked out of the element or the inputfield is unfocused by other means before they are done.

 if(!completedInput)
        EventSystem.current.SetSelectedGameObject(inputField.gameObject, null);

another way that may prove useful is to check if you are not over a EventSystem object then either null their new selection or something along those lines.

if (!EventSystem.current.IsPointerOverGameObject())
    //do something 

note that you can assign various keyboard layouts in codebehind or in the properties of a input field in the inspector.

inputField.contentType = InputField.ContentType.Autocorrected;

InputField Inspector View

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ First of all, thank you for commenting on this topic. Unfortunately i haven't managed to solve my problem with Unity scripts. The reason i had to write my custom native logic is that Unity doesnt handle soft-keyboard visibility logic. \$\endgroup\$ – eXiz7 Jul 16 '19 at 20:02
  • \$\begingroup\$ I'll make a case: I created empty project with just one base canvas, one InputField and EventSystem object. I wrote a little script - whenever InputField is focused, i disable diselection event of InputField and destroy EventSystem object, so that no event can be emited. And still, whenever i click outside of InputField the keyboard is closing. I have not found any trace of manual keyboard closing from Unity scripts. \$\endgroup\$ – eXiz7 Jul 16 '19 at 20:02
  • \$\begingroup\$ Your suggestion does not suit my case. Sure, i can reopen soft-keyboard after it being closed. But this does not look nice on user-end (keyboard flickering). If you actualy managed to get it work fine and i'm missing simething, please tell me. \$\endgroup\$ – eXiz7 Jul 16 '19 at 20:02
  • \$\begingroup\$ Even if i write "Update(){EventSystem.SetSelectedGameObject(InputField.gameObject); }" all it does is reselecting InputField, meanwhile keyboard is already gone. Try it yourself and see what you got. \$\endgroup\$ – eXiz7 Jul 16 '19 at 20:16
  • \$\begingroup\$ Justin, i really appreciate your help. But please, at least try it yourself before posting a suggestion. The brick wall i stumble in Unity, is that there is no way to prevent soft-keyboard from closing. By the time you know about it, the keyboard is already begun to hide. \$\endgroup\$ – eXiz7 Jul 16 '19 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.