Show Soft Keyboard when EditText Appears - java

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.

Related

How can I force the keyboard to appear?

When the user clicks on an editText the keyboard is brought up for the user to type. Can I make the keyboard to appear when a user clicks on a button instead of the editText? Can the numpad appear instead of the normal keyboard?
Can i make the keyboard to appear when a user clicks on a button
instead of the editText?
Yes, you need to set the focus and pop up the keyboard using InputMethodManager
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Focus the field.
editText.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
Can the numpad appear instead of the normal keyboard?
Yes, using input type
Either in the xml tag of edittext
<EditText...
android:inputType="number"/>
or in java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
public static void toggleKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
} catch (Exception e) {
Log.e("On show keyboard error: %s", e.getMessage());
}
}

java Android - two dialogs, prevent first dialog from closing after second one exits

Initially in my app I am creating an AlertDialog which has three buttons, in which the middle button opens up another AlertDialog. The problem is that when the second AlertDialog closes after a button is pressed, the first one closes with it. I think both AlertDialogs get closed after I press a button on the second AlertDialog.
What I want is for the first AlertDialog to open another AlertDialog that has its own buttons, and when second AlertDialog presses a button, it only closes itself and goes back to the first one. Is there any way to achieve this?
Here is the code for the button used to open the AlertDialog:
final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);
Here's the code for a button that opens an AlertDialog that contains another button that opens another AlertDialog using the middle button (create button) on itself, but closes them both when a button on the second one is pressed (either the yes or no button, which is not what I want as I only want the second one to close itself and go back to the first AlertDialog, and yea this sounds pretty confusing in theory so I can try to clarify if needed):
fabgroup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
helpBuilder.setTitle("Select a group");
helpBuilder.setMessage("Add to group?");
final TextView input = new TextView(mainactiv.this);
input.setSingleLine();
input.setText("");
helpBuilder.setView(input);
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
}
});
helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//open another alertbox
AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
helpBuilder2.setTitle("Assign a new group");
helpBuilder2.setMessage("Create group?");
final EditText input = new EditText(CreateNote.this);
input.setSingleLine();
input.setText("");
helpBuilder2.setView(input);
helpBuilder2.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Create Group
Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
}
});
helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog2 = helpBuilder2.create();
helpDialog2.show();
}
});
helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
});
Help would be greatly appreciated.
I eventually managed to solve this problem by creating two separate functions to generate each dialog box, and when one closes it calls the function to create the other one, kinda like recycling (or maybe closer to looping functions). Although, I'm not entirely sure how performance heavy this is, but it seems to do the job without any issues from what I'm testing. If anyone would like to chime in on how this could be an issue, then I'm open to hearing what others have to say about the negative points of using alert dialog boxes this way.
You can show an activity as dialog. Put this in your manifest file.
<activity android:theme="#android:style/Theme.Dialog" android:excludeFromRecents="true"/>
From this answer: Android Activity as a dialog

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

Show soft keyboard for dialog

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"

Categories