onFocusChange of EditText never called - android

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

Related

EditText fields not clearing when focus is changed

I've got two EditText fields and a button,
EditText topNumericInputField; // Top text field
EditText bottomNumericInputField; // Bottom text field
Button clearButton; // Regular Button
I'm trying to clear the text that's in them when I change focus, my onClick method looks like this:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topInputField:
clearTextBoxes();
break;
case R.id.bottomNumericInputField:
clearTextBoxes();
break;
case R.id.clearButton:
clearTextBoxes();
break;
}
}
private void clearTextBoxes() {
topNumericInputField.getText().clear();
bottomNumericInputField.getText().clear();
}
Now, it clears both fields fine with either:
a) The field I click on already has focus
or
b) I click the clear button
If I change focus from the top field to the bottom field, it gets focus, then I need to click it one more time.
I'm not sure exactly what's happening, but I'm sure it's related to something that keeps coming up in the LogCat log, every time I change focus two lines appear:
Tag: IInputConnectionWrapper Text: beginBatchEdit on inactive InputConnection
Tag: IInputConnectionWrapper Text: endBatchEdit on inactive InputConnection
I've tried searching SO and I've seen some submissions that are similar, but none seem to really be what I'm looking for.
Thanks!
I think problem is not with setting text but problem lies in capturing wrong event.
As you said you want to clear edittext when focus is changed, then you should override onFocusChange(View v, boolean hasfocus) to capture that event. You need to implement FocusChangeListener.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean focus) {
if (focus) {
((EditText)v).setText("");
}
}
});
A simple implementation should look like above. Refer this for detailed information
You can set the text null.
topNumericInputField.setText("");
bottomNumericInputField.setText("");
You just need to clear your Text by:
topNumericInputField.setText("");
bottomNumericInputField.setText("");

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

Android - EditText in ListView and OnFocusChangeListener

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

OnClick event only works second time on edittext

I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.setCursorVisible(false);
edt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.
How do I fix this?
Simply try to add this to your XML file. Your keyboard pops up when widget gains focus.
So to prevent this behaviour set focusable to false. Then normal use OnClickListener.
<EditText
android:focusable="false"
...
/>
Now, it should works.
You can use onTouch instead of onClick, so it doesn't matter if the EditText has focus or not.
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
return false;
}
});
Nothing much to do you just have to
edt.setFocusable(false);
If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true
<EditText android:focusable="true" android:focusableInTouchMode="false" ... />
make your alert dialog box appear on
setOnFocusChangedListener()
You should add onFocusChangeListener:
edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edt.callOnClick();
}
}
});
edt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
Avoid using a FocusChangeListener since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener along with your OnClickListener like this:
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
This will cause your EditText to receive focus before your onClick call.
Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.
It maybe helpful to you.
This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for #neaGaze. This worked for me in my_layout.xml :
<EditText android:focusable="true" android:focusableInTouchMode="true" ... />
It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :
myEditText = (EditText) findViewById(R.id.myEditText);
...
// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//do something or nothing; up to you
}
});
Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard , How to close Android soft keyboard programmatically etc. None of that worked.
In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>

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