AlertDialog shows EditText but no open the soft keyboard.

Put your problem here if it does not fit any of the other categories.

AlertDialog shows EditText but no open the soft keyboard.

Postby sblantipodi » Fri Sep 02, 2011 6:36 pm

Hi all...

I have this code to show an alert input.
When a user click on some object I launch this alert input to let the
user to input some data.

To do it I use this code.

void alertInputStr(String title, String message, int
numeroCampoEditabile) {
AlertDialog.Builder alertI = new AlertDialog.Builder(this);
final int num = numeroCampoEditabile;
alertI.setTitle(title);
alertI.setMessage(message);
input = new EditText(this);
alertI.setView(input);
alertI.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int
whichButton) {
Editable value = input.getText();
String valInput;
try {
valInput = value.toString();
} catch (Exception e) {
valInput = "";
}
switch (num) {
case 1:
Home.to = valInput;
instance.setContentView(new
SendMail(instance));
break;
}
}
});
alertI.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int
whichButton) {
}
});
alertI.show();

}

This code displays an alert with a textbox, user can tap on the
textbox to enter the desiderd value.
The problem is that in this way user needs to tap two times to enter
the text.
Is it possible to display the alert input with the keyboard opened,
without the needs to tap a second time to open the keyboard?

Thanks.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Top

Re: AlertDialog shows EditText but no open the soft keyboard

Postby sblantipodi » Fri Sep 02, 2011 7:59 pm

Just solved, I post the solution, just for reference.

myAlertDialog.show();
showVirturalKeyboard();

this is the showVirtualKeyboard() method:

// Mostra la tastiera virtuale
private void showVirturalKeyboard() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
InputMethodManager m = (InputMethodManager) MCPRO.this.getSystemService(Context.INPUT_METHOD_SERVICE);

if (m != null) {
// m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}
}
}, 100);
}
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Re: AlertDialog shows EditText but no open the soft keyboard

Postby Dockland » Fri Sep 02, 2011 8:48 pm

I think that the problem could be that EditText view is created with an activity as context. I do inflate and set a view - everything works as expected.
Dockland
Developer
Developer
 
Posts: 37
Joined: Wed Jul 27, 2011 12:29 pm

Re: AlertDialog shows EditText but no open the soft keyboard

Postby sblantipodi » Thu Sep 08, 2011 12:08 am

Dockland wrote:I think that the problem could be that EditText view is created with an activity as context. I do inflate and set a view - everything works as expected.


I don't catched what do you want to mean.
Where should I create the edittext to make it work?

have you got a snippet of the inflater?
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Re: AlertDialog shows EditText but no open the soft keyboard

Postby Dockland » Thu Sep 08, 2011 8:12 am

Make full example (archive and attach).
I have used
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AlertDialog.Builder b = new AlertDialog.Builder(this);
  2.         b.setView(new EditText(this));
  3.         b.setPositiveButton("exit", new DialogInterface.OnClickListener() {
  4.                        
  5.                         @Override
  6.                         public void onClick(DialogInterface dialog, int which) {
  7.                                 dialog.dismiss();                              
  8.                         }
  9.                 });
  10.         b.show();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

it displays soft keyboard after one click on EditText.
Dockland
Developer
Developer
 
Posts: 37
Joined: Wed Jul 27, 2011 12:29 pm

Re: AlertDialog shows EditText but no open the soft keyboard

Postby sblantipodi » Sat Sep 10, 2011 1:06 am

Dockland wrote:Make full example (archive and attach).
I have used
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AlertDialog.Builder b = new AlertDialog.Builder(this);
  2.         b.setView(new EditText(this));
  3.         b.setPositiveButton("exit", new DialogInterface.OnClickListener() {
  4.                        
  5.                         @Override
  6.                         public void onClick(DialogInterface dialog, int which) {
  7.                                 dialog.dismiss();                              
  8.                         }
  9.                 });
  10.         b.show();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

it displays soft keyboard after one click on EditText.


its not different from the code I posted previously.
My problem is that I want to display the softkeyaboard as soon as the alertdialog is displayed without the need of the additional tap on the edittext.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], MSNbot Media and 19 guests