I want to check if a String (coming from an EditText) is part of a String Array and I need the index. I have got a TextWatcher added as TextChangedListener like the following:
inside Activity-Class:
private String[] randomSpanishWords = { "pueblo", "madre" };
private TextWatcher ETListener = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
int index = Arrays.asList(randomSpanishWords).indexOf(charSequence);
}
#Override
public void afterTextChanged(Editable editable) {
}
};
in onCreate():
editText1.addTextChangedListener(ETListener);
No matter what Im typing in the EditText, index is always -1. What am I missing?
Try to convert your charSequence to String with
charSequence.toString()
Or use CharSequence for your array
private CharSequence[] randomSpanishWords = { "pueblo", "madre" };
Related
I am trying to implement something similar to a code editor where keywords are automatically highlighted. I am going to have a string array and I want to change the color and font of the editText string when the user types the text and it matches a string from the string array. I am using the addTextChangeListener but the text of the whole editText changes. I want just the matched word to be highlighted. Here is my code:
inputCodeEditText.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) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.toString().contains("for"))
{
inputCodeEditText.setTextColor(getResources().getColor(R.color.indigo));
}
}
});
I understand I have to use spans but the code crashes. Can anyone help me with the correct usage of spannable strings with addTextChangedListener() ?
Use addTextChangeListener API eg:
editText.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) {
// The changed text comes in "s" parameter.
// Here you can watch the changes and take an action you want...
}
#Override
public void afterTextChanged(Editable s) {
}
});
Trying to get an integer from Edit Text entered by the user(such as Total Amount) and to display it by multiplying with value(such as 5%) and display it using TextView automatically.
For the 1st part i.e. simply displaying the integer from EditText to TextView got NumberFormatException Error.
'''
temp = (Integer.parseInt(editText.getText().toString()));
textView.setText(temp);
'''
And for the second part i.e. automatically displaying the value from EditText to TextView, no idea how to approach.
Just use textwatcher and put the logic in it. Just make sure change the id of edittext and textview
et.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) {
String value = s.toString();
double outputValue = Integer.parseInt(value) * 0.05;
textView.setText(String.valueOf(outputValue));
}
});
Make sure in edittext that it only capture the numerical value.
you could try this approach
editText.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) {
String userInputText = s.toString();
try {
val userInputAsNumbger = userInputText.toInt();
}catch (e:NumberFormatException){
print(e.message)
}
}
});
to ensure it doesn't error
I want to know what is the locale of input text in edittext!
However i tried to get the keyboard locale but that wasn't helpful.
I don't want to type english in the edittext so help me!
I want user to know that he/she can't type english!
Sorry for my bad english!
Here is my code :
edtUsernameLogin.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (editable.toString().isEmpty()) {
mtfUsernameLogin.setLabelColor(G.resources.getColor(R.color.cpb_white), "نام کاربری");
} else {
String locale = G.inputMethodManager.getCurrentInputMethodSubtype().getLocale();
if (locale.equals("fa") || locale.equals("ar")) {
mtfUsernameLogin.setLabelColor(G.resources.getColor(R.color.sPink), "نام کاربری باید انگلیسی باشد !");
} else {
mtfUsernameLogin.setLabelColor(G.resources.getColor(R.color.sGreen), "نام کاربری");
}
}
}
});
The Solution is :
InputFilter usernameFilter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String blockCharacterSet = " اآبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی";
if (blockCharacterSet.contains(source)) {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.grey));
edtNameEdit.setHint("نام و نام خانوادگی");
return source.toString();
} else {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.sPink));
edtNameEdit.setHint("نام و نام خانوادگی باید فارسی باشد!");
return "";
}
}
};
edtNameEdit.setFilters(new InputFilter[]{usernameFilter});
And important line :
edtNameEdit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
I'm new to Android development and I want to create a method that runs every time my editText is changed.
In swift I use this function:
func fieldChanged(textfieldChange: UITextField){
}
I can't find anywhere a similar function for Java and Android Studio, are there any suggestions?
// Globally declare a variable
private TextWatcher mTextWatcher;
// Call textChanger() method in onCreate
private void textChanger() {
mTextWatcher = 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() == 6 && mEditText.getText().toString().trim().equals("some value to check"))
mEditTextOther.requestFocus();
else
mEditText.setError("enter correct");
}
#Override
public void afterTextChanged(Editable s) { }
};
mEditText.addTextChangedListener(mTextWatcher);
}
try this
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
I have a number of EditText's with an inputType="numberDecimal" and i'm wanting to change the input into currency (TextWatcher maybe?). Can anyone help with this? 99% of my project is done bar this. Thanks guys.
try below code:-
tv.addTextChangedListener(new TextWatcher()
{
private String current = "";
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(!s.toString().equals(current))
{
if(tv.getText().toString().trim().length()>0)
{
tv.removeTextChangedListener(this);
String formated = tv.getText().toString().trim().replace("$", "");
current = formated;
tv.setText("$"+formated);
tv.setSelection(formated.length()+1);
tv.addTextChangedListener(this);
}
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(final Editable s)
{
}
});