I build an android application. I have an EditText. I want to save changes (or anything else) automatically after the user change the text.
Now I use
editText.addTextChangedListener(textWatcher);
and
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
...
}
But it saves the changes after every little change (add \ remove a letter), and I want that it'll be saved only after the user finished (closed the keyboard, clicked on a different edittext, etc. ).
How can I do that?
Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control and you should save the data of editext. for example
EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
//SAVE THE DATA
}
}
});
Implement setOnFocusChangeListener for all EditTexts and you should handle Back Press event too. If the user change the data of edittext and didn't goto another EditText and pressed back then edited vallue will not save. So should use onKeyDown or onBackPressed too
As Kotlin is now official android language:
var editText = findViewById<EditText>(R.id.editText)
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
//SAVE THE DATA
}
}
Related
When an EditText has a focus, I want to select all text inside by default, but it doesn't work:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
// Go to the end
editText.setSelection(getEditTextView().getText().length());
// Select all the content
editText.selectAll();
}
}
});
Thank you very much guys!
There are 2 good way to select the text in an EditText :
Inside your main.xml :
android:selectAllOnFocus="true"
Or :
editText.setSelectAllOnFocus(true);
(If you want to do it programatically)
SOURCE : Select all text inside EditText when it gets focus
How to disable EditText searchview after completion of input on fragment?
You can set View.OnFocusChangeListener to your editText and after focus change call setEnabled(false). for eg.
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
//let the user input
} else {
//disable you search view
mEditText.setEnabled(false);
}
}
});
P.S. -If you are still facing a problem, please update your code snippet.
If user have to click button after enter text into searchview, you can handle this in event button click method. In your button click method you can write:
search.setEnabled(false);
How show hint text when open edittext in fullscreen mode?
I'm can show hint text with OnFocusChangeListener.
editChangeName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
editChangeName.setHint(b ? getResources().getString(R.string.name) : "");
}
});
But.
How hide hint text after closing edittext?
// assuming you have a EditText view with id
View viewById = getView().findViewById(R.id.editTextId);
if(viewById!=null) {
// set listener to this view "many views could share one listener"
viewById.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// if view match id && has no focus
if(v.getId() == R.id.editTextId && !hasFocus)
// clear hint
((EditText) v).setHint(null);
// else show hint
}
});
}
hasFocus <- as name says
has (true) -> show
doesn't have (false) -> hide
if you are not sure if view by id is one you assign to an edit text do a check ( double check will not hurt)
if (EditText.class.isAssignableFrom(v.getClass()))
// do cast
EditText editText = (EditText) v;
This code is correct. But. How hide hinttext, after closing softkeyboard.. edittext have focus – Eugenu Yansky
Detecting when user has dismissed the soft keyboard
How to remove focus without setting focus to another control?
How to clear focus and remove keyboard on Android?
I have two editTexts and when the user edits one and then hits done, it executes a method. I want to be able to know when the user stops editing it. Like in that case, with hitting the "done" button. Unfortunately, the user can 'stop' editing it, if he/she selects the other editText. For some strange reason, the OnEditorActionListener doesn't catch that case. What can I do about it? I've tried with onFocusChange, but that one is very unpredictable...
You can assume when the EditText looses focus
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//probably here!
}
});
I have a ListView that contains an EditText widget in each row. The user can update the numeric value in the EditText, and once the focus is lost on that EditText, I want a TextView updated on the screen that contains the sum. I'm using the below code to check if the focus has been lost, but it every time I put focus on an EditText, it enters the if statement, and I think that has to do with the fact it is contained within a ListView. I'd like it to only enter the if statement when the user removes the focus from the EditText.
MyEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
//Perform Calculation
}
}
});