I have a problem with my code, it's crashing for me when I have more than 1 textChangeListener.
I think that the problem is with using "new TextWatcher" more than once but I don't know what to change it to.
I'm only starting to learn java and app development so the code and variables are a bit messy.
the code:
editMiles.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) {
strMiles = 0 + editMiles.getText().toString();
intMiles = Integer.parseInt(strMiles);
editKnots.setText("" + intMiles * 1.15078);
editKilometers.setText("" + intMiles * 1.852);
}
#Override
public void afterTextChanged(Editable s) {
}
});
editBeaufort.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) {
strBeaufort = 0 + editBeaufort.getText().toString();
intBeaufort = Integer.parseInt(strBeaufort);
editKnots.setText("");
editKilometers.setText("");
editMiles.setText("");
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
you have to get the input(CharSequence s) from your EditText's onTextChanged paramenter.so your code will be..
editMiles.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) {
strMiles = s.toString().trim();
intMiles = Integer.parseInt(strMiles);
editKnots.setText("" + intMiles * 1.15078);
editKilometers.setText("" + intMiles * 1.852);
}
#Override
public void afterTextChanged(Editable s) {
}
});
editBeaufort.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) {
strBeaufort = s.toString().trim();
intBeaufort = Integer.parseInt(strBeaufort);
editKnots.setText("");
editKilometers.setText("");
editMiles.setText("");
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
i hope it will work for you
you have to give the correct input for your calculation, if you want to input int, you have to parse string to int, or if you want to input as double, parse string to double and throw for exception
Related
I have edited the text and list of results obtained from the search. Now I want to set first result for edittext, it looks like below
this is code i use to get result search:
bindingNV.etSearch.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().length()>0){
new SearchFlowWord(new NotifyDataChangeListener() {
#Override
public void dataChange(List<String> mList) {
if (mList.size()>0) {
resultSearch = mList.get(0);
}else {
resultSearch ="";
}
}
}, s);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
How i can set edittext like image
when typing edit text yoyo animation keep run how to keep typing show animation Once ?
text_send.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) {
YoYo.with(Techniques.SlideInUp)
.duration(300)
.repeat(0)
.
playOn(btn_send);
btn_send.setImageResource(R.drawable.ic_send);
when typing edit text yoyo animation keep run how to keep typing show animation Once ?
if you want to show it only once when the user starts typing then use a bool to do so
boolean is_show = false;
text_send.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) {
if(!is_show){
is_show = true;
YoYo.with(Techniques.SlideInUp)
.duration(300)
.repeat(0)
.
playOn(btn_send);
}
btn_send.setImageResource(R.drawable.ic_send);
or you asking something else?
I have two EdiText and when I write a number in the first one I would need the second to be set to 162 - first one. If it is necessary to re-type a second number, the component should recalculate the first number.
If I write something in the second the first must behave exactly like the second.
Below there is my code but it does not work:
inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);
View.OnClickListener inputScoreListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
int inputScoreWeInteger = Integer.parseInt(inputScoreWe.getText().toString());
int inputScoreYouInteger = Integer.parseInt(inputScoreYou.getText().toString());
if (inputScoreWeInteger > 0) {
inputScoreYouInteger = 162 - inputScoreWeInteger;
} else if (inputScoreYouInteger > 0) {
inputScoreWeInteger = 162 - inputScoreYouInteger;
}
String s1 = inputScoreWeInteger + "";
String s2 = inputScoreYouInteger + "";
inputScoreWe.setText(s1);
inputScoreYou.setText(s2);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
};
inputScoreWe.setOnClickListener(inputScoreListener);
inputScoreYou.setOnClickListener(inputScoreListener);
Use text change listener to trigger for everytime you change value
inputScoreWe.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){
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString())+"");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
replace your code with code as below
inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);
inputScoreWe.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) {
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString()) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputScoreYou.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) {
inputScoreWe.setText(162 - Integer.parseInt(inputScoreWe.getText().toString()) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
try this
brandET = findViewById(R.id.addCar_brand);
modelET = findViewById(R.id.addCar_model);
brandET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
brandChange = hasFocus;
}
});
modelET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
modelChange = hasFocus;
}
});
brandET.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 (brandChange && count > 0) {
int dataFromBrand = Integer.parseInt(s.toString());
modelET.setText((162 - dataFromBrand) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
modelET.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 (modelChange && count > 0) {
int dataFromModel = Integer.parseInt(s.toString());
brandET.setText((162 - dataFromModel) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
here brandET and modelET is your two edit text...and...brandChange and modelChange are two global boolean data
I have problem with edit text.I want if edit text are six characters go to next actitvity. I write this code but if write six characters don't go next activity.
this my code
smsCode.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.toString().trim().length()== 6){
Intent intent = new Intent(SmsCodeActivity.this,ScanQrCodeActivity.class);
startActivity(intent);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
smsCode.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(smsCode.getText().toString().trim().length()== 6){
Intent intent = new Intent(SmsCodeActivity.this,ScanQrCodeActivity.class);
startActivity(intent);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Try doing your work in onTextChanged
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (smsCode.getText().toString().trim().length()== 6){
Intent intent = new Intent(SmsCodeActivity.this,ScanQrCodeActivity.class);
startActivity(intent);
}
}
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())));
}
});