Android: Call function if EditText has not been changed in x seconds - java

I have this app I'm working on, which sends requests to a server whenever text changes in the EditText. And it works fine as it is.
The problem is, that if I send a request every time the edittext changes, it will be a lot of requests in the end. And I don't want to use a "send" button.
I want something the works something like this:
If the user types in the edittext, some timer goes on. And if the timer reaches x seconds, it executes the request. But the timer resets if the user types again.
In this way, the request is only sent, once the user has stopped typing.
You might know some better way, and I'd be glad to hear it.
Thanks in advance :D

One method would be to use myEditText.addTextChangedListener(...) and use a TextWatcher to know when the text changes.
Then in the onTextChanged() method you can post a delayed runnable using a Handler to send the current text to your server if they don't type anything further.
Here is what this roughly looks like:
private Handler handler = new Handler();
private Runnable postToServerRunnable = new Runnable() {
#Override
public void run() {
// TODO: PUT CODE HERE TO HANDLE CURRENT VALUE OF EDIT TEXT AND SEND TO SERVER
}
};
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// remove existing callback (timer reset)
handler.removeCallbacks(postToServerRunnable);
// 500 millisecond delay. Change to whatever delay you want.
handler.postDelayed(postToServerRunnable, 500);
}
};

There is this is method called EditText.edittext.addtextchangedlistener . add this listener to your edittext and override the methods like this.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
called after the The complete Text is changed// this is what i think you should use
}
I hope this is helpful . ThankYou.

Related

How do I run a method once a user has confirmed their input?

Im making a program where a user has to search through some of my data.
I've created an edit view for the user to put their text in to, but I want to run a method once the user has confirmed their input.
How do I do this?
Thank you.
If you are trying to add a search feature , why not use a search view instead.
there are many options to do this, i am writing 3 options below.
1 - If you have confirm button then use its onclicklistener for you method call.
2 - If you don't have confirm button then add focusChangeListener in edittext and call you method when edittext focus is lost.
3 - You can use a timerTask like when user stopped typing for 2 second (we assume that user input is done) you can call your method
Example:
edt_search.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) {
// user is typing: reset already started timer (if existing)
if (timer != null) {
timer.cancel();
}
}
#Override
public void afterTextChanged(final Editable s) {
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Search Logic
if (!s.toString().trim().isEmpty())
// Your Function call
}
}, 500); // 350ms delay before the timer executes the "run“ method from TimerTask
}
}

user input run time android

I am trying to build a simple converter code(convert feet to mtr) in Android. I am able to do this but only when user clicks some button. Now I want to modify it such that it starts to convert as and when the user gives input(Something like google converter). Is there any way to do this in Android?
Thanks in advance.
Add the listener to your edittext:
yourEditText.addTextChangedListener(addTextWatcher);
Add the TextWatcher interface:
private TextWatcher addTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
// here is where you could grab the contents of the edittext
// input each time a character is entered, and pass the value
// off to your unit conversion code. Careful to check for
// numerals/decimals only, or to set the proper inputType in
// your xml.
}
#Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable sequence) {
}
};

MultiAutocompleteTextView events

I'm trying to work with a MultiAutocompleteTextView in such way that whenever I type a character in the textview, an event will be fired OR after 2-3 elements have been typed, fire the event again.
The reason I work with multiautocomplete is because I also need the autocompletion feature .
Is there such an event that can be triggered after every character or 2-3 characters typed? Thanks!
Yes you can use add a TextWatcher and TextChangedListener to your edit Text like this:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//check count or count in edit text and do something
}
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
http://developer.android.com/reference/android/text/TextWatcher.html

Calling an external function from within the oncreate function (Android)

This is my first time asking a question here, as most of the time I find the answer I need within these forums. However I've been searching for ages and have had no luck so thought I would just ask.
My issue is when I call an external function it throws an error "The method processInput()is undefined for the type new TextWatcher(){}" my code is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText myTextBox = (EditText) findViewById(R.id.screenDisplay);
myTextBox.addTextChangedListener(new TextWatcher() {
// Not used but required for method to work correctly
public void afterTextChanged(Editable s) {}
// Not used but required for method to work correctly
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
// Processing input as entered into screen
public void onTextChanged(CharSequence s, int start, int before, int count)
{
processInput();
}
});
}
and the external method I'm trying to call is processInput(){}. The code for the function wrapper is shown below:
public void ProcessInput(String input){}
Hopefully its not something silly like a missing bracket that I'm overlooking,
Thank you in advance.
processInput();
instead try some thing like this. Make sure function name starts with small letter.
public void onTextChanged(CharSequence s, int start, int before, int count)
{
ProcessInput(s.toString());
}

KeyReleased equivalence in Android

On PC I can add a onKeyListener for a JTextField to listen keyReleased event. On Android I've used addTextChangedListener.
I have two EditText fields in my Android application. Editing one will affect the other. This will cause the program to fail in stack overflow error.
How can I listen for the phone's keyboard instead of changes in the EditText field? I don't want the program to invoke the listener because of the infinite loop caused by the listener.
Attach a onFocusChangedListener and add the TextChangedListener when a EditText has focus and remove it when it loses focus.
Something like this:
EditText1.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText) v).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) {
// affect EditText2
}
});
}
if(!hasFocus){
((EditText) v).removeTextChangedListener();
}
}
});
}
});
The same for EditText2
First of all, I would create one text change listener, something like SynchronizingWatcher and attach it to both EditTexts. Then, when you receive a text change event, before updating other text edits, just unregister old listeners, update text and enable listeners again:
class SynchronizingWatcher implements TextWatcher {
Set<EditText> synchronizedViews = new HashSet<EditText>();
public void watchView(EditText view) {
view.addTextChangedListener(this);
synchronizedViews.add(view);
}
public void afterTextChanged(Editable s) {
for (EditText editText : synchronizedViews) {
editText.removeTextChangeListener(this);
editText.setText(s); // Of course you can do something more complicated here.
editText.addTextChangeListener(this);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Don't care.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Don't care.
}
}
...
// Somewhere in your activity:
SyncrhonizingWatcher synchronizingWatcher = new SynchronizingWatcher();
synchronizingWatcher.watchView(myEditText1);
synchronizingWatcher.watchView(myEditText1);
Another solution: provide your own KeyListener that decorates existing KeyListener (you can get existing key listener with editText.getKeyListener() and set your decorator with editText.setKeyListener(). Your decorator would also update other edit texts in onKeyUp(). But I would try to stay away from messing with that stuff.

Categories