Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android TouchScreenKeyboard not always returning value to InputField

Discussion in 'UGUI & TextMesh Pro' started by Cromfeli, Apr 7, 2015.

  1. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    Every now and then when I type name on the mobile keyboard and click "Done" on the native keyboard, the string remains empty. And this seems to be completely random. On some devices it never fails, and in some other devices it fails 90% of the time. Am I doing something obviously wrong?

    Code (CSharp):
    1.     public void summonKeyboard()
    2.     {
    3.         keyboard = TouchScreenKeyboard.Open ("", TouchScreenKeyboardType.NamePhonePad);
    4.  
    5.         if (keyboard != null)
    6.         {
    7.             //firstNameString = keyboard.text;
    8.             StartCoroutine(getFirstName());
    9.         }
    10.  
    11.     }
    12.  
    13.     IEnumerator getFirstName()
    14.     {
    15.         while (!keyboard.done)
    16.         {
    17.             yield return null;
    18.         }
    19.         firstNameString = keyboard.text;
    20.     }
     
  2. MrEsquire

    MrEsquire

    Joined:
    Nov 5, 2013
    Posts:
    2,712
    What version Unity you using, this is very important
     
    Cromfeli likes this.
  3. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    Version 5.0.1f1

    Edit: Actually I am able to reproduce this bug with only 1 line of code. So I made bug report.

    Attached repro project. For example Samsung S4 Active has the problem, where Samsung Galaxy Tab 4 works always.
     

    Attached Files:

    Last edited: Apr 7, 2015
  4. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
  5. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
  6. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    I just received answer to the bug report that they have been able to reproduce the problem and it has been forwarded to developers. So some hope for solution.

    Meanwhile here is workaround example attached: It shows red color when ever the error was fixed with the workaround. This is compatible with touchscreen and desktop both. So just change the text colors to black for example so that user only see end result no matter what.

    Unity 5.0.1f1
     

    Attached Files:

    Last edited: Apr 10, 2015
  7. comworm

    comworm

    Joined:
    Aug 13, 2014
    Posts:
    11
    I hope following code helps you guys before unity team fix the problem.
    I successfully tested on my own probject.

    public class CustomInputField : InputField
    {
    private TouchScreenKeyboard _openedKeyboard;
    public void OnEndEdit()
    {
    if (TouchScreenKeyboard.isSupported == true)
    {
    text = _openedKeyboard.text;
    }
    }
    public void OnValueChange()
    {
    if (TouchScreenKeyboard.isSupported == true)
    {
    _openedKeyboard = m_Keyboard;
    }
    }
    }
     
  8. locus84

    locus84

    Joined:
    Mar 30, 2011
    Posts:
    119
    This is my solution for this issue
    Works perfectly for me.

    public void OnChangeValue()
    {
    if(!string.IsNullOrEmpty(inputField.text))
    backUpString = inputField.text;
    lastBackupStringTime = Time.realtimeSinceStartup;
    }

    public void OnSubmitMessage()
    {
    //TODO: remove when unity bug is resolved
    if(string.IsNullOrEmpty(inputField.text) && Time.realtimeSinceStartup - lastBackupStringTime < 0.05f)
    {
    Debug.Log("Value Restored : " + backUpString);
    inputField.text = backUpString;
    }

    if(string.IsNullOrEmpty(inputField.text)) return;

    Debug.Log(inputField.text);
    }
     
  9. Spoke44

    Spoke44

    Joined:
    Feb 16, 2015
    Posts:
    11
    Hello, same problem here on 5.0.1f1

    My fix, inspired by others, just drop this script on your InputField :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. // TODO: remove this component when unity bug will be resolved
    5. [RequireComponent(typeof(InputField))]
    6. public class InputFieldFix : MonoBehaviour {
    7.     private string backUpString = "";
    8.     private InputField input;
    9.  
    10.     void Start (){
    11.         input = GetComponent<InputField> ();
    12.         input.onValueChange.AddListener (OnChangeValue);
    13.     }
    14.  
    15.     void OnDestroy(){
    16.         input.onValueChange.RemoveAllListeners ();
    17.     }
    18.  
    19.     void OnChangeValue (string value){
    20.         if (string.IsNullOrEmpty (value) && backUpString.Length > 1) {
    21.             input.text = backUpString;
    22.             return;
    23.         }
    24.  
    25.         backUpString = value;
    26.     }
    27. }
    Just, not working if you just leave a single char in the input. I don't care about this detail, but just to let you know ;)
     
  10. johnagillett

    johnagillett

    Joined:
    Oct 20, 2012
    Posts:
    31
    This seems to prevent the deleting of all text in the field, but when you have multiple input fields they still seem to be static. For instance I have a menu with two of them and when you change one it changes the value (not the actual value in the text field, but the variable it is sending to) for both of them.
     
  11. Webbrowser

    Webbrowser

    Joined:
    May 28, 2015
    Posts:
    1
    I modified the script to allow for single characters to remain in the inputfield.

    Code (CSharp):
    1.  
    2. // http://forum.unity3d.com/threads/android-touchscreenkeyboard-not-always-returning-value-to-inputfield.317047/
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Collections;
    6.  
    7. // TODO: remove this component when unity bug will be resolved
    8. [RequireComponent(typeof(InputField))]
    9. public class InputFieldFix : MonoBehaviour
    10. {
    11.     private string backUpString = "";
    12.     private InputField input;
    13.     private bool isFinishingEditing;
    14.  
    15.     void Start()
    16.     {
    17.         isFinishingEditing = true;
    18.         OnChangeValue(" ");
    19.  
    20.         input = GetComponent<InputField>();
    21.         input.onValueChange.AddListener(OnChangeValue);
    22.         input.onEndEdit.AddListener(OnEndEdit);
    23.     }
    24.  
    25.     void OnDestroy()
    26.     {
    27.         input.onValueChange.RemoveAllListeners();
    28.         input.onEndEdit.RemoveAllListeners();
    29.     }
    30.  
    31.     void OnChangeValue(string value)
    32.     {
    33.         if (string.IsNullOrEmpty(value) && isFinishingEditing == true)
    34.         {
    35.             input.text = backUpString;
    36.             isFinishingEditing = false;
    37.         }
    38.         backUpString = value;
    39.     }
    40.  
    41.     void OnEndEdit(string value)
    42.     {
    43.         isFinishingEditing = true;
    44.     }
    45. }
    46.  
    Note that it isn't 100% stable when using a single character, but it works most of the time.
     
    Last edited: May 28, 2015