Android - EditText in ListView and OnFocusChangeListener - android

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
}
}
});

Related

Select all text when EditText has focus doesn"t work

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 pass the focus state to View's parent?

How can a view detect its child view's focus state? As you can see in this picture.
The parent view is a LinearLayout with a child view EditText. I wanna change the UI while the focus state changed(linearlayout turns red if focus on the edittext). But only the EditText can detect the focus state while the LinearLayout can not. Of cause I can listen the EditText's state using OnFocusChangedListrner. But I don't satisfy with it. Is there a simple way to make the parent view get the child view's focus event? The form has a lot of items.
I also think there is no other method than
onfocusChangeListner
If you have many edittext then you simply make one method with the parameter and inside it you write only one onfocuschangeListner like this
public void setColorToEditText(EditText editText) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
Log.e("userFocu", "userFocu");
if (!hasFocus) {
// code to execute when EditText loses focus
}
}
});

How to highlight text when edittext gets focused?

In my android app, I have a bunch of edititexts that's a numeric keyboard. On the keyboard is a next key, which when clicked moves the cursor to the next edittext. How can I make it highlight the text of the new focused edit text when the next key is clicked? Basically I want to just click next, then type some new numbers which replaces the old one.
Thanks.
Add this to your EditText on xml:
android:selectAllOnFocus="true"
editText.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus)
{
editText.setSelection(editText.getText().toString().length());
}
}
}
);

How to know when editText ends being editted?

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!
}
});

EditText focus lost

In a custom ListView, there are two columns, one contains a TextView and the other a EditText component.To enter some preferences, as the user clicks on the EditText, the software keyboard comes in focus but focus from the EditText is lost. How I can do this?
Focusable EditText inside ListView
For editText use method setFocusable(false).
For textView use setFocusable(true).
Also write a listener on focus lost for both textView and editText:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
//do job here when EditText loses focus
}
}
});

Categories

Resources