I am using this library for material editText with label:
https://github.com/rey5137/Material/wiki/Text-Field
nice library :)
but...
i am using next code to check are entered symbols correct:
private boolean hasCorrectSymbols(String input){
String tre = "[A-Za-z0-9\\#\\#\\$\\%\\&\\*\\(\\)\\-\\+\\_\\;\\:\\?\\.\\,\\!]+$";
if (input.matches(tre)){
return true;
}
return false;
}
for checking correct symbols I am using textWatcher:
mEditPass.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 1 && !loginPassHasCorrectSymbols(s.toString())){
mEditPass.getText().clear();
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
} else if (s.length() >1 && !loginPassHasCorrectSymbols(s.toString())) {
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
String text = s.toString();
text = text.substring(0, start);
mEditPass.setText(text);
mEditPass.setSelection(start);
}
}
#Override
public void afterTextChanged(Editable s) {}
});
if first symbol is correct and the second one for example is wrong - system will cut last entered (wrong) symbol and set cursor to the last text position, but if the first symbol is prohibited symbol - it will crash with this error:
java.lang.IndexOutOfBoundsException: setSpan (0 ... 1) ends beyond length 0
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
at android.text.method.PasswordTransformationMethod.onTextChanged(PasswordTransformationMethod.java:108)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:683)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:198)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Any ideas why it happen? And how to fix this??
In this code snippet, you shorten the text displayed in the EditText (so that the last valid position is start-1), but still try to set the selection position to start.
text = text.substring(0, start);
mEditPass.setText(text);
mEditPass.setSelection(start);
[EDITED]
Assuming that your Toast message is prompting the user to fix the error, I think what you actually want to do is to set the selection to the first character in the EditText with a bad symbol. Here is sample code for doing that:
// A reusable Pattern (at the class level) that defines the regex for bad characters.
private static final Pattern ILLEGAL_CHAR_PATTERN = Pattern.compile(
"[^A-Za-z0-9\\#\\#\\$\\%\\&\\*\\(\\)\\-\\+\\_\\;\\:\\?\\.\\,\\!]"
);
// Replacement for your listener code.
mEditPass.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 0 || count == 0) {
return;
}
Matcher matcher = ILLEGAL_CHAR_PATTERN.matcher(s);
if (matcher.find()) {
int firstIllegalPos = matcher.start();
mEditPass.setSelection(firstIllegalPos);
String mess = getString(R.string.toast_login_useLatin);
showToastMessage(mess);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
This problem was fixed by adding next code:
mEditPass.getText().clearSpans();
before
mEditPass.getText().clear();
The Error says you are ending the spanable text before its start, look for any character that you gave to end and its appearing before start
you should trim the text.
spannable.setSpan(
ForegroundColorSpan(textColor),
FIRST_INDEX, someText.trim().length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
just add one more space to your string and then
mEditPass.setSelection(new string);
Related
I am very new to this sort of thing and am trying to teach myself but have gotten stuck here. I am writing an app which will calculate the size battery required. The calculator side of things i have done and it is working however i want to add a feature that when the answer is displayed, it will tell you what part number you need for the battery required. I have tried to work this out myself and failed miserably so i am hoping someone may be able to point me in the right direction
The answer from the battery calculator shows in ahresult. what i want is battreq to show the part number required for the correct size battery. so for example, if the calculator showed that a 2.43Ah battery was required, battreq would show "A 3.2ah battery is required" as that is the next size available.
ahresult = (TextView) findViewById(R.id.ahresult);
battreq.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Integer.parseInt(ahresult.getText().toString());
if (ahresult.equals(>= "0.00" && <= "1.20")){
battreq.setText("A 1.2Ah battery is required");
}
else if (ahresult.equals (>= "1.21 && <= 2.10")){
battreq.setText("A 2.1Ah battery is required");
}
else if (ahresult.equals (>= "2.11 && <= 3.20")){
battreq.setText("A 3.2Ah battery is required");
}
// so on and so on with different battery sizes
}
Here is my (poor) effort at having a go but i am getting illegal start of expression.
Could someone guide me in the right direction please?
EDIT: Still seem to be having issues with this despite the help here. Below is the updated code. Have i got anything wrong here?
ahresult.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
double result = Double.parseDouble(ahresult.getText().toString());
if (ahresult.equals(result >= 0.001 && result <=1.200 )) {
battreq.setText("1.2Ah req");
}
else if (ahresult.equals(result >= 1.201 && result <=2.100 )) {
battreq.setText("2.1Ah req");
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Firstly, you should add addTextChangedListener to ahresult instead of battreq.
Secondly, you have to parse the changed text s to Double then compare within your desired range:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Double result = Double.parseDouble(s.toString());
if (ahresult.equals(result >= 0.00 && result <= 1.20)){
battreq.setText("A 1.2Ah battery is required");
}
else if (ahresult.equals (result>= 1.21 && result <= 2.10)){
battreq.setText("A 2.1Ah battery is required");
}
else if (ahresult.equals (result>= 2.11 && result <= 3.20)){
battreq.setText("A 3.2Ah battery is required");
}
// so on and so on with different battery sizes
}
You need change this
battreq.addTextChangedListener(new ...
With this
ahresult.addTextChangedListener(new ...
You need listen the ahresult's value and change the battery needed.
In the "onTextChanged" the "CharSequence s" param is the ahresult's text
Although code is far from being complete, for the begining I tried to detect via TextWatcher
when character is pressed in EditText that doesn't belong to hex values and not to permit it
to be displayed, but to inform user of error entry.
The excerpt from code follows, where arrayOfChars consists of 16 permitted hex chars and edt2
is EditText var. What I tried is to enter following chars: "aeyd", so it is aim to inform of
"y" as an error and not to display it.
edt2.addTextChangedListener(new TextWatcher(){
private boolean errorDetected= false;
private int oldbefore;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String st;
if (errorDetected){ //set in code bellow
errorDetected= false;
return;
}
st= s.toString();
if (before > 0){
if (before<= s.length()){st= s.subSequence(before, s.length()).toString();}
}
boolean velid= true;
for (char c: st.toCharArray()){
if (new String(arrayOfChars).indexOf(Character.toUpperCase(c))==-1){
edt2.setError("Wrong char - " + c);
errorDetected= true;
oldbefore= before;
velid= false;
break;
}
}
if (velid) {edt2.setError(null);}
}
#Override
public void afterTextChanged(Editable s) {
if (errorDetected){s.delete(oldbefore, s.length());}
}
});
When "y" is entered everything behave as expected- only "ae" displayed and error info also.
However when "d" is entered and breakpoint settled at the beginning of onTextChanged, I see
"s" parameter to be "aeyd"- so "y" is still preserved somehow.
Any help where I go wrong ?
I was wondering what is the best way to validate form ?
I did try the following:
EditText fname = (EditText)findViewById(R.id.first_name);
String fname_text = fname.getText().toString;
if(fname_text.equalsIgnoreCase(""))
{
fname.setError("Field is required");
}
and also:
fname.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after){
}
#Override
public void afterTextChanged(Editable s)
{
Pattern mPattern = Pattern.compile("[A-Za-z]{2,20}$");
Matcher matcher = mPattern.matcher(s.toString());
if(!matcher.matches()) // on Success
{
fname.setError("Please make sure you input a valid first name");
}
}
});
The thing that I am confused in is that ... whenever the page loads for the first time, the error message is shown, but when I go inside the EditText and type some content, and if I erase the content, the error message does not persist. So how do I keep this validation persistent ??? Because the way that the program is shaping up, it looks like it won't validate anything very nicely. And you guys know some good links for regex in android with complete example, please do recommend.
And also, how will me putting the Pattern and Matcher methods in onTextChanged or beforeTextChanged affect the output ?
Instead of running the check right after
EditText fname = (EditText)findViewById(R.id.first_name);
String fname_text = fname.getText().toString;
Use an OnFocusChangeListener and run it whenever the onFocusChange() method is called. Ideally, you'd run it only when the View loses focus. Something like:
EditText fname = (EditText)findViewById(R.id.first_name);
String fname_text = fname.getText().toString;
fname.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
if(!(v.isFocused())) {
//Run your validation
}
}
});
This way, you only run the Validation when the user is done typing, instead of everytime the user changes something.
I want to create the EditText which allow Numeric Values, Comma and Delete and other values are ignore.
So How I can achieve this by programming code ?
Thanks.
I achieved same thing using follwing code, hope you will also find help from it.
editText.addTextChangedListener(controller);
#Override
public void afterTextChanged(Editable s) {
if(s.trim.length() > 0)
{
int start = s.toString().length() - 1;
int a = (int) s.charAt(start);
if (!(a >= 48 && a <= 57 || a == 44))
s = s.delete(start, start + 1);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
To restrict what characters can be typed into an android EditText, you must set them in the android:digits XML attribute. See http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits . Make sure to also validate user input before putting it into storage.
just simple ,
you want to create a text-field like that you can enter the new string must have some grammar that you want to allow.
i have some email validation grammar that can help you.
string = "/alphabetics/number/special-character" ;
and another is
user-entered-string = " ";
just compare user-entered-string's lexems and you defined grammar.
I tried to set this up so that when I enter something into the EditText field, it will change the text color and set a variable to the value of the numbers entered. However, when I delete the characters from the field, it triggers an error that closes the app. I think I may either need to find an if statement that doesn't rely on length, or maybe use some of the other methods (onTextChanged, beforeTextChanged... I don't really know how to use either of those correctly though)
public void afterTextChanged(Editable arg0) {
if(arg0.length()>0){
ageTag.setTextColor(Color.GREEN);
ageEntered=true;
ageInYears=Integer.parseInt(enterAge.getText().toString());
}
}
Implement the android.text.TextWatcher interface
Bind a listener:
enterAge.addTextChangedListener(this);
I had a similar problem. I had to append "-" after 5th char, 11th etc. When I wanted to delete the "-" it was ignoring the delete key, so what I did is the following.
Created two variables: beforeCharCounter and charCounter and in onTextChanged I have set their values on before and count.
activationKeyEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
beforeCharCounter = before;
charCounter = count;
System.out.println(count);
}
#Override
public void afterTextChanged(Editable s) {
if ((s.length() == 5 && beforeCharCounter <= 5 && charCounter == 5) ||
(s.length() == 11 && beforeCharCounter <= 11 && charCounter == 11)
|| (s.length() == 17 && beforeCharCounter <= 17 && charCounter == 17)) {
s.append('-');
}
}
});
if the value of the before is bigger then the length then for sure it is delete and this will solve the delete issue. The count is used for when the user has entered 10 chars and he press on the 5th one. If we don't keep the exact count dashes will be appended at the end like crazy.
Hope this will help you solve the issue.