onFocusChange is not working for me - android

I want to disappear the keyboard when the focus goes out of the TextEdit box. I have throughly searched stackoverflow and google for an answer and have tried every single response/article I have seen but nothing seems to work for me.
As many, I am very new to Android development. I must be overlooking some minor detail to no avail.
My current implementation (again, I have tried many solutions) is as follows:
My class implements OnFocusChangeListener. OnCreate after calling super and setContentView I do:
EditText editText = (EditText) findViewById(R.id.text_box);<br> editText.setOnFocusChangeListener(this);
and
#Override
public void onFocusChange(View v, boolean hasFocus) { Log.d(TAG, "--> onFocusChange"); }
But I never see that Log message.
Sometimes I think that onFocusChange is not what I think it is. My idea is that when we touch the textedit field this view gets the focus, and when you touch any other area of the screen, say a button/listbox/etc the textedit losses the focus and onFocusChange should be called, but it is not, I never see the log entry.
Perhaps it is useful to clarify that I am using Android Studio and created an app that uses fragments.
So, I am doing this on the Detail Activity which in turn is part of a fragment. I have also tried to do it on the onActivityCreated of the fragment.
Neither approach works for me.
Any ideas what I can be missing here?
I would really appreciate your comments.

edittext.requestFocus();
or
Try this :
EditText editText = (EditText) findViewById(R.id.text_box);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus == true){
edittext.requestFocus();
}
else{
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
});

editText.setFocusableInTouchMode(true);
try doing that first?
You need to have the field be focusable before you can have the Focus Events fire on it. But i guess it didn't help you in this case :(

onFocusChangeListener is an interface which when you try to implement it as you did you also need to pass the context you wanna be Observed, so in practice one of the view elements of your ViewHolder must be Observed. so you can do it like:
(Assuming that you want to check when your EditText got focused)
editText.onFocusChangeListener = this
even if you are in an Adapter or ViewHolder while you are implementing OnFocusChangeListener you can assign that corresponding class to that Context you wanna be supervised in case of focus changes.

Related

Android button click twice to receive click event

I have set a button to focusable and focusableInTouchMode to true (I need it to be focusable). Now when I click the button, the onCilck event cannot be triggered (it is gaining focus and ignore onClick event), only when I cilck again the onClick event will be triggered.
I found a solutions like this:
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.performClick();
}
}
});
But this solution will cause some side effect as well, for example, if we click 'next' in the keyboard and jump focus to the button, onClick will fire. This is not what I want.
This issue is really anoying, is this a bug? Or it is intentional behaviour? Anyway to solve this?
set focusableInTouchMode false. Or just don't include that tag at all. Let it take its default value, whatever it is supposed to.

Android - Edittext getting focus after dialog

I am calling a Dialog in an OnClick function in the OnCreate. In that dialog the user of the app can add something to a external database.
Now my problem is that when the dialog is gone ( myDialog.dismiss() ) and it is returning to the actual activity, an EditText is getting focussed. My whole screen contains Spinners and TextViews, except for only 1 EditText.
The weird thing is that when the activity is first called, it stats on top of the activity (showing the first Spinner and not foucssing the EditText), but when the dialog is dismissed, EditText is focussed.
I've got android:windowSoftInputMode="adjustResize|stateHidden" in the manifest within the activity tags
And i have tryed to put android:focusableInTouchMode="true" in the EditText in the XML too, but that didn't work.
Can anyone help me find what i'm looking for?
Thanks in advance!
Android will focus the first focusable View from the top so you can try set something else focusable :)
Android focus is somewhat random, you can't really control it. You could try to disable the textview and enable it again when the dialog is gone, but I would advice against it.
To be honest, just let Android do it's thing. We have tried to work against the focus logic in a really big app and gave up after a lot of tries and convinced the customer it's not worth the hassle.
I've been having this same problem. Try:
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
hideSoftKeyboard();
}
});
private void hideSoftKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
This worked for everything except for a button in my dialog that has only an
onDismiss() method. Adding focusable=true and focusableInTouchMode=true to a TextView above my EditText (as Jeppe Andersen mentioned) solved this issue. Hope this helps.

Cannot input text for an Edittext view inside a Popupwindow?

