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?
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.
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With