- Programming VIP
- Android Dialog and the correct way to open the soft keyboard
- 1. Write before
- 2. Implementation process
- 3. Code
- 4. Write at the end
- How to show/hide the Android Soft Keyboard in dialog?
- 9 Answers 9
- Android: EditText in Dialog doesn’t pull up soft keyboard
- Open soft keyboard programmatically
- 25 Answers 25
- Kotlin
- Android: show soft keyboard automatically when focus is on an EditText
- 28 Answers 28
Programming VIP
Very Interesting Programming
Android Dialog and the correct way to open the soft keyboard
1. Write before
The first blog in 2017, I wish you all a Happy New Year, healthy and safe in the new year!
The main content of this blog is about the display and hiding of the soft keyboard in Dialog. The requirement is to have a password input box in Dialog, pop up the Dialog display soft keyboard, turn off the hidden soft keyboard in Dialog.
Well, is it a bit simple, but there are still some problems in the process of implementation. After trying most of the methods on the Internet, I finally found a good way to share it with you.
Look at the picture:
2. Implementation process
Let’s start with the initial implementation:
But, the soft keyboard is not displayed, there are two ways to display the soft keyboard, the first way is used, the second method has no effect in Dialog.
So look at the source code of Dialog and see that the display and hiding of Dialog is handled by Handler. Is it possible that the method of displaying the soft keyboard executes too fast, so the soft keyboard is displayed delay after the show method, and it is found that it can be displayed normally.
But deferred processing is obviously not a good way to do this. Is there a way to call back once the Dialog display is complete? Well, yes, there is a setOnShowListener method that displays a soft keyboard in the callback method onShow of the OnShowListener interface.There are no limitations to hiding the soft keyboard, either after dismiss or in the callback method onDismiss.
Delayed display soft keyboard
3. Code
Show Dialog prompt box
Ways to show and hide soft keyboards
Call the method that displays the Dialog
4. Write at the end
In the frequent tests of showing and turning off Dialog, it was found that sometimes the soft keyboard would not be displayed, but there was no rule found. Students with understanding can leave comments for me.
Welcome to your classmates’comments. If you think this blog is useful for you, leave a message or click on your favorite blog.
Added by esmarts on Tue, 14 May 2019 19:57:27 +0300
Источник
How to show/hide the Android Soft Keyboard in dialog?
In my application custom dialog is in BaseExpandableListAdapter class. In dialog I have two edit text. First is name and its mandatory. And second is address its optional. And two buttons OK and cancel. When Dialog shows I want to show keyboard with request focus for edit text name. After clicking of OK button Soft Keyboard should get hide.
9 Answers 9
on click of ok button write the below code:-
Define context as Context context=this.
Use following code to hide keyboard
Use following code to show keyboard
Use this function:
I wish it be useful
Use this in your activity
Here is a solution:
- When Dialog shows I want to show keyboard with request focus for edit text name.
This is easy as you yourself answered it. Add to your EditText via xml or editText.requestFocus(); via code before you show the dialog.
- After clicking of OK button Soft Keyboard should get hide.
This can be achieved in two ways, depending upon what you are doing on click of your OK button.
a. If you are starting a new Activity — Add android:windowSoftInputMode=»stateHidden» to this activity in the Manifest, so everytime the activity starts keyboard will be hidden unless you call it.
b. If you are on the same page — call the below method.
In case getCurrentFocus().getWindowToken() gives error then pass any View to it (you can track this via the try catch block) where View could be anything, a Button, EditText etc of your Activity ( myButton..getWindowToken() ).
Источник
Android: EditText in Dialog doesn’t pull up soft keyboard
So I’ve got what seems to be a common problem, which is that the EditText in my dialog box doesn’t show up when it gets focus. I’ve seen several workarounds, such as in this thread, this one and this one (and many more), but I have never seen a satisfactory explanation for why this is happening in the first place.
I would much prefer to have android use its own default behavior for EditTexts than to build my own, but it seems like everyone (in those threads) has accepted that the default behavior for EditTexts in Dialogs is to just give a cursor and no keyboard. Why would that be?
For the record, none of these workarounds seem to be working for me — the closest I’ve been able to come is forcing a keyboard to appear underneath the dialog box (using InputMethodManager.toggleSoftKeyboard(*)). My particular configuration is API15, the EditText shows up in a footer on a ListView within an AlertDialog. The EditText android:focusable=»true» is set, and onFocusChangeListener is receiving focus events.
Edit:
As requested, here is the specific code snippet that I’m working with. I won’t bother with the whole layout, but in this specific application, the EditText appears in response to pressing a button on the dialog (similar to an action view). It is contained in a RelativeLayout which by default has visibility «gone»:
The code which builds this sets the visibility of the relativeLayout to «Visible» (and hides the other UI elements). This should be enough to pull up the keyboard when the EditText gets focused, based on my experience with EditText. However, for some reason this is not the case. I can set the following onFocusChangeListener:
Using this configuration, when I first enter the EditText, the onFocusChangedListener triggers, and generates a log that invariably looks like this:
The keyboard shows up and then disappears, probably because I toggle it twice, but even when I make sure it stays up, it’s behind the dialog window (in a greyed out area), and there’s no way to get to it without closing the dialog.
That said, I’d like to emphasize that even though I may be able to get this work-around to work, I’m primarily interested in finding a simple reason why the EditText isn’t triggering in the first place, and why this seems to be so commonplace!
Источник
Open soft keyboard programmatically
I have an activity with no child widgets for it and the corresponding xml file is,
and I want to open soft keyboard programmatically while the activity gets start.and what I’ve tried upto now is,
Give me some guidance.
25 Answers 25
I have used the following lines to display the soft keyboard manually inside the onclick event, and the keyboard is visible.
But I’m still not able to open this while the activity gets opened, so are there any solution for this?
In your manifest file, try adding the following to the that you want to show the keyboard when the activity starts:
This should cause the keyboard to become visible when the activity starts.
For more options, checkout the documentation.
Please follow the below code. I am sure your problem will be solved.
All I needed was to expose the keyboard, in a very precise moment. This worked for me! Thanks Benites.
And in the very precise moment:
I have used the following lines to display the soft keyboard manually inside the onclick event.
Put that in onResume method:
the runnable is needed because when the OS fires the onResume method you can’t be sure that all the views where draw, so the post method called from your root layout makes it wait till every view is ready.
in onCreate method of activity or onActivityCreated of a fragment
seems like this is working
seems this works better: in manifest:
seems the manifest working in android 4.2.2 but not working in android 4.0.3
Kotlin
I have used like this to show the soft keyboard programatically and this is worked for me to prevent the auto resize of the screen while launching the keyboard.
In manifest:
In XXXActvity:
I assume this will save others time to search for this problem.
I used it as singleton like:
Use it in your activity like:
And you call this method like this:
Use above code in onResume() to open soft Keyboard
InputMethodManager.SHOW_FORCED isn’t good choice. If you use this setting you should manage hiding keyboard state. My suggestion is like this;
Also, you can focus on view (usually EditText) taking parameters it. This makes it a more useful function
Источник
Android: show soft keyboard automatically when focus is on an EditText
I’m showing an input box using AlertDialog . The EditText inside the dialog itself is automatically focused when I call AlertDialog.show() , but the soft keyboard is not automatically shown.
How do I make the soft keyboard automatically show when the dialog is shown? (and there is no physical/hardware keyboard). Similar to how when I press the Search button to invoke the global search, the soft keyboard is automatically shown.
28 Answers 28
You can create a focus listener on the EditText on the AlertDialog , then get the AlertDialog ‘s Window . From there you can make the soft keyboard show by calling setSoftInputMode .
For showing keyboard use:
For hiding keyboard use:
You can request a soft keyboard right after creating the dialog (test on SDK — r20)
I had the same problem and solved it with the following code. I’m not sure how it will behave on a phone with hardware keyboard.
Snippets of code from other answers work, but it is not always obvious where to place them in the code, especially if you are using an AlertDialog.Builder and followed the official dialog tutorial because it doesn’t use final AlertDialog . or alertDialog.show() .
Is preferable to
Because SOFT_INPUT_STATE_ALWAYS_VISIBLE will hide the keyboard if the focus switches away from the EditText, where SHOW_FORCED will keep the keyboard displayed until it is explicitly dismissed, even if the user returns to the homescreen or displays the recent apps.
Below is working code for an AlertDialog created using a custom layout with an EditText defined in XML. It also sets the keyboard to have a «go» key and allows it to trigger the positive button.
Well, this is a pretty old post, still there is something to add.
These are 2 simple methods that help me to keep keyboard under control and they work just perfect:
Show keyboard
Hide keyboard
I know this question is old by I think using an extension function is a prettier way to show keyboard for an edit text
here is the method I use to show keyboard for an edittext.
kotlin code: just need to call edittext.showKeyboard()
the java code:
Let me point some additional info to the solution of yuku, because I found it hard to get this working! How do I get the AlertDialog object from my AlertDialog.Builder? Well, it’s the result of my alert.show() execution:
Take a look at this discussion which handles manually hiding and showing the IME. However, my feeling is that if a focused EditText is not bringing the IME up it is because you are calling AlertDialog.show() in your OnCreate() or some other method which is evoked before the screen is actually presented. Moving it to OnPostResume() should fix it in that case I believe.
Yes you can do with setOnFocusChangeListener it will help you.
If anyone is getting:
Cannot make a static reference to the non-static method getSystemService(String) from the type Activity
Try adding context to getSystemService call.
The original question concerns Dialogs and my EditText is on a regular view. Anyhow, I suspect this should work for most of you too. So here’s what works for me (the above suggested highest rated method did nothing for me). Here’s a custom EditView that does this (subclassing is not necessary, but I found it convenient for my purposes as I wanted to also grab the focus when the view becomes visible).
This is actually largely the same as the tidbecks answer. I actually didn’t notice his answer at all as it had zero up votes. Then I was about to just comment his post, but it would have been too long, so I ended doing this post anyways. tidbeck points out that he’s unsure how it works with devices having keyboards. I can confirm that the behaviour seems to be exactly the same in either case. That being such that on portrait mode the software keyboard gets popped up and on landscape it doesn’t. Having the physical keyboard slid out or not makes no difference on my phone.
Источник