@Override public void appendGallery(MediaGallery mediaGallery) { Editable editableText = mContentEditText.getText(); if (editableText == null) { return; } int selectionStart = mContentEditText.getSelectionStart(); int selectionEnd = mContentEditText.getSelectionEnd(); if (selectionStart > selectionEnd) { int temp = selectionEnd; selectionEnd = selectionStart; selectionStart = temp; } int line, column = 0; if (mContentEditText.getLayout() != null) { line = mContentEditText.getLayout().getLineForOffset(selectionStart); column = mContentEditText.getSelectionStart() - mContentEditText.getLayout().getLineStart(line); } if (column != 0) { // insert one line break if the cursor is not at the first column editableText.insert(selectionEnd, "\n"); selectionStart = selectionStart + 1; selectionEnd = selectionEnd + 1; } editableText.insert(selectionStart, " "); MediaGalleryImageSpan is = new MediaGalleryImageSpan( getActivity(), mediaGallery, R.drawable.legacy_icon_mediagallery_placeholder); editableText.setSpan(is, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); editableText.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); editableText.insert(selectionEnd + 1, "\n\n"); }