I have a preference to get port of a service as shown below
<EditTextPreference
android:defaultValue="4444"
android:key="port"
android:title="Port"
android:dependency="service_on"
android:inputType="number"
android:maxLength="4"
/>
I have restricted it to maxLength 4 but there does not seem to be a minlength or minvalue ??
Basically I want to avoid empty values or lower value ports to avoid conflicts of port already in use/ android allocated ports etc
Any ideas how can I smartly changed the xml only without having to check on callbacks/listeners ?
Thanks,
Related
Is it possible to create an alert in the edittext?
I've seen it in 2 apps with exactly the same style, so I understand it's a default view or property.
YOu have to use https://github.com/vekexasia/android-edittext-validator
There are several values you can set to the test attribute:
regexp: for custom regexp
numeric: for an only numeric field
alpha: for an alpha only field
alphaNumeric:
personName: checks if the entered text is a person first or last name.
personFullName: checks if the entered value is a complete full name.
email: checks that the field is a valid email
creditCard: checks that the field contains a valid credit card using Luhn Algorithm
phone: checks that the field contains a valid phone number
domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 )
ipAddress: checks that the field contains a valid ip address
webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
date: checks that the field is a valid date/datetime format ( if customFormat is set, checks with customFormat )
nocheck: It does not check anything except the emptyness of the field.
It is possible using setError method. it is available from API Level one only.
editText.setError("Enter yor error message").
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="#string/cont_num"
android:inputType="phone"/>
</android.support.design.widget.TextInputLayout>
From the android design library
Rather than using an EditText widget one solution would be a RelativeLayout with a Auto complete TextView widget and an alert Icon of your choice set rightOf your AC TextView within the RL.
Then add a text change listener to your AC textview which is looking for your specific text input i.e. binary and if a breach is detected display warning and error icon?
I have an Android application using default values from the Preferences Android framework.
It all works fine except for phone numbers (defined with android:inputType="phone" in preferences.xml).
The phone numbers get treated as numeric value so if I go to the preferences screen to see the default values I see
3.3631241E10
for the value defined in preferences.xml as
android:defaultValue="+33631241234"
To avoid this problem, I have used values from strings.xml defining the default values in preferences.xml like this :
android:defaultValue="+33631241234
It works ... but I don't like it: that's a source of problem as I need to redefine the same phone number for each language used ! !
I must be doing something wrong as I haven't found anyone else with the same problem on internet however I don't see what I am doing wrong ! !
Any help would be really appreciated.
that's a source of problem as I need to redefine the same phone number for each language used
you don't - when the text is absent for a language the default is used - docs :
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml.
I have a set of values defined in preference.xml:
<EditTextPreference
android:defaultValue="19999999999"
android:key="#string/phone_number"
android:persistent="true"
android:summary="Test Number"
android:title="Phone" />
For some reasons, the OS thinks "19999999999" is an integer(int type) and caps it to 2^31. So I end up seeing something like: 672647167
19999999999 : 10010101000000101111100011111111111
Removing the first four binary numbers, I get the following that matches with what Android OS shows.
672647167 : 101000000101111100011111111111
Is there a workaround?
Try using something like android:hint="#string/PhoneNumberDefault" and put your phone number in the strings resource. Should have the desired effect.
I am essentially trying to set the digits value of an EditText programmatically. So far I have:
weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightInput.setKeyListener(DigitsKeyListener.getInstance());
Which is fine, but I also want to be able to include a decimal place (.). Any ideas?
Try this:
<EditText
android:inputType="number"
android:digits="0123456789."
/>
From Code:
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
But, it allows the user to include several "."
See JoeyRA's answer for real numbers.
Try this:
weightInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
weightInput.setKeyListener(DigitsKeyListener.getInstance(false,true));
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
Use InputType.TYPE_NUMBER_FLAG_DECIMAL.
Also see: Input Types.
if anyone still finding proper way, note that its setRawInputType() not setInputType()
val digits = "ABCDabcd" // or any characters you want to allow
editText.keyListener = DigitsKeyListener.getInstance(digits)
editText.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME)
For IP address input ( Multiple dots and numbers )
try
<EditText
android:id="#+id/ipBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/ipAddrHint"
android:inputType="numberDecimal|number"
android:digits="0123456789."
android:textSize="30sp" />
None of the above solutions worked as expected. Digits in xml file still allow me to put "," in the input.
Check for it, it's works for me:
edittext.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
What is the best way of configuring a widget in android for entering a 4 digit PIN?
Currently I have:
<EditTextPreference
android:title="PIN"
android:summary="Your PIN number"
android:key="password"
android:numeric="integer"
android:maxLength="4"
android:password="true"
/>
This works quite well, but there is no way of constraining the input to be at least 4 digits. Is this how you would have someone set a PIN in preferences? Or is there a better way?
By the way, before people comment on security implications, I didn't choose the 4 digit PIN, it is for connecting to a third party system and I can't change it. And no, this isn't for banking.
You can add an OnPreferenceChangeListener and check the password length there.
E.g. if using a PreferenceActivity:
findPreference("password").setOnPreferenceChangeListener( new OnPreferenceChangeListener(){
public boolean onPreferenceChange (Preference preference, Object newValue){
if ((String)newValue).length == 4)
super.onPreferenceChange(preference, newValue);
else
Toast.makeText(this, "The PIN must have 4 digits", Toast.LENGTH_LONG).show();
}
});
There is an interesting View in Github that makes the coding of the 4 entry quite simple and easy to use. Check source