I want to enter text from soft keyboard to an Edittext widget inside a popupwindow, but I'm struggling with some problems.
My activity is a game-card table, and enable some animations on that. In the gametable, there are some buttons. If player click on certain button, a popupwindow will be shown. The problem is player cannot input text to Edittext inside a popupwindow. I found some similar issues about this, and the most popular suggestion is :
Popupwindow.setFocusable(true)
With this setting, I can input normally to Edittext. However, this will block main UI thread and cause stop the animation in game table.I don't want this happen because it's bad for user experience.
It's possible to show softkeyboard when edittext has focus using below code snippet. However, I can not input anything from keyboard to edittext. Moreover, when I close popupwindow, the keyboard still show and not close.
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
InputMethodManager imm = (InputMethodManager)ct.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
}
});
Please help me find the way to input data to Edittext without blocking animation in mainUI thread. The alternative way is to use custom dialog but it no help. Using dialog cause blocking mainUI thread too.
Thank you so much for any suggestion.
If you don't want to block the UI try running this part of your code under a thread.
AsyncTask will be a good choice.
Check how to use AsyncTask here :-> http://developer.android.com/reference/android/os/AsyncTask.html
Good Luck

Way to hide softkeypad provided not effecting the features of the edittext in android

I have hidden soft keypad because I have custom keypad on the app. When the edittext is clicked, soft keypad shouldn't pop up. So, I have tried so many ways from the sources, but nothing worked except the editText.setFocusable(false); . But now the problem is edittext is not getting highlighted when I clicked it and even cursor is not visible. I have tried using InputManager, android:windowSoftInputMode="stateAlwaysHidden in the manifest and referred many like link 1 , link 2 etc., but these techniques atleast don't even hide the soft keypad on my app. Finally I got this through setFocusable, but there is a highlighting problem and cursor invisible problem and even requestFocus() in the onClickListener didn't work. Can someone give exact solution for this problem? Code snippet is appreciated.
Try this one in activity class
getwindow().setsoftInputMode(winowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This one is avoiding of soft key pad
please use this in manifest:
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden"
You ddont need to add any method in menifist. just add this code.. It will automaticlly hide when you click on button to get value.
Want to hide softkeyboard use this code in your click listener method.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFomWindow(edittext.getWindowToken(),0);
i hope this code work fine.
Try this:
InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFomWindow( edittext.getWindowToken(), 0);
how about if you editText.setOnTouchListener and when you create the new OnTouchListener do nothing something like:
editText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

Automatic popping up keyboard on start Activity

I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.
How can I prevent this?
Use this attributes in your layout tag in XML file:
android:focusable="true"
android:focusableInTouchMode="true"
As reported by other members in comments it doesn't works on ScrollView therefore you need to add these attributes to the main child of ScrollView.
You can add this to your Android Manifest activity:
android:windowSoftInputMode="stateHidden|adjustResize"
I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:
android:windowSoftInputMode="stateAlwaysHidden"
I think this is the easy way even if you are using fragments.
"stateAlwaysHidden" The soft keyboard is always hidden when the
activity's main window has input focus.
If you have another view on your activity like a ListView, you can also do:
ListView.requestFocus();
in your onResume() to grab focus from the editText.
I know this question has been answered but just providing an alternative solution that worked for me :)
Use this in your Activity's code:
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
https://stackoverflow.com/a/11627976/5217837 This is almost correct:
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE
I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:
public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
} else {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
}
}
In the onCreate() and in the onPause() of the activity for each EditText:
setEditTextFocus(myEditText, false);
For each EditText an OnTouchListener:
myEditText.setOnTouchListener(new EditText.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
setEditTextFocus(myEditText, true);
return false;
}
});
For each EditText in the OnEditorActionListener:
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
.......
setEditTextFocus(myEditText, false);
return false;
}
});
And for each EditText in the layout xml:
android:imeOptions="actionDone"
android:inputType="numberDecimal|numberSigned" // Or something else
There is probably more code optimizing possible.
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
I have found this simple solution that worked for me.Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Hope it will work for you .
this is the solution I am using, is not the best solution but it's working well for me
editComment.setFocusableInTouchMode(false);
editComment.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
editComment.setFocusableInTouchMode(true);
editComment.requestFocus() ;
return false;
}});
Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).
On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.
If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.
This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext"
android:nextFocusLeft="#id/autotext"/>
NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.
And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.
This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.
You can do this in your layout XML, no code required..
Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !
EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
Now keyboard is open enjoy :)
android:windowSoftInputMode="stateHidden|adjustResize"
Working fine
Add below code to your top of the activity XML and make sure the View is above EditText
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>
android:focusableInTouchMode="true"
Add the above line to xml of EditText or TextInputLayout which has focus and is causing the softInputKeyboard to pop up.
This solved the problem for us and now the keyboard doesn't popup
search_edit_text = (EditText) findViewById(R.id.search_edit_text);
search_edit_text.requestFocus();
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
This works for me guys fragment may have some different syntax . THIS WORKS FOR ACTIVITY
Use this in your Activity's code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
If your view has EditText and Listview then Keyboard will open up by default.
To hide keyboard from popping up by default do the following
this.listView.requestFocus();
Make sure you are requesting focus on listview after getting view for editText.
For e.g.
this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();
If you do it, then editText will get focus and keyboard will pop up.

Categories

Resources