Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show android:hint only in Extract UI mode

I have a list of EditTexts in an android layout. Each one is labeled with a TextView, so no hint is necessary in portrait mode, and a hint would even be redundant. However, in landscape mode, many people have keyboards configured to take full screen and hide the app until the keyboard is hidden and the input is injected into the selected view:

SwiftKey keyboard in landscape fullscreen extract ui mode

This may be fine if you have one EditText field for input, but if you have a list, nobody is memorizing the six TextView labels before going through the inputs.

How can I set a hint that only appears if the keyboard is in extract ui mode - be it in landscape or portrait orientation?

like image 940
Menasheh Avatar asked Jul 19 '16 20:07

Menasheh


Video Answer


1 Answers

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.

like image 92
Damien Diehl Avatar answered Oct 16 '22 21:10

Damien Diehl