Pedro
Pedro

Reputation: 211

Change EditText IME_ACTION programmatically

In my app I need to display one or two edittexts to collect information (e1 and e2), depending on the selection the user will do through a radiobutton. This is done by setting the visibility state of the edittext to GONE and works fine.

My problem is how to programmatically set the IME_ACTION from "done" to "next" for each case, i.e.:

1) Only e1 is visible - set IME_ACTION of e1 to DONE

2) e1 and e2 are visible - set IME_ACTION of e1 to NEXT and IME_ACTION of e2 to DONE.

I'm using android:minSdkVersion="4" and android:targetSdkVersion="16" and testing on a Android 2.2 device.

Here's my layout:

<EditText
android:id="@+id/e1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="@string/sh15"
android:textColor="@android:color/black"
android:textSize="@dimen/s">
</EditText>         
<EditText
android:id="@+id/e2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="@string/sh16"
android:textColor="@android:color/black"
android:textSize="@dimen/s">
</EditText> 

Here's my code:

RadioGroup r= (RadioGroup) dialog.findViewById(R.id.rg);
  r.setOnCheckedChangeListener(new OnCheckedChangeListener() 
  {
   public void onCheckedChanged(RadioGroup group, int checkedId) 
   {
    switch(checkedId)
    {
     case R.id.rb1: //show one edittext
         e1.setVisibility(View.VISIBLE);             
         e2.setVisibility(View.GONE);
         e1.setImeOptions(EditorInfo.IME_ACTION_DONE);
     break;
     case R.id.rb2: //show two edittext
         e1.setVisibility(View.VISIBLE);
         e2.setVisibility(View.VISIBLE);
         e1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
         e2.setImeOptions(EditorInfo.IME_ACTION_DONE);
     break;

    }
   }
  });  

Upvotes: 8

Views: 9607

Answers (2)

AllDayAmazing
AllDayAmazing

Reputation: 2383

In case anyone lands here: EditText.setImeOptions(EditorInfo.IME_ACTION_NEXT); and god help you if it's TouchWiz :)

Also, I have noticed problems when the EditText can't switch if it has focus, so make sure to close the keyboard and rescind focus

Upvotes: 1

QuokMoon
QuokMoon

Reputation: 4425

e2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Overrid
    public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
                //your code    
              }
          }
)};

Upvotes: 6

Related Questions