I managed to solve this using InputConnectionWrapper, which has an explicit callback for entering fullscreen mode.
/**
* [InputConnection] wrapper which applies hint text to
* the IME when entering fullscreen mode.
*/
class FullscreenHintInputConnection(
delegate: InputConnection,
private val editText: EditText,
private val hintText: CharSequence
) : InputConnectionWrapper(delegate, false) {
override fun reportFullscreenMode(enabled: Boolean): Boolean {
if (enabled) {
editText.hint = hintText
} else {
editText.hint = null
}
return super.reportFullscreenMode(enabled)
}
}
In an EditText subclass:
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
val connection = super.onCreateInputConnection(outAttrs)
return FullscreenHintInputConnection(connection, this, "Lorem ipsum")
}
While this solution works, it's worth noting that AppCompat manages to do this a little more elegantly. The library modifies the EditorInfo argument passed into onCreateInputConnection
, avoiding the InputConnectionWrapper subclass entirely.
AppCompatEditText.java
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return AppCompatHintHelper.onCreateInputConnection(
super.onCreateInputConnection(outAttrs),
outAttrs,
this
);
}
AppCompatHintHelper.java
class AppCompatHintHelper {
static InputConnection onCreateInputConnection(InputConnection ic, EditorInfo outAttrs,
View view) {
if (ic != null && outAttrs.hintText == null) {
ViewParent parent = view.getParent();
while (parent instanceof View) {
if (parent instanceof WithHint) {
outAttrs.hintText = ((WithHint) parent).getHint();
break;
}
parent = parent.getParent();
}
}
return ic;
}
}
This is used to apply a hint supplied from a TextInputLayout to the underlying TextInputEditText. Some of these APIs are restricted to the library and therefore you would need to copy them into your own project. But unfortunately, this technique isn't working for me so far, the hint remains blank.