Mbhammerbro
Mbhammerbro

Reputation: 194

Changing input type at runtime

I am using an application where after a user clicks a button, an alert is created asking the user for an email address. This alert contains an EditText, and two buttons. I would like to change the input type of this EditText to type email upon runtime, but it does not change. Code can be found below.

EDIT: setRawInputType was changed to setInputType. This now changed my input type to email address, but my keyboard does not actually change with it. Am I missing something possibly about the way this works?

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    alert.setMessage("Email Address:");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText().toString();;
      Intent i = new Intent(Intent.ACTION_SEND);
      i.setType("message/rfc822");
      i.putExtra(Intent.EXTRA_EMAIL  , value);
      i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
      i.putExtra(Intent.EXTRA_TEXT   , "body of email");
      try {
          startActivity(Intent.createChooser(i, "Send mail..."));
      } catch (android.content.ActivityNotFoundException ex) {
          Toast.makeText(getBaseContext(), "Please enter a valid number",
                    Toast.LENGTH_LONG).show();
      }
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
      }
    });

    alert.show();

Upvotes: 1

Views: 1952

Answers (2)

Mbhammerbro
Mbhammerbro

Reputation: 194

I figured this out myself after messing around with it a lot. In order to actually change the input type I had to use the following:

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

Upvotes: 2

Austin Greco
Austin Greco

Reputation: 33544

Try using setInputType instead of setRawInputType

See docs here

Set the type of the content with a constant as defined for inputType. This will take care of changing the key listener, by calling setKeyListener(KeyListener)

whereas raw does not

Upvotes: 0

Related Questions