My code for opening an input dialog reads as follows:
final AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Dialog Title"); alert.setMessage("Request information"); LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.edittextautotextlayout, null); final EditText inputBox = (EditText) textEntryView.findViewById(R.id.my_et_layout); alert.setView(inputBox);
This works fine except that I have to tap the text entry line before the soft keyboard appears.
Following the advice given here I have tried inserting:
inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { alert.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } });
but Eclipse objects that "the method getWindow() is not defined for the type AlertDialog.Builder".
It seems that the setOnFocusChangeListener code works for an AlertDialog object but not an AlertDialog.Builder. How should I modify my code to make the soft keyboard appear automatcially.
To enable your latest Android keyboard, scroll to the bottom and hit the System entry. Then, click Languages & input. Pick Virtual keyboard on the following page. You will find a list of all the existing keyboards on your smartphone here.
A simple dialog containing an DatePicker . This class was deprecated in API level 26.
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
As long as you always need to show the keyboard immediately once the dialog opens rather than once a specific form widget inside gets focus (for instance, if your dialog just shows an EditText
and a button), you can do the following:
AlertDialog alertToShow = alert.create(); alertToShow.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); alertToShow.show();
Rather than calling .show()
on your builder immediately, you can instead call .create()
which allows you to do some extra processing on it before you display it onto the screen.
This is in response to miannelle.
The following method is called when a menu option is selected:
private void addNote() { final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.textentryalertdialog); dialog.setTitle("Add note"); TextView msgText = (TextView) dialog.findViewById(R.id.messagetext); msgText.setText("Whatever prompt you want"); final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext); Button okButton = (Button) dialog.findViewById(R.id.OKButton); okButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { dialog.dismiss(); // app specific code } }); Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton); cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { dialog.dismiss(); } }); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); }
The textentryalertdialog.xml file defines a linear layout containing
TextView android:id="@+id/messagetext" ...
EditText android:id="@+id/my_edittext" ...
Button android:id="@+id/OKButton" ...
Button android:id="@+id/CancelButton" ...
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With