[enter image description here][1]I have eight EditText, five for Income & three for deduction and I have set a TextWatcher at all EditText by calling addTextChangedListener().
Now I have set the sum of first five EditText to TextView(value) and sum of other three EditText to TextView(ded_value). As whichever EditText is going to fill likewise sum is setting on respective TextView.
These TextView are showing the only total earning & total deduction and now I m am trying to set the net payable salary which will be difference of value,ded_value(means total earning-total deduction=net payable salary).
And the result of this defference should be diaplay on another TextView that is netsalaryvalue. As whichever(value,ded_value) TextView is going to changing likewise difference should be set on TextView(netsalaryvalue).
enter code here //this is income salry & deduction salary code
textWatcher=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) {
//income code value
b_salary=et_bs.getText().toString();
d_allowance=et_da.getText().toString();
hr_allowance=et_hra.getText().toString();
t_allowance=et_ta.getText().toString();
m_allowance=et_ma.getText().toString();
//deduction code value
itax=et_itax.getText().toString();
pf=et_pf.getText().toString();
tds=et_tds.getText().toString();
if (!et_bs.getText().toString().isEmpty())
{
value.setText("");
value.setText(b_salary);
if (!et_da.getText().toString().isEmpty())
{
value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)));
if (!et_hra.getText().toString().isEmpty())
{
value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)+Integer.parseInt(hr_allowance)));
if (!et_ta.getText().toString().isEmpty())
{
value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)+Integer.parseInt(hr_allowance)+Integer.parseInt(t_allowance)));
if (!et_ma.getText().toString().isEmpty())
{
value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)+Integer.parseInt(hr_allowance)+Integer.parseInt(t_allowance)+Integer.parseInt(m_allowance)));
}
}
}
}
}
if (!et_itax.getText().toString().isEmpty())
{
ded_value.setText("");
ded_value.setText(itax);
if (!et_pf.getText().toString().isEmpty())
{
ded_value.setText(String.valueOf(Integer.parseInt(itax)+Integer.parseInt(pf)));
if (!et_tds.getText().toString().isEmpty())
{
ded_value.setText(String.valueOf(Integer.parseInt(itax)+Integer.parseInt(pf)+Integer.parseInt(tds)));
}
}
}
}
};
et_bs.addTextChangedListener(textWatcher);
et_da.addTextChangedListener(textWatcher);
et_hra.addTextChangedListener(textWatcher);
et_ta.addTextChangedListener(textWatcher);
et_ma.addTextChangedListener(textWatcher);
et_itax.addTextChangedListener(textWatcher);
et_pf.addTextChangedListener(textWatcher);
et_tds.addTextChangedListener(textWatcher);
// this is net payable salry code
textWatcher2=new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
netsalaryvalue.setText("");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
totalincome=value.getText().toString();
totaldedu=ded_value.getText().toString();
if (!value.getText().toString().equals("")||!ded_value.getText().toString().equals(""))
{
if (!value.getText().toString().equals("00.00")||!ded_value.getText().toString().equals("00.00"))
{
netsalaryvalue.setText(String.valueOf(Integer.parseInt(totalincome)-Integer.parseInt(totaldedu)));
}
}
}
};
value.addTextChangedListener(textWatcher2);
ded_value.addTextChangedListener(textWatcher2);
strong text When I was finding only the sum then my code was running freely but when I had write the code for netpayablesalry then my app is going to crash. Now I m trying to resolve this issue but still now issue is not resolve, please help me.
strong text According to my question exact answer is:-
enter code here public void afterTextChanged(Editable s) {
basic=et_bs.getText().toString();
allowance=et_da.getText().toString();
value.setText(basic);
if (!et_da.getText().toString().isEmpty())
{
value.setText(""+(Integer.parseInt(basic)+Integer.parseInt(allowance)));
}
}
strong text now it is working which was I trying.
Do it like this
textWatcher=new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
value.setText("");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
basic=et_bs.getText().toString();
allowance=et_da.getText().toString();
if(!basic.isEmpty() && !allowance.isEmpty())
value.setText(""+(Integer.parseInt(basic)+Integer.parseInt(allowance)));
}
#Override
public void afterTextChanged(Editable s) {
}
};
you can also add these three lines in onAfterTextChanged
#Override
public void afterTextChanged(Editable s) {
basic=et_bs.getText().toString();
allowance=et_da.getText().toString();
if(!basic.isEmpty() && !allowance.isEmpty())
value.setText(""+(Integer.parseInt(basic)+Integer.parseInt(allowance)));
}
You could try this
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
basic=et_bs.getText().toString();
allowance=et_da.getText().toString();
////Check for null/empty/Not-Integer value
int first = 0;
int second = 0;
try {
first = Integer.parseInt(basic);
second = Integer.parseInt(allowance);
}
catch(Exception e ) {
}
value.setText(""+String.valueOf(first+second)));
}
Related
I am working in an Android studio project. Here I want to implement one searching system. I have an edittext and an imagebutton. When user writers something in edittext and press imagebutton, system shows the relevant data from some database. This much I have covered.
Moreover, I want to implement a system where while user writes at least a specific length of letters (say 3) in edittext, the searching will start automatically. With more adding of letters the searching will be filtered accordingly. Is it possible to do this? Or something similar to this?
on searchview there is this syntax. just check the length of the text
public void onQueryTextChange(String query) {
if(query.length() >= 3) {
searchStarts();
}
}
or if you dont use search view just use textwatcher
textWatcher = 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 (timer != null) {
timer.cancel();
}
}
#Override
public void afterTextChanged(Editable s) {
if(s.length() > 3){
searchStarts();
}
};
you have to add text watcher to your edit text. I will show you the solution with using debounce to avoid updating list all time user type a text.
So lets start with adding textWatcher to your edittext field:
searchField.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) {
//this one is for all letters, you can check the s length eg if(s.length() >= 3)
searchSubject.onNext(s.toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
This solution use rxjava if you do not want to do it just replace searchSubject with calling your search method.
So the fields:
private PublishSubject<String> searchSubject = PublishSubject.create();
private Subscription searchSubscription;
private String currentSearchPrefix = "";
And subscription (call this method onResume):
/**
* Subscribe to searchSubject to update list of items depends on given prefix.
* Debounce on changes 500 milliseconds
*/
private void subscribeSearch() {
searchSubscription = searchSubject
.debounce(500, TimeUnit.MILLISECONDS)
.onBackpressureLatest()
.subscribe(result -> {
currentSearchPrefix = result;
MainActivity.this.runOnUiThread(this::refreshList);
});
}
My method refreshList just update list by filter it using currentSearchprefix by one of field. Remember to unsubscribe searchSubscription onPause().
I have an EditText and a TextWatcher. while testing in our test device we never found StackOverflowError, but once we published our app in Google Play Store, we are getting StackOverflowError issue for some user. Why this is happening, I go through some of link but not got the perfect answer. Is anything need to be done in my code.
Skeleton of my code:
weightEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
m_currentWeight = weightEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s)
{
if(!weightEditText.getText().toString().equals("")) {
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2}(\\.[0-9]{0,2}?)?)?$");
Matcher matcher = mPattern.matcher(s.toString());
if (!matcher.find()) {
weightEditText.setText(m_currentWeight);
weightEditText.setSelection(weightEditText.getText().length());
}
}
}
});
To avoid recursion here you need to unregister your textWatcher before setting the text and then reregister it.
Declare the TextWatcher outside the addTextChangedListener(...) method. Then you can do weightEditText.removeTextChangedListener(mWatcher) and weightEditText.addTextChangedListener(mWatcher)
You are trying to call setText() inside of the text watcher which will produce an infinite loop. You can use a flag variable to avoid this.
status variable is set as false by defaut.
status variable indicates whether the TextChange is made by App itself or by the user himself. if it is true, then the TextChange is made by App itself and vice versa.
Try this code. Cheers ! :)
public class MainActivity extends AppCompatActivity {
boolean status=false;//global variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weightEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
m_currentWeight = weightEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s)
{
if(status){
status=false;
return;
}else{
status=true;
if(!weightEditText.getText().toString().equals("")) {
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2}(\\.[0-9]{0,2}?)?)?$");
Matcher matcher = mPattern.matcher(s.toString());
if (!matcher.find()) {
weightEditText.setText(m_currentWeight);
weightEditText.setSelection(weightEditText.getText().length());
}
}
}
}
});
}
}
How can i hide a TextInputLayout error after input one text in EditText.
Is it possible?
how can i achieve this or I am doing something wrong here.!!
code
layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone);
layoutEdtPhone.setErrorEnabled(true);
layoutEdtPhone.setError(getString(R.string.ui_no_phone_toast));
layoutEdtPassword = (TextInputLayout)rootView.findViewById(R.id.layoutEdtPassword);
layoutEdtPassword.setErrorEnabled(true);
layoutEdtPassword.setError(getString(R.string.ui_no_password_toast));
edtPhone=(EditText)rootView.findViewById(R.id.edtPhone);
edtPassword=(EditText)rootView.findViewById(R.id.edtPassword);
xml
<EditText
android:id="#+id/edtPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/edt_background_selector"
android:drawableLeft="#drawable/phone_icon"
android:drawableStart="#drawable/phone_icon"
android:hint="#string/phone"
android:inputType="phone"
android:padding="5dip"
android:singleLine="true"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/layoutEdtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/edtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#drawable/edt_background_selector"
android:drawableLeft="#drawable/password_icon"
android:drawableStart="#drawable/password_icon"
android:hint="#string/password"
android:inputType="textPassword"
android:padding="5dip"
android:singleLine="true"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
To illustrate further the answer given by Prithviraj, TextInputLayout does not do the validation itself. It is just a mechanism to show the error or hint. You are responsible for setting/clearing the error. Here is how you can do that. Note that in addition to TextChangedListener, you may also need OnFocusChangeListener to set the error when user jumps to second edit text without doing any modification in the first field.
protected void onCreate(Bundle savedInstanceState) {
//.....
edtPhone.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) {
validateEditText(s);
}
});
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
validateEditText(((EditText) v).getText());
}
}
});
}
private void validateEditText(Editable s) {
if (!TextUtils.isEmpty(s)) {
layoutEdtPhone.setError(null);
}
else{
layoutEdtPhone.setError(getString(R.string.ui_no_password_toast));
}
}
}
You can set layoutEdtPhone.setErrorEnabled(false);
Do
mTextInputLayout.setError(null);
to clear the error message.
A good practice can be a method to check for errors like this:
#Override
public void checkErrorBeforeAction() {
boolean error = false;
mTextInputLayout.setError(null);
if (mEditText.getText().toString().length == 0)) {
mTextInputLayout.setError("Field empty");
}
else if (mEditText.getText().toString().isValid) { // Other condition
mTextInputLayout.setError("Field is invalid");
}
if (!error) {
// Call action
}
}
This way it refreshes the error message before setting a new one.
I used #TextChanged of ButterKnife and worked for me, look:
#Bind(R.id.layoutEdtPhone)
TextInputLayout tlayoutEdtPhone;
#Bind(R.id.edtPhone)
EditText edtPhone;
//start ButterKnife (I spent the URL with full description for initilize)
#OnTextChanged(R.id.edtPhone)
public void changedTextOnEditPhone() {
tlayoutEdtPhone.setError("");
}
If you want know about ButterKnife, I wrote a post with more detail, but it was done in my native language, that is, pt_br. http://blog.alura.com.br/aumentando-a-produtividade-com-butter-knife-no-android/
textInputLatout.getEditText().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() < 1) {
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Please enter a value");
}
if (s.length() > 0) {
textInputLayout.setError(null);
textInputLayout.setErrorEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Sadly there is no built-in mechanism to achieve this behavior. I created ViewUtils class that helps me:
public final class ViewUtils {
private ViewUtils() {}
public static void resetTextInputErrorsOnTextChanged(TextInputLayout... textInputLayouts) {
for (final TextInputLayout inputLayout : textInputLayouts) {
EditText editText = inputLayout.getEditText();
if(editText != null) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
}
#Override
public void afterTextChanged(final Editable s) {
if(inputLayout.getError() != null) inputLayout.setError(null);
}
});
}
}
}
}
And then you can easily use it in the client code:
ViewUtils.resetTextInputErrorsOnTextChanged(mEmailTextInputLayout, mPasswordTextInputLayout);
Using KTX:
textInputLayout.editText?.doOnTextChanged { text, start, count, after ->
if (text?.any(invalidCharacters.toCharArray()::contains) == true) {
textInputLayout.error = "Invalid character entered: ${invalidCharacters}"
} else {
textInputLayout.error = null
}
}
This is how I've done it using a custom class that extends TextInputLayout. Uses a TextWatcher like the other answers, but there's no need to manually do anything after using this in the xml. It takes care of everything for you.
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context) {
super(context);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
initRemoveErrorWatcher();
}
private void initRemoveErrorWatcher() {
EditText editText = getEditText();
if (editText != null) {
editText.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) {
setError(null);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
}
To use, simply replace TextInputLayout in your xml with this:
<com.example.customViews.CustomTextInputLayout>...
If you want to remove the error display from TextInputLayout use:-
YourtextInputLayout.setErrorEnabled(false);
else
If you want to remove error display from edittextfield use:-
edittext_id.setError(null);
I suggest using Textwatcher fucntion in Android for more efficient validation handling..
eg:
editText.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) {
if (!validatefName()) {
return;
}
}
});
Removes error on touch
listOf(binding.userName, binding.password).forEach {
it.setOnTouchListener { v, _ ->
model.error.set(null)
v.performClick()
false
}
}
use textwatcher here's the link http://www.learn2crack.com/2014/02/android-textwatcher-example.html
Use a if else condition,if condition is true set TextInoutLayoutObj.setError(null); if its false set your error there
I know a little bit about TextWatcher but that fires on every character you enter. I want a listener that fires whenever the user finishes editing. Is it possible? Also in TextWatcher I get an instance of Editable but I need an instance of EditText. How do I get that?
EDIT: the second question is more important. Please answer that.
First, you can see if the user finished editing the text if the EditText loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText instance within the TextWatcher only if you have declared the EditText as an instance object. Even though you shouldn't edit the EditText within the TextWatcher because it is not safe.
EDIT:
To be able to get the EditText instance into your TextWatcher implementation, you should try something like this:
public class YourClass extends Activity {
private EditText yourEditText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
// yourEditText...
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
Note that the above sample might have some errors but I just wanted to show you an example.
It was bothering me that implementing a listener for all of my EditText fields required me to have ugly, verbose code so I wrote the below class. May be useful to anyone stumbling upon this.
public abstract class TextChangedListener<T> implements TextWatcher {
private T target;
public TextChangedListener(T target) {
this.target = target;
}
#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) {
this.onTextChanged(target, s);
}
public abstract void onTextChanged(T target, Editable s);
}
Now implementing a listener is a little bit cleaner.
editText.addTextChangedListener(new TextChangedListener<EditText>(editText) {
#Override
public void onTextChanged(EditText target, Editable s) {
//Do stuff
}
});
As for how often it fires, one could maybe implement a check to run their desired code in //Do stuff after a given a
Anyone using ButterKnife. You can use like:
#OnTextChanged(R.id.zip_code)
void onZipCodeTextChanged(CharSequence zipCode, int start, int count, int after) {
}
I have done it using AutotextView:
AutotextView textView = (AutotextView) findViewById(R.id.autotextview);
textView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
seq = cs;
}
#Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
new SearchTask().execute(seq.toString().trim());
}
});
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
myOutputBox.setText(s);
}
});
TextWatcher didn't work for me as it kept firing for every EditText and messing up each others values.
Here is my solution:
public class ConsultantTSView extends Activity {
.....
//Submit is called when I push submit button.
//I wanted to retrieve all EditText(tsHours) values in my HoursList
public void submit(View view){
ListView TSDateListView = (ListView) findViewById(R.id.hoursList);
String value = ((EditText) TSDateListView.getChildAt(0).findViewById(R.id.tsHours)).getText().toString();
}
}
Hence by using the getChildAt(xx) method you can retrieve any item in the ListView and get the individual item using findViewById. And it will then give the most recent value.
As far as I can think bout it, there's only two ways you can do it. How can you know the user has finished writing a word? Either on focus lost, or clicking on an "ok" button. There's no way on my mind you can know the user pressed the last character...
So call onFocusChange(View v, boolean hasFocus) or add a button and a click listener to it.
The Watcher method fires on every character input.
So, I built this code based on onFocusChange method:
public static boolean comS(String s1,String s2){
if (s1.length()==s2.length()){
int l=s1.length();
for (int i=0;i<l;i++){
if (s1.charAt(i)!=s2.charAt(i))return false;
}
return true;
}
return false;
}
public void onChange(final EditText EdTe, final Runnable FRun){
class finalS{String s="";}
final finalS dat=new finalS();
EdTe.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {dat.s=""+EdTe.getText();}
else if (!comS(dat.s,""+EdTe.getText())){(new Handler()).post(FRun);}
}
});
}
To using it, just call like this:
onChange(YourEditText, new Runnable(){public void run(){
// V V YOUR WORK HERE
}}
);
You can ignore the comS function by replace the !comS(dat.s,""+EdTe.getText()) with !equal function. However the equal function itself some time work not correctly in run time.
The onChange listener will remember old data of EditText when user focus typing, and then compare the new data when user lose focus or jump to other input. If comparing old String not same new String, it fires the work.
If you only have 1 EditText, then u will need to make a ClearFocus function by making an Ultimate Secret Transparent Micro EditText 😸 outside the windows 😽 and request focus to it, then hide the keyboard via Import Method Manager.
In Kotlin Android EditText listener is set using,
val searchTo : EditText = findViewById(R.id.searchTo)
searchTo.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
// you can call or do what you want with your EditText here
// yourEditText...
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
})
I have taken the solution from #RNGuy thanks for that!
And changed the listener a bit so now it will only accept integers by updating the textView.
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public abstract class NumberChangedListener implements TextWatcher {
private final EditText target;
private final String defaultValue;
public NumberChangedListener(EditText target, int defaultValue) {
this.target = target;
this.defaultValue = defaultValue + "";
}
#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) {
this.onTextChanged(target, s);
}
private void onTextChanged(EditText target, Editable s) {
String input = s.toString();
String number = input.replaceAll("[^\\d]", "");
if (!number.equals(input)) {
target.setText(number);
return;
}
Integer integer;
try {
integer = Integer.valueOf(number);
} catch (NumberFormatException ignored) {
target.setText(defaultValue);
return;
}
if (!integer.toString().equals(number)) {
target.setText(integer.toString());
return;
}
onNumberChanged(integer);
}
public abstract void onNumberChanged(int value);
}
and use as
int defaultVal = 10;
mTextView.addTextChangedListener(new NumberChangedListener(mTextView, defaultVal) {
#Override
public void onNumberChanged(int value) {
// use the parsed int
}
});
I have a listview with products in it. I override the tostring method of the products:
#Override
public String toString() {
return this.getNaam();
}
I add addTextChangedListener to my EditText.
tvZoek.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString().toLowerCase());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
When I do a search the list is filtered. But for example if I search for "Bio" and there are 5 product in the list with "Bio" in their name, than it is showing first 5 products in the list, not the 5 products with "Bio" in their name.
I didn't override the getFilter() method in the adapter.
How do I show the correct products? (I'm working with ArrayAdapter)
try the following.
public void onTextChanged(CharSequence s, int start, int before, int count) {
updateList(s.toString);
}
public void updateList(String filter) {
List<> tempList = new ArrayLsit<>();
int yourListSize = myList.count();
for (int i = 0; i < myListSize; i++) {
if (filter != null) {
if (myList.get(i).contains(filter)) {
tempList.add(myList.get(i));
}
}
}
// create adapter using tempList
// setAdapter
}