0

What I want to do: I am having an activity that is creating an AlertDialog, this activity is introducing programmatically and edittext to the AlertDialog. I want to ensure that such edittext is focused and the keyboards appears.

There are a lot of threads in SO where good explanations appears of how to perform that (like this Android TextField : set focus + soft input programmatically) when the view is creating using layout (and so, you have and id).

However, there are several rules, specially, you can't focus a view until it has been layour in the screen.

So what is happening to me is that, I create the edittext programmatically, afterwards I show the dialog and afterwards I am not able to access to my view as it has not ID (can't use findviewbyid).

So, how can I requestfocus and display the keyboard on a Edittext created programmatically in an AlertDialog?

I am showing here the pice of code (removing for simplicity the possitive button and negative button):

 final EditText input = new EditText(getApplicationContext());
     input.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,                     LayoutParams.WRAP_CONTENT));
     input.setGravity(Gravity.LEFT);
     input.setImeOptions(EditorInfo.IME_ACTION_DONE);
     input.setText(current_name);
     input.setTextColor(Color.BLACK);
     input.setBackgroundColor(Color.WHITE);
     input.setFocusableInTouchMode(true);
     input.requestFocus();



    AlertDialog EditDialog =new AlertDialog.Builder(this)
    //set message, title, and icon
    .setTitle(title_id) 
    .setIcon(R.drawable.ic_launcher_blue2)
    .setView(input)

What I would like to do is that when my EditDialog is laid out in the screen I can request the focus on input and show the keyboard.

Community
  • 1
  • 1
Trebia Project.
  • 930
  • 2
  • 17
  • 36

1 Answers1

0

try this :

input.setFocusableInTouchMode(true);
input.requestFocus();
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
  • That is not working, I already tried. You can only request focus on a view when is already drawn. This is the creation step and the view is created when I do EditDialog.show(); – Trebia Project. Nov 16 '14 at 11:57