Enter double space to replace comma in multiautocompletetextview - java

I want to add comma when I enter double space from keyboard in multiautocompletetextview. I search lots of thing in google. But can't reach my goal. I want to replace comma for double space entering by user.
So obviously, I must have to write something logic in ontextChange() or OnAfterTextChanged() in addtextwatcher listener.but i do't got event of after add 2 space.
I have already used comma tokenizer when select word from list.but i want to add comma when user entering double space using keypad.
Thanks in advance

The simplest solution i can provide you is to use String.replace(), Here is small code snippet to help you
#Override
protected void onCreate(Bundle savedInstanceState) {
...
edt.addTextChangedListener(textWatcher);
}
And TextWatcher which you are going to set on EditText
TextWatcher textWatcher = 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) {
edt.removeTextChangedListener(textWatcher);
String text = edt.getText().toString();
text = text.replace(" ", ",");
edt.setText(text);
edt.setSelection(text.length());
edt.addTextChangedListener(textWatcher);
}
#Override
public void afterTextChanged(Editable editable) {
}
};

Try like this, I have not tried this code
boolean userPressedKey = false ;
int spaceCount = 0;
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
userPressedKey = false ;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
userPressedKey = true;
});
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (userPressedKey) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
spaceCount ++;
if(spaceCount == 2){
//append comma to the edittext here
Toast.makeText(MainActivity.this, "White space is clicked twice", Toast.LENGTH_LONG).show();
}
return true;
}else{
spaceCount=0;
}
}
super.onKeyDown(keyCode, event);
}

Related

Android Studio: only accept input if its a letter using TextChangedListener

I'm working on an app using Android Studio where I want to take input from the user and use it later on.
I want to only accept this input if its a letter. If the user inputs a number I want to show the user an error message instead of accepting the input.
This is the code I'm currently working on, at the moment it accepts whatever input the user enters:
EditText input;
input.setText("");
input = findViewById(R.id.input);
input.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) {
if (charSequence.length() != 0) {
useInputLaterOn(charSequence.charAt(0));
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Use this code to check for a letter and show error if it's not a letter
input.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) {
if (charSequence.length() == 0) return;// No need to check because there's no text
Pattern pattern = Pattern.compile("^[a-zA-Z ]+$");
Matcher matcher = pattern.matcher(charSequence.toString());
if (!matcher.matches()) {
// It's not a letter
// Remove last entered character and show error message
input.setText(charSequence.toString().substring(0, charSequence.toString().length() - 1));
input.setSelection(input.getText().toString().length());
input.setError("Not a letter");
} else {
// It's a letter do something
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});

Why my app stops when I delete all from EditText?

I want to check if the value of EditText is greater than 5, I want the background of the edittext to become RED and it does so but stops when I delete the Text. Please help.
vibration.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) {
vibration.setBackgroundColor(Color.YELLOW);
}
#Override
public void afterTextChanged(Editable s) {
float numV = Float.parseFloat(vibration.getText().toString());
if(numV > 5){
vibration.setBackgroundColor(Color.RED);
}else{
return;
}
}
});
first check if the the value you are getting from EditText is not null.
if(!yourEditText.isempty)
{
//Do your work here
}
else{
//EditText is null}

how to use afterTextChanged to reduce an amount inside a textview

I am trying to reduce an amount set on a TextView using aftetTextChanged. To achieve this I am doing the following inside my framgnet
TextWatcher code
editTextDiscount.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) {
Double damageCost = 0d;
String decDE = txtDamageEstimate.getText().toString().trim();
decDE = decDE.replace("£", "");
Double decDeResults = Double.valueOf((String) (decDE));
if (!TextUtils.isEmpty(editTextDiscount.getText())) {
damageCost = editTextDiscount.getCleanDoubleValue();
}
txtDiscount.setText(" "+nf.format(-damageCost));
txtDamageEstimate.setText(nf.format(decDeResults-damageCost));
}
});
This works however it reduced x2 the amount inputed and when I backspace my input, it wont reset to its original amount e.g if it was 20.00 and I enter 00.01 it will show 19.97 and if i delete that .01 to 0 it will remain 19.97
TextWatcher shouldn't be used this way, it is designed as a watcher not an interactor.
I think you try to do something when edit is done. You should use FocusListener instead.
txtDiscount.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus)
// here focus is lost, means edit is done, validate text here
}

How to get the subtraction result of two EditText in third EditText for Android?

I'm new to Android programming. While making a program I was looking for the program to auto fill the subtraction result of two EditText widgets in third EditText, without any button. Please help me to find the solution.
int firstvalue=FirstText.getText.Tostring();
int secondvalue=SecondText.getText.Tostring();
int Answer=firstvalue - secondvalue;
thirdvalue.setText(Answer);
Hope it works. If any Problem occour tell me
You can use OnEditorAction Listener Method to edit Text and do what you want when the focus change
editText2.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do here your stuff f
int value1=Integer.valueOf(edittext1.getText().Tostring());
int value2=Integer.valueOf(edittext2.getText().Tostring());
int value3=value1 - value2;
thirdvalue.setText(String.valueOf(value3));
return true;
}
return false;
} });
EditText e1=(EditText) findViewById(R.id.editText1);
EditText e2=(EditText) findViewById(R.id.editText2);
EditText e3=(EditText) findViewById(R.id.editText3);
e2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.equals("") )
{
float a= Float.parseFloat(e1.getText().toString());
float b= Float.parseFloat(e2.getText().toString());
float c=a-b;
e3.setText(String.valueOf(c);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
Ofcourse I assume You'll do all the error handling. but this code will only if both fields are filled!.
Field1 = (EditText)findViewById(R.id.field1);
Field2 = (EditText)findViewById(R.id.field2);
Field3 = (EditText)findViewById(R.id.field3);
Field1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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 && Field2.getText().length() != 0)
Field3.setText(""+(Integer.parseInt(s.toString()) - Integer.parseInt(Field2.getText().toString())));
}
});
Field2.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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 && Field1.getText().length() != 0)
Field3.setText(""+( Integer.parseInt(Field1.getText().toString()) - Integer.parseInt(s.toString())));
}
});

dynamically change the length of String inside the EditText field in Android

I am using an EditText field in my application.
EditText is for entering the ddns input:
e.g www.example.com/xxxx
I want to restrict the length of the ddns id to 30 characters after "/" character.
i.e after "/" character, what follows must be of maximum 30 characters
I want to do it dynamically and restrict user to not type more than 30 characters.
How can i do it.
You can try the below way to restrict the user to enter less than 30 character.
tf.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//processing part
}
});
A very fast and ugly answer would look something like this:
yourEditText.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.toString().contains('/') {
if(s.toString().split('/')[1].length() == 30) {
//By only working with the EditText:
InputMethodManager imm = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
yourEditText.setFocusable(false);
yoruEditText.setEnabled(false);
}
}
}
#Override
public void afterTextChanged(Editable s) {
}});
//What i think is the best implementation, adding a TextView sitting on top of
//EditText with visibility set to GONE
yourEditText.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.toString().contains('/') {
if(s.toString().split('/')[1].length() == 30) {
//By working with EditText and a TextView
yourEditText.setVisibility(View.GONE);
yourTextView.setVisibility(View.VISIBLE);
yourTextView.setText(s);
}
}
}
#Override
public void afterTextChanged(Editable s) {
}});
In any case you want to add a textWatcher to your editText and change it accordingly.

Categories

Resources