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!
}
});
Related
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
}
}
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
}
}
});
I like to detect user's focus on the EditText using
IDEditTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Toast.makeText(getBaseContext(),
((EditText) v).getId() + " has focus - " + hasFocus,
Toast.LENGTH_LONG).show();
}
});
But when I set the cursor on it, onFocusChange is never get called. When I research, found that EditText xml should have
android:focusableInTouchMode="true"
android:focusable="true"
So I put these two lines in the EditText's xml, but still not called.
My intention is if the user focus on the EditText, I like to display the AlertDialog rather than keyboard, so that user's choice on the dialog will be displayed on the EditText. How can I implement that? Where can I have similar program for that implementation?
Thanks
I'm doing a app wich have two EditText. What I wanna do is, if you click on one and write on it, the other must erease anything It has and only shows the hint string. I'm triying doing it witha a check method that does:
if(celsius.isFocused()){
faren.setText(faren.getHint().toString());
}
if(faren.isFocused()){
celsius.setText(celsius.getHint().toString());
}
Then I call this method within the onCreate() method, but of course It only checks one time, and if use that checkMethod inside a loop, the app doesn't show anthing, It freezes. An suggestions?
Use the OnFocusChangeListener.
faren.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
celcius.setText(""); // This will automatically show the hint text
}
});
I have a question that How to check whether the widget having a focus or not. Actually I have a form in which there is a DOB edit text, a user navigates all the edit text boxes with Next action on keyboard and I want when user navigates to DOB field from any of the control then a Calendar Dialog automatically appears, currently what happens is user have to click on edit text then a Calendar Dialog appears, I want whenever DOB field gets a focus then it automatically call the Calendar Dialog.
I have searched regarding the same enough on web but failed to achieve this. Please help me out about this problem.
Thanks in advance.
Is it not
if(view.isFocussed) {}
?
1) Create a Handler, as a field in your Activity. Like this:
private Handler myHandler = new Handler();
2) When you create the ListView, add a OnFocusChangeListener, like shown below.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myHandler.postAtFrontOfQueue(new Runnable() {
public void run() {
myList.setSelection(0);
}
});
}
}
});