Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying soft keyboard whenever AlertDialog.Builder object is opened

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.

like image 219
prepbgg Avatar asked Oct 29 '10 18:10

prepbgg


People also ask

How do I enable soft keyboard on Android?

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.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How do I turn off AlertDialog?

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.


2 Answers

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.

like image 173
Defragged Avatar answered Sep 24 '22 19:09

Defragged


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.

like image 34
prepbgg Avatar answered Sep 24 '22 19:09

prepbgg