Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the focus to a specific input field in an AlertDialog?

In a login dialog with two input fields (user name / password) I would like to set the focus on the second field (because user name is stored in preferences).

Does the AlertDialog.Builder provide a way to set the focus?

like image 513
mjn Avatar asked Oct 18 '25 07:10

mjn


2 Answers

Iam using alertdialog too, you could try

final EditText input = new EditText(this);    
input.setInputType(InputType.TYPE_CLASS_TEXT); // you should use .TYPE_TEXT_VARIATION_PASSWORD
input.requestFocus();

Example:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(x);
        builder.setIcon(R.drawable.x);

        final EditText input = new EditText(this);

        input.setInputType(InputType.TYPE_CLASS_TEXT);
        input.setText("mytext");

        builder.setView(input);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
            @Override
          // xy

        });
        builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
            @Override
            // xy
        });

        builder.show();

        input.requestFocus(); // <--- for the focus
    }

Regards

like image 94
Oli Avatar answered Oct 19 '25 21:10

Oli


If you are using a custom XML layout for your dialog, you can use <requestFocus /> on your EditText.

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>

Or if you want to do it programmatically, use .requestFocus() on your EditText after the dialog is shown. Assuming you have a custom layout named custom_login_dialog.xml containing the Username and Password fields:

    // Inflate your custom layout
    LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
    View customView = inflater.inflate(R.layout.custom_login_dialog, null);

    // Define your EditText fields
    final EditText etUsername = (EditText) customView.findViewById(R.id.etUsername);
    final EditText etPassword = (EditText) customView.findViewById(R.id.etPassword);

    // Build the dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(customView); // Set the view of the dialog to your custom layout
    builder.setTitle("Login");
    builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...
        }
    });

    // Create and show the dialog
    builder.create().show();

    // Request focus after showing dialog
    etPassword.requestFocus();
like image 40
Krauxe Avatar answered Oct 19 '25 22:10

Krauxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!