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
Related
so i have edittext & textview. edittext for NIK & textview for NAMA. the concept is when I have finished typing the NIK field, then at that moment the name data (NAMA) appears in the textview automatically based on database. I still confused how to do it properly.
Database Structure
NIK
NAMA
96296
Farrasta
94878
Alfian
Edit Text
etNik.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) {
String data = etNik.getText().toString();
if (data == NikKry){
getNama();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Function for get NAMA
private void getNama(){
APIRequestData armNama = RetroMaster.konekRetrofit().create(APIRequestData.class);
Call<List<DataMaster>> tampilNama = armNama.ardGetNama(NikKry);
tampilNama.enqueue(new Callback<List<DataMaster>>() {
#Override
public void onResponse(Call<List<DataMaster>> call, Response<List<DataMaster>> response) {
if (response.isSuccessful()) {
tvNama.setText(response.body().get(0).getNAMA());
}
}
#Override
public void onFailure(Call<List<DataMaster>> call, Throwable t) {
Toast.makeText(TambahActivity.this, "Gagal "+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
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 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 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