# Numeric Text Field Input (patch included)

**URL:** <https://forum.juce.com/t/numeric-text-field-input-patch-included/14121>\
**Category:** Android\
**Created:** [December 25, 2014, 12:10am UTC](https://forum.juce.com/t/numeric-text-field-input-patch-included/14121 "2014-12-25T00:10:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [December 25, 2014, 12:10am UTC](https://forum.juce.com/t/numeric-text-field-input-patch-included/14121/1 "2014-12-25T00:10:04Z")

</div>

Just came across this looking to adapt numeric input fields for the Android port of my app.&nbsp;

Not quite sure how it would work currently but I think this is important for e.g. sliders and text inputs.&nbsp;

Somehow need to get an InputType parameter to the onCreateInputConnection function in the Java Activity, is my first guess.

Anyone managed a way round this?

&nbsp;

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [January 3, 2015, 2:27pm UTC](https://forum.juce.com/t/numeric-text-field-input-patch-included/14121/2 "2015-01-03T14:27:00Z")

</div>

I've been searrching around the JUCE code to try and find a way to get a numerical text input keyboard to appear when editing the value for a Slider. Currently a generic keyboard comes up which is not very user friendly.

So far I see that TextEditor inherits&nbsp;TextInputTarget, which has:

```
​virtual VirtualKeyboardType getKeyboardType() { return textKeyboard; }
```

So I've gone and made a few alterations to the TextEditor, Label and Slider classes, patches below:

```
Subject: [PATCH] add keyboardType for Android/iOS
---
 modules/juce_gui_basics/widgets/juce_Label.cpp    | 4 +++-
 modules/juce_gui_basics/widgets/juce_Label.h      | 5 +++++
 modules/juce_gui_basics/widgets/juce_Slider.cpp   | 1 +
 modules/juce_gui_basics/widgets/juce_TextEditor.h | 5 +++++
 4 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/modules/juce_gui_basics/widgets/juce_Label.cpp b/modules/juce_gui_basics/widgets/juce_Label.cpp
index 7ab3d5e..04b8fce 100644
--- a/modules/juce_gui_basics/widgets/juce_Label.cpp
+++ b/modules/juce_gui_basics/widgets/juce_Label.cpp
@@ -32,7 +32,8 @@ Label::Label (const String& name, const String& labelText)
       minimumHorizontalScale (0.7f),
       editSingleClick (false),
       editDoubleClick (false),
-      lossOfFocusDiscardsChanges (false)
+      lossOfFocusDiscardsChanges (false),
+      keyboardType (TextEditor::textKeyboard)
 {
     setColour (TextEditor::textColourId, Colours::black);
     setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
@@ -209,6 +210,7 @@ void Label::showEditor()
         addAndMakeVisible (editor = createEditorComponent());
         editor->setText (getText(), false);
         editor->addListener (this);
+        editor->setKeyboardType (keyboardType);
         editor->grabKeyboardFocus();
 
         if (editor == nullptr) // may be deleted by a callback
diff --git a/modules/juce_gui_basics/widgets/juce_Label.h b/modules/juce_gui_basics/widgets/juce_Label.h
index 825b0a0..58cfdd6 100644
--- a/modules/juce_gui_basics/widgets/juce_Label.h
+++ b/modules/juce_gui_basics/widgets/juce_Label.h
@@ -162,6 +162,9 @@ public:
 
     /** Specifies the amount that the font can be squashed horizontally. */
     float getMinimumHorizontalScale() const noexcept                    { return minimumHorizontalScale; }
+    
+    /** Set keyboard type for passing down to TextEditor/ */
+    void setKeyboardType (TextEditor::VirtualKeyboardType type)         { keyboardType = type; }
 
     //==============================================================================
     /**
@@ -338,6 +341,8 @@ private:
 
     bool updateFromTextEditorContents (TextEditor&);
 
+    TextEditor::VirtualKeyboardType keyboardType;
+    
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Label)
 };
 
diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp
index 8c76b90..daaebbe 100644
--- a/modules/juce_gui_basics/widgets/juce_Slider.cpp
+++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp
@@ -565,6 +565,7 @@ public:
             owner.addAndMakeVisible (valueBox = lf.createSliderTextBox (owner));
 
             valueBox->setWantsKeyboardFocus (false);
+            valueBox->setKeyboardType (TextEditor::numericKeyboard);
             valueBox->setText (previousTextBoxContent, dontSendNotification);
             valueBox->setTooltip (owner.getTooltip());
 
diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h
index 4dd5b21..4dbf85f 100644
--- a/modules/juce_gui_basics/widgets/juce_TextEditor.h
+++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h
@@ -583,6 +583,10 @@ public:
     void setInputRestrictions (int maxTextLength,
                                const String& allowedCharacters = String::empty);
 
+
+    VirtualKeyboardType getKeyboardType()               { return keyboardType; }
+    void setKeyboardType (VirtualKeyboardType type)     { keyboardType = type; }
+
     //==============================================================================
     /** This abstract base class is implemented by LookAndFeel classes to provide
         TextEditor drawing functionality.
@@ -692,6 +696,7 @@ private:
     juce_wchar passwordCharacter;
     OptionalScopedPointer<InputFilter> inputFilter;
     Value textValue;
+    VirtualKeyboardType keyboardType;
 
     enum
     {
```

This allows me to set the inputMethodType to 'number' and I can see it by doing Log calles in the Java activity. However, as I have learned today, the line&nbsp;

```
​imm.setInputMethod (getWindowToken(), type);
```

has no effect as Android does not allow setting the keyboard type programmatically. Searching around I see some suggestions to use&nbsp;showInputMethodPicker()&nbsp;however this is more for using preset language keyboards AFAICS.&nbsp;

So... the next step I plan to take is to use a totally custom keyboard in Java, following examples/suggestions such as

http://stackoverflow.com/questions/15099725/need-number-only-soft-keyboard and&nbsp;

http://stackoverflow.com/questions/1896939/android-app-specific-soft-keyboard

Cheers

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [January 13, 2015, 11:09pm UTC](https://forum.juce.com/t/numeric-text-field-input-patch-included/14121/3 "2015-01-13T23:09:10Z")

</div>

So ... I now have custom soft keyboards working,&nbsp;both numeric and decimal types. &nbsp;Jules has kindly adjusted&nbsp;and merged my&nbsp;patches to get this working on the JUCE side, including some minor changes to iOS to bring up numeric or decimal keyboards. I will&nbsp;post up an example project soon with my keyboard Fragment and modified Java Activity.&nbsp;
