Enabling keyboard incognito mode for Android webview

410 Views Asked by At

I need to be able to conditionally enable the incognito/private mode on android keyboards inside a webview.

For regular inputs there is the setImeOptions() method which works fine but the WebView class doesn't appear to have that and the only possible hook I could find is overriding onCreateInputConnection like I've attempted below:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    if(privateBrowsingEnabled){
        Log.d("INCOG", "Enabling private keyboard mode "+outAttrs.imeOptions );
        outAttrs.imeOptions |= IME_FLAG_NO_PERSONALIZED_LEARNING;
    }else{
        Log.d("INCOG", "Disabling private keyboard mode "+outAttrs.imeOptions );
        outAttrs.imeOptions = IME_NULL;
    }
    return super.onCreateInputConnection(outAttrs);
}

The overridden method is called but changing the imeOptions property doesn't appear to have any effect on the keyboard.

1

There are 1 best solutions below

0
On

Was looking to do this myself and the following snippet ended up working for me:

@Override
public InputConnection onCreateInputConnection(EditorInfo info) {
    InputConnection connection = super.onCreateInputConnection(info);
    info.imeOptions |= EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING;
    return connection;
}

My guess is super.onCreateInputConnection() overrides imeOptions so you have to set the flag after calling super.

I'm using the latest version of GBoard which supports incognito mode.