Reputation: 450
I'm making an android app for multiple languages. I would like to set the inputType for an EditText view to be different based on the keyboard language. In the XML file I've tried:
android:inputType="@string/xyz"
and in the Strings resource file string xyz would be
<string name="xyz">number</string>
I was hoping that this was a smart idea for easily changing the input type bases on language. For example, if the language were English I would set xyz to letters, but in Japanese I would have xyz as numbers. I was trying to avoid doing this programmatically because as soon as I start entering input filters in code much of my XML settings are ignored, even if they are not directly overridden by the new code.
I get an error though when I try to call this string resource to the inputType. Any assistance would be greatly appreciated.
Upvotes: 3
Views: 2036
Reputation: 11
Also, you can use (and override) style for your EditText (with attrs) for each language qualificator
f.e.
<style name="PinCodeInputType">
<item name="android:inputType">numberDecimal</item>
</style>
<EditText
android:id="@+id/code"
style="@style/PinCodeInputType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Upvotes: 0
Reputation: 14444
You can do it only via XML but you have to use an integer
and not a string
resource:
In your value folder
<integer name="myInputType">12</integer> <!-- numberPassword -->
In your layout
android:inputType="@integer/myInputType"
The integer values are listed here: https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
Upvotes: 0
Reputation: 3975
Yes you can achieve this programmatically since inputType
can take an integer
value. Here is the implementation it is actually so easy.
TYPE_CLASS_TEXT has a constant value: 1
TYPE_CLASS_NUMBER has constant value: 2
This is the value your strings.xml (which you are going to localise): For example:
input_type value in strings for English (text):
<string name="input_type">1</string>
input_type value in strings for Japanese (number):
<string name="input_type">2</string>
In your Activity
, declare your EditText
and set the inputType
from strings.xml so it will be set according to the localisation:
editText = (EditText)findViewById(R.id.editText1);
editText.setInputType(Integer.parseInt(getResources().
getString(R.string.input_type)));
Upvotes: 3
Reputation: 38605
No, you cannot do this. The android:inputType
attribute is defined as using flags that are also defined by Android. You cannot provide an arbitrary string value for this attribute, you can only use those pre-defined flags.
Upvotes: 0