Android Edittext Text Control - How to Disable Possibility to Insert Gif from Google Keyboard

Insert images/sticker/gif in Edittext in android from custom keyboard/google keyboard

As given in your question it looks like you don't have set content mime type. I have created a EditText with callback keyBoardInputCallbackListener which detect if any gif/png/jpg/webp is inserted via softkeyboard.

public class MyEditText extends android.support.v7.widget.AppCompatEditText {

private String[] imgTypeString;
private KeyBoardInputCallbackListener keyBoardInputCallbackListener;

public MyEditText(Context context) {
super(context);
initView();
}

public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}

private void initView() {
imgTypeString = new String[]{"image/png",
"image/gif",
"image/jpeg",
"image/webp"};
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
final InputConnection ic = super.onCreateInputConnection(outAttrs);
EditorInfoCompat.setContentMimeTypes(outAttrs,
imgTypeString);
return InputConnectionCompat.createWrapper(ic, outAttrs, callback);
}


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
}
}
boolean supported = false;
for (final String mimeType : imgTypeString) {
if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
supported = true;
break;
}
}
if (!supported) {
return false;
}

if (keyBoardInputCallbackListener != null) {
keyBoardInputCallbackListener.onCommitContent(inputContentInfo, flags, opts);
}
return true; // return true if succeeded
}
};

public interface KeyBoardInputCallbackListener {
void onCommitContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts);
}

public void setKeyBoardInputCallbackListener(KeyBoardInputCallbackListener keyBoardInputCallbackListener) {
this.keyBoardInputCallbackListener = keyBoardInputCallbackListener;
}

public String[] getImgTypeString() {
return imgTypeString;
}

public void setImgTypeString(String[] imgTypeString) {
this.imgTypeString = imgTypeString;
}
}

Use this in your activity class -

MyEditText myEditText = (MyEditText)findViewbyId(R.id.myEditText);
myEditText.setKeyBoardInputCallbackListener(new KeyBoardInputCallbackListener() {
@Override
public void onCommitContent(InputContentInfoCompat inputContentInfo,
int flags, Bundle opts) {
//you will get your gif/png/jpg here in opts bundle
}
});

Disabling of EditText in Android

I believe the correct would be to set android:editable="false".

And if you wonder why my link point to the attributes of TextView, you the answer is because EditText inherits from TextView:

EditText is a thin veneer over
TextView that configures itself to be
editable.

Update:

As mentioned in the comments below, editable is deprecated (since API level 3). You should instead be using inputType (with the value none).

How to disable displaying suggestions on the Soft Keyboard

When developing for 2.0+, the supposed way is setting android:inputType="textNoSuggestions" (ref).
Unfortunately, suggestions are still shown on HTC Desire 2.2 (and probably other HTC Sense devices as well).

Using android:inputType="textVisiblePassword"will not help as well as the software keyboard by HTC won't allow you to switch languages.

So I stick to android:inputType="textFilter" to disable suggestions.



Related Topics



Leave a reply



Submit