Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gboard: Enable GIF insertion on EditText

I am using the Gboard from Google in my app and when i input a GIF from the keyboard app to my EditText it then shows a toast

"The text field does not support GIF insertion from the keyboard"

I have search about this a thousand times and can't find out a result

Any help would be appreciated.

like image 424
GGWP Avatar asked Jan 05 '18 07:01

GGWP


People also ask

Why are GIFs in Gboard not working?

So, if your Gboard GIF is not working correctly or has stopped working, it could be that your Gboard app needs an update. How To Update The Gboard App? If there is an update pending for Gboard app, you will be able to see it under the Updates tab. To update it, simply tap on the Update icon next to the Gboard app.

Does Gboard support GIF?

You can also scroll through GIF themes by swiping left next to the search bar. Once you find the GIF you want, just tap it. In some apps, Gboard will send the GIF after you tap it.


1 Answers

Image Keyboard Support

Users often want to communicate with emojis, stickers, and other kinds of rich
content. In previous versions of Android, soft keyboards (also known as input
method editors or IMEs) could send only unicode emoji to apps. For rich
content, apps had to either build app-specific APIs that couldn't be used in
other apps or use workaround like sending images through Easy Share Action or the
clipboard.

How it works

Keyboard image insertion requires participation from both the IME and the app. The following sequence describes each step in the image insertion process:

When the user taps on an EditText, the editor sends a list of MIME content types that it accepts in EditorInfo.contentMimeTypes.

The IME reads the list of supported types and displays content in the soft keyboard that the editor can accept.

When the user selects an image, the IME calls commitContent() and sends an InputContentInfo to the editor. The commitContent() call is analogous to the commitText() call, but for rich content. InputContentInfo contains an URI that identifies the content in a content provider. Your app can then request permission and read the content from the URI.

To accept rich content from IMEs, apps must tell IMEs what content types it 
accepts and specify a callbackup method that is executed when content is
received. The following example demonstrates how to create an EditText that
accept PNG images:
EditText editText = new EditText(this) {
    @Override
    public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
        final InputConnection ic = super.onCreateInputConnection(editorInfo);
        EditorInfoCompat.setContentMimeTypes(editorInfo,
                new String [] {"image/png"});

        final InputConnectionCompat.OnCommitContentListener callback =
            new InputConnectionCompat.OnCommitContentListener() {
                @Override
                public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
                        int flags, Bundle opts) {
                    // read and display inputContentInfo asynchronously
                    if (BuildCompat.isAtLeastNMR1() && (flags &
                        InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                        try {
                            inputContentInfo.requestPermission();
                        }
                        catch (Exception e) {
                            return false; // return false if failed
                        }
                    }

                    // read and display inputContentInfo asynchronously.
                    // call inputContentInfo.releasePermission() as needed.

                    return true;  // return true if succeeded
                }
            };
        return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
    }
};

Here is Full Documentation Reference

https://developer.android.com/guide/topics/text/image-keyboard.html#how_it_works

Adding Image Support to IMEs

IMEs that want to send rich content to apps must implement the Commit Content API as shown below:

Override onStartInput() or onStartInputView() and read the list of supported content types from the target editor. The following code snippet shows how to check whether the target editor accepts GIF images.

@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
    String[] mimeTypes = EditorInfoCompat.getContentMimeTypes(editorInfo);

    boolean gifSupported = false;
    for (String mimeType : mimeTypes) {
        if (ClipDescription.compareMimeTypes(mimeType, "image/gif")) {
            gifSupported = true;
        }
    }

    if (gifSupported) {
        // the target editor supports GIFs. enable corresponding content
    } else {
        // the target editor does not support GIFs. disable corresponding content
    }
}

Commit content to the app when the users selects an image. Avoid calling commitContent() when there is any composing text because it might cause the editor to lose focus. The following code snippet shows how to commit a GIF image.

/**
 * Commits a GIF image
 *
 * @param contentUri Content URI of the GIF image to be sent
 * @param imageDescription Description of the GIF image to be sent
 */
public static void commitGifImage(Uri contentUri, String imageDescription) {
    InputContentInfoCompat inputContentInfo = new InputContentInfoCompat(
            contentUri,
            new ClipDescription(imageDescription, new String[]{"image/gif"}));
    InputConnection inputConnection = getCurrentInputConnection();
    EditorInfo editorInfo = getCurrentInputEditorInfo();
    Int flags = 0;
    if (android.os.Build.VERSION.SDK_INT >= 25) {
        flags |= InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION;
    }
    InputConnectionCompat.commitContent(
            inputConnection, editorInfo, inputContentInfo, flags, opts);
}

Here is Full Documentation

https://developer.android.com/guide/topics/text/image-keyboard.html#adding_image_support_to_imes

like image 187
Mallikarjuna Avatar answered Sep 28 '22 03:09

Mallikarjuna