Show Keyboard when AlertDialog pops up - java

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

Related

How to make only AlertDialog's button clickable when dialog is showing [duplicate]

This question already has answers here:
Prevent Android activity dialog from closing on outside touch
(20 answers)
Closed 2 years ago.
i have an AlertDialog on my Android app. When i show to AlertDialog i want to disable only when users click "OKAY" button. Because i reset the screen when users clicked "OKAY" button.
My problem is when i click somewhere on screen outside of AlertDialog, dialog is closing but i can not clean the screen.
This is my code;
AlertDialog.Builder builder = new AlertDialog.Builder(GameOnePlayer.this, R.style.AlertDialogTheme);
//builder.setCancelable(true);
View view = LayoutInflater.from(GameOnePlayer.this).inflate(
R.layout.layout_winner_dialog,
(ConstraintLayout)findViewById(R.id.layoutAlertDialogContainer)
);
builder.setView(view);
final AlertDialog alertDialog = builder.create();
//alertDialog.setCanceledOnTouchOutside(false);
view.findViewById(R.id.buttonAlertDialog).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideSystemUI();
clearScreen();
alertDialog.dismiss();
}
});
if(alertDialog.getWindow() != null){
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
alertDialog.show();
I tried alertDialog.setCanceledOnTouchOutside(false); but it did'not work.
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
This is my style.xml
What do I have to do for this? Thanks.
Edit: I tried these advices but did not work.
builder.setCancelable(false);
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
Edit2:
Hi i found the solution on the other post.
Just added these line in the onCreate method.
this.setFinishOnTouchOutside(false);
Many thanks for all you helpers.
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
builder.setCancelable(false)
Put this line below the "builder.setView(view)" line so your dialog is not close when you touch outside the dialog or when you press back button your dialog is not closed.
You can add OnCancelListener to your dialog to listen Dialog cancellation and perform the screen reset.
builder.setOnCancelListener {
//call your method to reset the screen
}

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 style AlertDialogs android

I need help with the dialog interface in android. i don't get it.. i' ve searched here to, i got many answers but with every code i used from here my application crashed..
So I make a notes app an you can choice a with an alert dialog which type of note you want.
The dialog window is black. So can someone show me how to chage the color maybe simple in white so that i understand how it works?
here is my code:
private void showNewNoteChoices() {
final CharSequence[] items = {
getResources().getString(R.string.text_note_type),
getResources().getString(R.string.log_note_type),
getResources().getString(R.string.important_type),
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select type of note");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
loadNoteFragment(item, newNoteTitles, null);
}
});
AlertDialog alert = builder.create();
alert.show();
}
I know that i have to make an xml file for the specific layout and i have to make an style . Can someone show me how I can make my dialog interface in white?
I would recommend you using Dialog. You can style it the way you want.
For example, you create a custom dialog with custom layout like this;
final Dialog dialog = new Dialog(MainActivity.this);
dialog .setContentView(R.layout.custom_layout);
Then you can create views and listeners;
Button button = (Button) dialog.findViewById(R.id.button);
//Other views and listeners etc..
Finally you show it;
dialog.show();

How to display popup option of "set as wallpaper" when user press on image for 3-5 sec in inageviewer app?

Following... this link ..How to set image as wall paper in viewpager app?. I am able to set wallpaper directly located in my drawable folder. However, i want to give user a chance to set wallpaper by displaying pop up dialogue box which should be displayed. When user clicks on images for 3-5 sec.
I am kind of new to android programming.. So, please help..
android framework already supporting class "alertDialogue.builder". you can set message whatever you wanna show in addition to dialogue buttons, title etc.
http://developer.android.com/reference/android/app/AlertDialog.Builder.html
5sec is too long to stay in one fingerpoint. in onsimplegesturedetector class, there is already "long press" detector
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
you can set your own ontouchListener that implements onTouchListener that has gesture detector, detects long press and can show dialogue .
there can be better way but I'm using this logic in my project so you can reference and fix it if you find better way
findViewById("your wall paper image id").onTouchListener(new MyOnTouchListener());
class MyOnTouchListener implements onTouchListener{
GestureDetector gd = new GestureDetector(new SimpleOnGestureListener(){
#Override
public void onLongPress(MotionEvent e) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder
.setMessage("click yes to set wallpaper!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//setting wallpaper
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}

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