views:

67

answers:

4

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.

A: 

try using inputBox

inputBox.getWindow().setSoftInputMode( 
               WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Aaron Saunders
Thanks for the suggestion. However, when I try that Eclipse says "the method getWindow() is undefined for type EditText"
A: 

try using view

v.getWindow().setSoftInputMode( 
           WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
pengwang
Thanks for the suggestion. I tried that, but unfortunately I got the same error message ("the method getWindow() is undefined for the type View")
+1  A: 

Have you tried to set focus on your EditText -> inputBox.requestFocus() or something like that?

Mur Votema
Thanks for the suggestion. I tried inserting inputBox.requestFocus(); after alert.setView(inputBox);, but no joy!
... and if you try to call requestFocus after alert.create() ? Because first dialog should be created and then edit can get focus, no?!
Mur Votema
My code doesn't explicitly call create(). Presumably it is called indirectly by the statement "AlertDialog.Builder alert = new AlertDialog.Builder(this);" which definitely comes before any other reference to alert.
but is there show()-method somewhere in your code?!
Mur Votema
Yes, the code shown in my original question continues "alert.setPositiveButton(...); alert.setNegativeButton(...); alert.show();". Following this with "inputBox.requestFocus()" does nothing. Trying "inputBox.setPressed(true);" just highlights the text box in green. I can't see any other relevant methods to try.
i'm not sure, but probably it's not possible to get access to a view, which was added to builder (http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/app/AlertDialog.java;h=021dc2e39f210d1e8c39f2380e83fd986ddf4d54;hb=edbabeb7fabfb3c7793b565cdaaf656e5e332efe) ....... why don't you try to use custom dialogs then?! another idea to create your own alertdialog.builder extending original alertdialog.builder and having member view in it.
Mur Votema