Show soft keyboard for dialog - java

I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);
The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.
I also tried this code
dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange (View v, boolean hasFocus)
{
if (hasFocus)
{
Main.log("here");
dialogInput.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
/*
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(dialogField,0);
*/
}
}
});
in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.
I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.

Awesome question, I was trying to do that too and found a solution.
Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:
AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;
builder.set...
dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
This worked fine for me.
Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.

AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
Window window = dialog.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Kotlin
Here's the tested code.
val dialog = AlertDialog.Builder(requireContext()).apply {
setTitle(…)
setView(editText)
setPositiveButton(…)
setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.
Import AlertDialog from androidx.appcompat.app.AlertDialog.
Import WindowManager from android.view.

Dialog Fragment With Kotlin
override onStart Method
override fun onStart() {
super.onStart()
dialog.window?.
setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
)
}
if you want to close after dismiss then override dismiss method with below code
override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

Here's my solution, it's working well for dialog.
txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.
android:windowSoftInputMode="stateAlwaysHidden"

Related

Soft keyboard auto show when use AsyncTask

I don't know why when I use AsyncTask to load data to an Adapter, after AsyncTask finishes, the soft keyboard shows on screen.
There is a Edittext in the activity. If I delete the EditText on the Activity, after the AsyncTask finishes, the soft keyboard does not show.
I use this code on onPostExecute() but it does not work:
protected void onPostExecute(String[] result) {
hideSoftKeyboard(SearchActivity.this);
RecommendAdapter mAdap = new RecommendAdapter(con, mLocation);
mListview.setAdapter(mAdap);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
Now I know the answer.It because I use so it always focus on .Add the android:focusable="true"
android:focusableInTouchMode="true" to the Layout so the soft keyboard not show.

Show Soft Keyboard when EditText Appears

I have this code in my android app, for an alert dialog and keyboard, and when the dialog is shown I want the keyboard to appear. But its not and I am not sure why?
Here is my code:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.i(TAG, "in focus");
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}
}
});
But the keyboard does not appear even though the edit text is in focus, and I tell it to appear why, doesn't it come up?
Thanks for the help in advance.
I tested your code and the keyboard opened when alert.show() was executed. If you are using an emulator be sure that you have the virtual keyboard enabled. Otherwise a keyboard will never appear on the device screen.

How to make the virtual keyboard go away?

I have an EditBox in my AlertDialog popup. I made it to popup the virtual keyboard the moment the AlertDialog pops up (so that I don't need to click on that white field to show the keyboard), this way:
InputMethodManager imm = (InputMethodManager)
Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
But when I'm done typing I need to click "Done" on the keyboard and then OK in AlertDialog. The problem is, that users go right to the OK button once they're done typing and after clicking OK, the virtual keyboard stays onscreen. They now have to click back button on their device. How to clear the keyboard once the OK button is pressed?
Here's my whole AlertDialog code, if it helps:
case R.id.bKonacno:
}
LayoutInflater layoutInflaterK = LayoutInflater.from(context);
View promptViewK = layoutInflaterK.inflate(R.layout.popup_answer, null);
AlertDialog.Builder alertDialogBuilderK = new AlertDialog.Builder(context);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilderK.setView(promptViewK);
final EditText inputK = (EditText)promptViewK.findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager)
Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
alertDialogBuilderK
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
//some code of mine
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertK = alertDialogBuilderK.create();
alertK.show();
break;
You can do the following when Ok/Cancel button is pressed.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Read Close/hide the Android Soft Keyboard for more. Hope it helps.
Try adding the following to your buttons:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Show Keyboard when AlertDialog pops up

I've researched many answers on SO and come up with the following code to show the keyboard when my Dialog pops up:
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
final EditText input = new EditText(this);
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean focused) {
alertDialog
.getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
input.setFocusable(true);
input.requestFocus();
alertDialog.setView(input);
alertDialog.show();
The dialog Shows, but the keyboard doesn't pop up. This is all within an onTouch(...) method if that makes a difference.
My app is landscape mode only. I find that in portrait mode, it is showing. Why is this?
Any help is appreciated.
It looks like it was landscape mode that was throwing me off. The following piece of code solved the problem immediately:
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
imm.showSoftInput(input, InputMethodManager.SHOW_FORCED);
alertDialog
.getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

How do i implement a cancel-button in a Custom Dialog without AlertDialog?

i know, similar questions have been asked, and i've already looked through everything i could find, but i didn't find any answer to this problem.
Here's the Code:
protected Dialog onCreateDialog(int id)
{
Dialog dialog = new Dialog(this);
switch(id)
{
case R.layout.database_feed:
dialog.setContentView(R.layout.database_feed);
((Button) dialog.findViewById(R.id.discard_button)).setOnClickListener(
new View.OnClickListener()
{
//#Override
public void onClick(View v)
{
//dialog.cancel();
}
}
);
break;
}
return dialog;
}
I simply want to close the Dialog on a click on R.layout.database_feed button. But i don't have acces to the dialog within the onClick-method. I really feel confused.
I don't want to use an AlertDialog or a DialogBuilder, because there are other things in the Dialog that are difficult to implement in an AlertDialog or something.
Also, i already know the solution to make a separate Activity for the Dialog - but actually i want to know how it works the way i'm trying here.
Moreover i have already tried to use a DialogInterface.OnClickListener() but i can't use that in the setOnClickListener(...)-Method.
Just cancelling the dialog can't be that hard... but i don't get it.
Any hint/help is appreciated!
Thx
Change
Dialog dialog = new Dialog(this);
to
final Dialog dialog = new Dialog(this);
Then you can access dialog in your onClick() method.
Either store the 'dialog' as a class variable, or make it final in your method.

Categories