Search code examples
androidkeyboardrotationandroid-lifecycleonconfigurationchanged

How to change keyboard IMEaction on screen rotation


I need to change keyboard IME whenever the screen rotates. If device is in landscape i need the action to be IME_ACTION_NEXT, if the device rotates - while keyboard is presented - i need to change the action to accept returns.

I've tried creating another version of the view as a landscapeview, but that does not change the keyboard. I've also tried changing the ime action in "onConfigurationChanged", but no changes are made to the keyboard in that case either.

Are there anyway to force the keyboard to refresh on rotation?


Solution

  • OnConfigurationChanged is only called if you tell it to via android:configChanges="orientation|screenSize" in your manifest. Do you have that set? Otherwise your Activity isn't called on orientation, its actually killed and restarted.

    An alternative would be to to use separate layouts for landscape and portrait. They could specify different settings, but then you have to maintain 2 files.

    A possibility is to set the value in your single layout to @string/keyboardAction, and define that in your strings.xml file, then create a landscape strings.xml file to override it. That should work, but I've never actually tried to do that with a imeOptions string so I can't completely promise it. But it would be the cleanest of the two ways if it works.

    Edit:

    Just reread- this is when the keyboard is already present, it works if it was opened after rotation? That actually makes sense- now we're getting deep into the keyboard API. What actually causes the keyboard to reload that field will differ between keyboards, so an exact answer is hard. The thing that would certainly make it happen would be if onStartInputView is called with a new session (the second parameter true) on rotation. If its called with false, it may or may not reset the label, but it should. I don't believe that its called at all on rotation.

    At any rate, try calling inputMethodManager.restartInput on rotation. That's the function that EditText calls when you change the inputType, the keyboard app should then reinitialize the views.