Modify & Refresh Layout When Opening / Closing Keyboard

Nathan Sokalski 4,111 Reputation points
2022-01-05T23:42:04.117+00:00

I have a RecyclerView containing EditText(s). Because the keyboard uses a huge amount of the screen height when in landscape, I need to hide several View(s) in order to allow the user to see enough of the RecyclerView to scroll to other EditText(s) in the list. In order to make sure the entire RecyclerView remains visible so that the user can scroll to all items, I am using SoftInput.AdjustResize. However, I am having 2 problems:

  1. When to show/hide the View(s) I will be hiding to make space for the RecyclerView? I know that Android does not currently have events for when the keyboard is opened or closed, so I am trying to determine how to make sure the visibility of the appropriate View(s) is updated when necessary?
  2. View(s) not refreshed when opening/closing keyboard? I have tried setting the visibility of different View(s) to static values, and when the keyboard is opened or closed, the layout sometimes does strange things, including not always displaying afterwards the same View(s) that were displayed before opening the keyboard. After determining where to put it, what can I do to refresh the layout of all View(s)?

I find UI very challenging for landscape keyboard, because the screen barely leaves enough space for one EditText (or whatever View is involved), so allowing the user to move between fields or scroll between items they cannot see makes it confusing.

Developer technologies | .NET | Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Nathan Sokalski 4,111 Reputation points
    2022-01-07T17:31:10.243+00:00

    The code is executed when the keyboard is closed. I do believe, however, that I have found a solution that seems to be working (I don't know if it would be appropriate for all people's scenarios, but it solved it for me). In the GlobalLayout handler, after doing the other stuff I needed to do, l temporarily stored the height (which would be either 0 for a row weight of 1 or a fixed height) of my RecyclerView as follows:

    int prevheight = this.rvPlayerNamesInput.LayoutParameters.Height;
    

    Then, after changing the RecyclerView's height back to either 0 (for a row weight of 1) or a fixed height, I compared the previous value to 0 before setting a View's Visibility (which could probably be almost any View, in my case I used a TextView which should be hidden at this point in my app) to Visible and then back to Gone as follows:

    if (prevheight == 0)
    {
        this.tvPlayerNames.Visibility = ViewStates.Visible;
        this.tvPlayerNames.Visibility = ViewStates.Gone;
    }
    

    I use this condition to avoid repetitive calling of GlobalLayout. I include this code for both opening & closing the keyboard, the only difference between the two is whether to use prevheight==0 or prevheight!=0. My app is not 100% finished, so it is always possible to find some scenario that needs a few extra tweaks (things always do, right?), but hopefully this can help everyone else, and me as well.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.