Cannot input text for an Edittext view inside a Popupwindow? - android

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

Related

onFocusChange is not working for me

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.

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.

How to close soft keyboard in fragment

I have an EditText inside a fragment, which is in itself inside an actionbarsherlock tab. When I touch inside the EditText box a soft keyboard appears with one of the keys having a magnifying glass (search) icon. When I type some text and click on the search key I can process the typed-in-string in my onEditorAction, but the soft keyboard remains on display. How can I close it programatically?
By the way if one answer is that I could configure some setting for EditText such that it closes automatically on search, I would still like to know if the soft keyboard can be closed with a method call as I also have my own search button on screen (nothing to do with the soft keyboard) and I would like the soft keyboard to close when that's pressed too.
Note: Before anyone rushes to claim this question is a repeat of a previous question, I have seen many Q&A's about hiding the soft keyboard at various points. Many of the answers seem inordinately complicated and in many it is not clear whether the idea is to permanently hide the keyboard or just just temporarily close it till the user taps on an EditText field again. Also some answers require calls to methods not available in fragments.
In my fragments I close the keyboard simply in this way:
public static void closeKeyboard(Context c, IBinder windowToken) {
InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(windowToken, 0);
}
closeKeyboard(getActivity(), yourEditText.getWindowToken());
This is working code to hide soft keyboard for android.
try {
InputMethodManager input = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}catch(Exception e) {
e.printStackTrace();
}
I'm using this code in a fragment
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
when I click on an action bar icon and it's working, I don't see why it shouldn't work in your case (maybe I misunderstood the question).
You can check my answer here. It was the only way that worked for me inside fragment.
A clear way to close keyboard and clearfocus of an EditText inside a fragment, is to make sure that your EditText XML has :
android:id="#+id/myEditText"
android:imeOptions="actionDone"
Then set listener to your EditText (with Kotlin, and from a fragment):
myEditText.setOnEditorActionListener({ v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.clearFocus()
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view!!.windowToken, 0)
}
false
})
Working in Fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

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

android - show soft keyboard on demand

Hi I wrapped edittext control onto a control that is being displayed on the screen at users request. It overlays the whole screen until user presses 'done' button on the keyboard.
I am not able to explicitly show the control on the screen. only when user taps into control only then its shown. Am I missing something?
I even try this and it does not brin it up when I launch the overlay that Edit Text exists on:
customCOntrol.showKeyboard();
public void showKeyboard()
{
InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
}
here is the settig I have on the screen itself in the config file android:windowSoftInputMode="stateHidden|adjustPan"
Thank you in advance
In your showKeyboard function you are calling:
imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
This will hide the softInput keyboard from the window!
Do you want to show the keyboard? If yes then would you use:
imm.showSoftInput(view, flags, resultReceiver);
EDIT: I think you can also toggle the keyboard from the InputMethodManager, try:
imm.toggleSoftInput(0, 0);
#dropsOfJupiter
You can do: editText.requestFocus() as you launch the Activity or Fragment containing your EditText reference. This will give the focus to the EditText and will bring uo the SoftKeyboard.
I hope this helps.
PROBLEM:
I faced with this keyboard not showing up problem. I wrote the following solution inspired by this answer but not their solution! It works fine. In short the reason for this mess is that the request focus and the IMM provided service can only run on a view that is created and active. When you do all these on the creation phase onCreate(Bundle savedInstance).. or onCreateView(LayoutInflater inflater... and the view is still in initializing state, you won't get an active view to act on! I have seen many solutions using delays and checks to wait for that view to get active then do the show keyboard but here is my solution based on the android frame work design:
SOLUTION:
in your activity or fragment override the following make sure your view has the access (define it in the top of the activity/fragment):
#Override
public void onStart() {
yourView.requestFocus();
showSoftKeyboard(yourView);
super.onStart();
}
public void showSoftKeyboard(View view) {
if(view.requestFocus()){
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}

Categories

Resources