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}
Related
I have two editTexts. I want to test every time if the text has been changed and if it's the case I want to change the text of the second one too.
Here is what I've done :
TextWatcher fieldValidatorTextWatcherElec = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (filterLongEnough1()) {
et_electricite_€.setText(String.valueOf(new BigDecimal(Double.parseDouble(et_electricite.getText().toString())*tarif).setScale(2, RoundingMode.HALF_UP).doubleValue()));
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
private boolean filterLongEnough1() {
return et_electricite.getText().toString().trim().length() > 0;
}
};
et_electricite.addTextChangedListener(fieldValidatorTextWatcherElec);
TextWatcher fieldValidatorTextWatcherElecTarif = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (filterLongEnough()) {
et_electricite.setText(String.valueOf(new BigDecimal(Double.parseDouble(et_electricite_€.getText().toString())/tarif).setScale(2, RoundingMode.HALF_UP).doubleValue()));
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
private boolean filterLongEnough() {
return et_electricite_€.getText().toString().trim().length() > 0;
}
};
et_electricite_€.addTextChangedListener(fieldValidatorTextWatcherElecTarif);
The problem is: When I click for the first time it changes; when I try to do the same thing with the second editText the application doesn't move and after that, it crashes. Here's the errorlog:
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 2057252)
E/MQSEventManagerDelegate: reportJEEvent error happened:android.os.TransactionTooLargeException: data parcel size 2057252 bytes
you can add an edit text listener. Put you logic on afterTextChanged
EditText answer = new EditText(this);
//second, we create the TextWatcher
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) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
String answerString = answer.getText().toString();
//and now we make a Toast
//modify "yourActivity.this" with your activity name .this
Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();
}
};
//third, we must add the textWatcher to our EditText
answer.addTextChangedListener(textWatcher);
I'm adding a text in EditBox with setText code.After I'm changing this text into EditText and I want to get in a String value this text with getText.But this returning null.
My code:
EditText kullanilan = (EditText) findViewById(R.id.edt_kullanilacakSayi);
gunSayi = Double.toString(izinGun); // gunSayi is string value
kullanilan.setText(gunSayi);
String songunSayi=kullanilan.getText().toString();
I found a solution.
kullanilan.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) {
songunSayi=kullanilan.getText().toString();
}
});
}
I want to use the cursor position to do something, so write this code:
input =(EditText)findViewById(R.id.Input_EditText);
input.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s)
{
int index = input.getSelectionEnd();
if(index==0)
{
//do nothing
}
else
{
Toast.makeText(MainActivity.this, Integer.toString(index), Toast.LENGTH_SHORT).show();
}
}
});
But if you press the newline key, then press the Delete key, then the program will crash
Will the problem is where?
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())));
}
});
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.