I have an EditText wrapped in a TextInputLayout. The TextInputLayout has an option for maximum length. Example below:
app:counterMaxLength="12"
Is it possible to have an option for counterMinLength? For example, if I am entering a password where the length cannot be less than a certain length.
I don't think it has an attribute like that, because when activity created it has an empty string value which breaks "min length" constraint.
So to resolve that I think you should verify the length in java/kotlin code like:
if(editText.getText().toString().length() > MIN_VALUE) {
// do somthing
}
I dont think that they are supporting this option at the moment :)
But I think if you want you can easily create a custom Edittext and then validate the minimum number of input field by using
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (edt.getText().toString().trim().length() < 8) {
edt.setError("Minimum length exception");
}
}
or you can you addTextChangedListener for your edittext
Something like that. ^^
Related
Recently, I migrate my android project to AndroidX and I use the EditTextPreference from AndroidX library. Now, I want to set the maximum length of the EditTextPreference to let say 50. I have tried to use:
android:maxLength="50"
but it's not working.
It seems that all android namespace won't work with the EditTextPreference and there is no code suggestion so I cannot find any related code to set the maximum length. How can I set the maximum length?
You need find your EditTextPreference by key, then set onBindEditTextListener to it and change layout attributes at the onBindEditText method:
EditTextPreference preference = findPreference("edit_text_preference_key");
preference.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
editText.setInputType(InputType.TYPE_CLASS_NUMBER); // set only numbers allowed to input
editText.selectAll(); // select all text
int maxLength = 2;
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)}); // set maxLength to 2
}
});
You can put this code to onResume() method of yout PreferencesFragment or PreferencesActivity.
You may try with java code, that will works.
Here is snipped.
EditText et = (EditText) findViewById(R.id.myeditText);
et.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(50) }); // maximum length is 50
I hope that will help you.
This is the code to set maximal length (in this case 10) of EditTextPreference:
final EditTextPreference prefCustomText = findPreference(ActivityPreferences.PREF_DISPLAY_CUSTOM_TEXT);
prefCustomText.setOnBindEditTextListener(editText -> {
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
});
And I'm adding information as wrote #Viktor Brešan:
I think you should append the new InputFilter to ones that might have
been added to EditText previously. You can get them by calling
editText.getFilters()
I'm overriding the onFocusChanged to pinpoint the TextEdit that just lost focus. The point is to remove this TextEdit if it doesn't have any text in it. The problem is that the if((EditText... is not valid. I get cannot resolve method 'getText()'. I've tried casting it to tell that it's a EditText view.
listItemsView is the id of the LinearLayout and listItems is an ArrayList of EditText.
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
int removeCurrent = listItems.indexOf(this)+1;
// code to execute when EditText loses focus
if((EditText)view.getText().equals("")) {
}
listItemsView.removeViewAt(removeCurrent);
listItems.remove(removeCurrent);
}
}
I've also tried:
if((EditText)listItemsView.getChildAt(removeCurrent).getText().equals(""))
And got the same result.
When you want to cast a value to a type, you do this:
(T)V
Where T is the type you want to cast to, and V is the value.
So far so good?
In your code,
(EditText)view.getText().equals("")
What is V and what is T? The answer might surprise you! T is obviously EditText, but V here is actually view.getText().equals("")!
In other words, you did not cast view to EditText. That's why the compiler can't find a method named getText. To tell the compiler to specifically cast view, add parentheses:
((EditText)view).getText().equals("")
When a user enters information in an EditText, and moves to the next EditText, the information is highlighted as shown below:
The code for this:
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
//v.setBackgroundColor(Color.LTGRAY); //also works like this
((EditText) v).setBackgroundColor(Color.LTGRAY);
((EditText) v).setTextColor(Color.BLACK);
}
}
});
Which is called in the onCreate method like this:
edittext = (EditText) findViewById(R.id.editText1);
However, It would be much better if the background color only applied to the text itself, rather than the view, like this (from the gmail app):
Does anybody have any suggestions on how to apply the background color to the text only (not the whole EditText view) as above?
Thanks.
You can achieve what you want by using a BackgroundColorSpan. You can find more information here:
http://developer.android.com/reference/android/text/style/BackgroundColorSpan.html
To use spans you need to build a SpannableString which you can do using a SpannableStringBuilder:
http://developer.android.com/reference/android/text/SpannableStringBuilder.html
How to allow only a valid floating point number into a text field floating point like these only
2.353
-2.354
4444.45
Implement a focus listener on the field. When the focus changes from the textfield to any other part of your form simply use a regexp to check the validity of the input.
Something like :
^(-)?\d*(\.\d*)?$
Should do the trick.
Then use the pattern matching of Android to see if the input matches the regexp :
Pattern p = Pattern.compile("^(-)?\d*(\.\d*)?$");
Matcher m = p.matcher(inputString);
if (m.find()) {
////Found
}
else {
//Not found
}
But be aware of local settings...In France for example, the dot(.) used to separate the decimals is in fact a comma(,)
Use OnFocusChangeListener to achieve this.
//value pool
final String[] check = new String[]{"2.353","-2.354","4444.45"};
yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
String s = ((EditText)v).getText().toString().trim();
for(String tmp : check){
if(!TextUtils.isEmpty(s) && s.equals(tmp)){
//it ok
break;
}
else{
//do something
}
}
}
});
For edittext use android:inputType="number"
Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())`).
android:inputType="numberDecimal|numberSigned"
I have two Edit Text fields in my application,if i enter 50 in first edit Text then in the second edit text i want to give permission to enter values less than 50 when the user enters a value in the second edit text, i want to give alert to the user if it is bigger than first edit text value.
How can i do this,means i need to check and show the alert when he enter the values i don't want to show the alert when we press any other fields...
For example:in field 1 if user enter 35
in field2 he has enter value less than 35 only...
initialize both edittext's, lets called them myEditText1 and myEditText2
in myEditText2 set a listener, there should be some kind of listener for when the text changes. Look for something like setOnTextChangedListener, here is how it would be used (this is pseudocode)
myEditText2.setOnTextChangedListener(new OnTextChangedListener(){
#Override
public void OnTextChanged(View v)
{
int i = Integer.valueOf(myEditText1.getText().toString());
int j = Integer.valueOf(myEditText2.getText().toString());
if(j >= i)
{
myEditText2.setText(""); //this automatically sets the editText2 field back to empty
}
}
});
this will only allow the user to enter numerical values lower than the value in the first edit text.
But you need to make sure that the inputType: of both of your editText fields is set to numeric.
You can compare numbers onFocuseChange of second edit text.
editText2.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
// here place code for comparison and calling alert
}
}
});