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);
Related
for example: "2020/55" I have an edittext shape, put one after 4 numbers, write a number after it and it works well. My problem starts at the time of deleting. When deleting the part after /, it shows"/////" the figures of the part before / as and instead.I add your picture, how can I delete it.
enter image description here
And mask class
public class CaseInputMask implements TextWatcher {
int uzunluk = 0;
EditText girilenMetin;
public CaseInputMask(EditText girilenMetin) {
this.girilenMetin = girilenMetin;
this.girilenMetin.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String metin = girilenMetin.getText().toString();
uzunluk = metin.length();
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
String metin = charSequence.toString();
String girilenDeger = girilenMetin.getText().toString();
if (girilenDeger.length() == 4) {
metin += '/';
girilenMetin.setText(metin);
girilenMetin.setSelection(metin.length());
}
} catch (Exception e) {
}
}
#Override
public void afterTextChanged(Editable editable) {
}
}
You should check deletion like this:
public class CaseInputMask implements TextWatcher {
private boolean running = false;
private boolean deleting = false;
private final String inputMask = "####/##";
public CaseInputMask() {
}
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
deleting = count > after;
}
#Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
if (running || deleting) {
return;
}
running = true;
int length = editable.length();
if (length < inputMask.length()) {
if (inputMask.charAt(length) != '#') {
editable.append(inputMask.charAt(length));
} else if (inputMask.charAt(length-1) != '#') {
editable.insert(length-1, inputMask, length-1, length);
}
}
running = false;
}
}
And
girilenMetin.addTextChangedListener(new CaseInputMask())
I want to format my EditText after every typing.
(e.g: User types 1234 and I correct as 1.234,00)
With this function, I format String correctly and I can see it on Toast message. But whenever I try to set text to edittext, it gives error after typing second number.
This is my format function:
public String moneySeperator(double moneyAmount){
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator('.');
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("#,###.00", symbols);
String prezzo = decimalFormat.format(moneyAmount);
return prezzo;
}
Here is my EditText listener:
editText.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) {
editText.removeTextChangedListener(this);
if(!editText.getText().toString().isEmpty()){
String userInput = editText.getText().toString();
Toast.makeText(Denemey.this, ""+moneySeperator(Double.parseDouble(userInput)), Toast.LENGTH_SHORT).show();
editText.setText(""+moneySeperator(Double.parseDouble(userInput)));
}
editText.addTextChangedListener(this);
}
});
Here is the error: (It points editText.setText line)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.incidijital.kdvtevkifati, PID: 7500
java.lang.NumberFormatException: For input string: "15,00"
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
at java.lang.Double.parseDouble(Double.java:547)
at com.myproject.dd$1.afterTextChanged(Denemey.java:46)
UPDATED: I made my code really shorter. When I try a double like 1234.50
it works well on printing, but I think there is something wrong about afterTextChanged listener. My first 4 types (e.g: 1,234) works well, after I add more it crash because of Double.parseDouble casting, but there is no comma.
I removed formatter function.
#Override
public void afterTextChanged(Editable editable) {
editText.removeTextChangedListener(this);
java.text.NumberFormat formatter = java.text.NumberFormat.getInstance(java.util.Locale.GERMANY);
if(!editText.getText().toString().isEmpty()){
double myDob = Double.parseDouble(""+editable);
editText.setText(""+(formatter.format(myDob)));
editText.setSelection(editText.getText().length());
}
editText.addTextChangedListener(this);
}
});
You have to remove all non numeric chars before parsing it as double. I am assuming here that your #moneySeperator working fine for format .
editText.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) {
editText.removeTextChangedListener(this);
String userInput = editText.getText().toString().replaceAll("[^0-9]", "");
if(!userInput.isEmpty()){
Toast.makeText(Denemey.this, ""+moneySeperator(Double.parseDouble(userInput)), Toast.LENGTH_SHORT).show();
editText.setText(""+moneySeperator(Double.parseDouble(userInput)));
}else{
editText.setText("")
}
editText.addTextChangedListener(this);
}
});
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" };
I tried the following code, but android doesn't let me do that, as I'd enter into an infinite loop.
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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) {
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
});
Please any ideas would be very helpful. Thanks
3 possible solutions :
Check for the carriage return presence in the string
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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)
{
//I want to format the already existing text in s in a certain way as the user is typing
if (s.toString().charAt(s.length() - 1 ) != '\n')
{
mEditText.setText(s.toString() + " \n");
}
}
});
Not the best solution, according to what's in your input string
Do it in beforeTextChanged
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {}
});
Cancel the listener, temporarily
Like this :
final TextWatcher tw = 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)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.removeTextChangedListener(tw);
mEditText.setText(s.toString() + " \n");
mEditText.addTextChangedListener(tw);
}
});
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(tw);
You are most likely entering an infinite loop because you are changing the text in public void afterTextChanged(Editable s) which then causes the method to be called again.
The best solution would be to change the text in one of the other methods, or on the loss of focus of the editable. See View.OnFocusChangeListener for more details on this.
Here is an example:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
textView.setText(s.toString() + " \n");
}
}
});
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)
{
}
});