question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Refresh/Update Default Action Button in Android Keyboard

I have a RecyclerView that contains EditText(s). The default (from what I can tell) Action Button (android:imeOptions) is either actionNext (when there are 1 or more items after the focused EditText) or actionDone (when the last EditText is focused). One of the controls in each row of my RecyclerView is a Button that adds another row (which obviously includes an EditText) after the current one. However, if the user clicks this Button while the keyboard is open, the Action Button does not get updated until the currently focused EditText loses & regains focus. How can I force the Action Button to be updated appropriately (sort of like closing & reopening the keyboard)?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

However, if the user clicks this Button while the keyboard is open, the Action Button does not get updated until the currently focused EditText loses & regains focus.

Hi, njsokalski. When clicking the button, will it change the focus on the control? The Action Button depends on the editText which is being focused. If you want to change the style of the action button, please set the new added editText to be focused.

For example:

private void Btn_Click(object sender, EventArgs e)
{
    EditText edit = new EditText(this);
    var _params = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);
    edit.LayoutParameters = _params;

    edit.InputType = Android.Text.InputTypes.ClassText;

    edit.ImeOptions = Android.Views.InputMethods.ImeAction.Search;
    layout.AddView(edit);

    edit.RequestFocus();
}

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Assuming when you say "When clicking the button" you are referring to the Action Button, then yes, it does change focus (just like I want). However, it does not update the visual appearance of the Action Button. My scenario may be clearer with a few screenshots:
78290-screenshot-1615903279.png
In this screenshot, the focused EditText ("Number 4") is the last in the list, therefore the keyboard shows a checkmark as expected and desired. Clicking the + Button adds another row (an EditText, + Button, and X Button) after this. After doing this (at which point the "Number 4" EditText is still focused, the + Button does not change focus to the newly added row), the Action Button visually remains as a checkmark, although its function changes to actionNext. So it is only the visual component that is not updated, all functionality is updated appropriately.

0 Votes 0 ·

Try to clear the focus of the previous editText and set focus for the new added one. And use InputMethodManager.ShowSoftInput method to show the keyboard programmatically.

private void Adapter_ItemClick(object sender, int e)
{
    list.Add(new model { name = "new_item" + number++, imeAction = ImeAction.Search });
    adapter.NotifyDataSetChanged();
    handler.PostDelayed(() => {
        View view = mLayoutManager.FindViewByPosition(adapter.ItemCount - 1);
        EditText editText = view.FindViewById<EditText>(Resource.Id.edit);
        editText.RequestFocus();
        InputMethodManager imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
        imm.ShowSoftInput(editText, ShowFlags.Implicit);
    }, 200);
}
0 Votes 0 ·