Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText text control - how to disable possibility to insert GIF from Google Keyboard?

My app's target API is 27 (Oreo). My app uses EditText control for text input. However, from Google Keyboard it's now possible to insert GIF. In this case, my app receives ACTION_SEND intent, with gif inserted from a keyboard, which is implemented for completely different functionality. So it just messes entire app flow and takes the user to completely different activity.

I want my EditText to accept text only and don't allow users to insert any gif there. How can I configure EditText to stop allow inserting GIFS from keyboard?

like image 886
user1209216 Avatar asked Apr 20 '18 11:04

user1209216


Video Answer


2 Answers

Tested on Samsung Galaxy S9+ (Samsung Keyboard), android 9 and Meizu Pro6 GBoard, android 6. Works perfect

To fix on GBoard, create your own edit text and override method "onCreateInputConnection(...)" as lines below:

    public class NoGifEditText extends AppCompatEditText {

    @Override
    public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
        final InputConnection ic = super.onCreateInputConnection(editorInfo);
        EditorInfoCompat.setContentMimeTypes(editorInfo, new String[]{"image/*", "image/png", "image/gif", "image/jpeg"});

        return InputConnectionCompat.createWrapper(ic, editorInfo,
                (inputContentInfo, flags, opts) -> {
                    Toast.makeText(getContext(), "No gif support", Toast.LENGTH_SHORT).show();
                    return true;
                });
    }
}

Explanation: Input compat handle insertion of selected mimetypes ("image/*", "image/png", "image/gif", "image/jpeg") and generate event. You catch this event inside block below and just show message, that gif not supported.

    {
        Toast.makeText(getContext(), "No gif support", Toast.LENGTH_SHORT).show();
        return true;
    }

To fix on Samsung keyboard, just add this lines in your edit XML

android:privateImeOptions="disableSticker=true;disableGifKeyboard=true"

As you see, button "add gif" on Samsung keyboard disabled.

like image 166
Рома Богдан Avatar answered Oct 18 '22 22:10

Рома Богдан


For EditText set the below option.

android:privateImeOptions="disableSticker=true;disableGifKeyboard=true"

Example :

android:layout_width="match_parent"
android:layout_height="wrap_content"              
android:privateImeOptions="disableSticker=true;disableGifKeyboard=true"/>
like image 41
MCH Avatar answered Oct 18 '22 22:10

MCH