I am using SpannableString to insert emotion icons into EditText.
The following four cases are all OK:
(1) append text or emotion icon at the end of EditText,
(2) insert emotion icon between text,
(3) insert emotion icon between existed emotion icons.
(4) insert text between existed text.
But the problem is I cannot insert text between those icons. Here is my code and logcat info.
(1) Add icon to EditText:
Drawable d = UiUtil.getCachedDrawable(mActivity, EmResource.findResIdByTag(value));
if (d != null) {
d.setBounds(0, 0, w, w);
String str = "<img src='" + value + "'/>";
SpannableString ss = new SpannableString(str);
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
ss.setSpan(span, 0, str.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
contentEt.getText().replace(contentEt.getSelectionStart(), contentEt.getSelectionEnd(), ss);
}
(2) After inputing some icons, it looks like this (I have moved the cursor manualy after typing):
I have added a TextWatcher for this EditText. Log info from TextWatcher:
<img src='115'/><img src='116'/><img src='117'/><img src='118'/>
(3) Then I press 'y' on the keyboard (in the position of previous screen shot). After that the cursor is moving but the text isn't shown. Like the following screen shot:
But the log info shows that the text is inserted to EditText successfully.
<img src='115'/><img src='116'/>y<img src='117'/><img src='118'/>
<img src='115'/><img src='116'/>yy<img src='117'/><img src='118'/>
<img src='115'/><img src='116'/>yyy<img src='117'/><img src='118'/>
Any suggestions? Thanks a lot.
Change Spannable.SPAN_INCLUSIVE_EXCLUSIVE to Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
Hope it helps.
As SpannableString content is immutable, it might not allow you to edit between two emojis, Instead of that try with SpannableStringBuilder whose content and markup can both be changed. Hope it will help
Related
I am creating a Gallery application. Each image will show the title along with the name of the person who uploaded the image. Example of what I want is below:
Here the Night is dark, but this darkness has the silence is the title for the image, and the shaggy boy is the user who uploaded the image for example.
So, now I want Night is dark, but this darkness has the silence to be in the separate textview and the shaggy boy at the end of the Night is dark, but this darkness has the silence's textview even if Night is dark, but this darkness has the silence is a single line or multi-line.
I have used the Flexbox Layout but I am failed to achieve what I want.
How to append the textview to the end of another textview? Thanks!!!
You can use a single TextView and set its text String with a Spannable and match each portion of text with indices here I am separating both portions by a hyphen; but you can change that as you would like.
TextView textView = findViewById(...);
String tag = "Night is dark, but this darkness has the silence - shaggy boy";
SpannableString spannableString = new SpannableString(tag);
spannableString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, tag.indexOf("-"), 0);
spannableString.setSpan(new ForegroundColorSpan(Color.GRAY), tag.indexOf("-"), tag.length(), 0);
textView.setText(spannableString);
Maybe you could just get the text of the author's TextView and add it to the String of the first TextView?
You can use a single TextView with String palceholders of the quote and the author, and add the colors using HTML tags.
strings.xml
<resources>
<string name="tag">
<![CDATA[
<font color="#00000">%1$s</font> -
<font color="#D3D3D3">%2$s</font>
]]>
</string>
</resources>
Behavior:
String message = "Night is dark, but this darkness has the silence";
String name = "shaggy boy";
String formattedTag = String.format(getString(R.string.tag), message, name);
textview.setText(Html.fromHtml(formattedTag));
I'm trying to create a custom keyboard but only with emotions. The keyboard is for the system not for my own application.
The problem: I cannot commit a text with the image. I try to send the text like a SpannableString but it not work.
I searched a lot but still don't have a answer. Someone can help me?
Update
I tried:
ImageSpan image = new ImageSpan(v.getContext(),R.drawable.ic_clock_active);
SpannableString spannable = new SpannableString(SMILE_ANCHOR);
spannable.setSpan(image, spannable.length() -
SMILE_ANCHOR.length(), spannable.length(), 0);
ic.commitText(spannable, 1);
in Android I would like to concatenate number with Arabic text. but if i concatenate the number then the arabic text direction changes.
Example: My Application reads each line of arabic text from file and adds the line number to each line and appended to string builder , After completion of reading final string will be displayed on text view.
Here is my code
SpannableStringBuilder styledString = new SpannableStringBuilder(TextUtils.concat(l,"("+sn+")"));
styledString.setSpan (new CustomTypefaceSpan("", type), 0, l.length()-1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
// styledString.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE),l.length()-1,l.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannablestringbuilder.append(styledString);
Expected Output:
(3) ﺑِﺴْﻢِ ٱﻟﻠَّﻪِ ٱﻟﺮَّﺣْﻤَٰﻦِ ٱﻟﺮَّﺣِﻴِ(1)ٱﻟْﺤَﻤْﺪُ ﻟِﻠَّﻪِ ﺭَﺏِّ ٱﻟْﻌَٰﻠَﻤِﻴﻦَ (2) ٱﻟﺮَّﺣْﻤَٰﻦِ ٱﻟﺮَّﺣِﻴﻢِ
Actual Output
(1)ﺑِﺴْﻢِ ٱﻟﻠَّﻪِ ٱﻟﺮَّﺣْﻤَٰﻦِ ٱﻟﺮَّﺣِﻴﻢِ (2)ٱﻟْﺤَﻤْﺪُ ﻟِﻠَّﻪِ ﺭَﺏِّ ٱﻟْﻌَٰﻠَﻤِﻴﻦَ ٱﻟﺮَّﺣْﻤَٰﻦِ(3) ٱﻟﺮَّﺣِﻴﻢِ
Try
Removing spaces
Use hyphens - instead of spaces
Try finding a way to change the direction
By the way, this question is answerde here and might be a duplicate!
Thanks for posting!
I have created a custom soft Keyboard(IME) where we can add custom emoji.
Whenever i try to add my emoticons to it. it override the last text entered. i mean it dont append the emoticons unless it override it. how can i add emoji to currentInputConnection
for example
i image write hello image abcimage ///where image represents emoji
it becomes
i image write hello image image
// and i can add image after space easily or i can repeatedly add images easily .
when i add text it appends to emoji but when i add emoji after entering some text it remove the text and then add it self(emoji image).
Just for testing purpose i put emoji code to shift key
Problem code
else if (primaryCode == Keyboard.KEYCODE_SHIFT)
{
// this.handleShift();
//this.mComposing.append(getSmiledText(getApplicationContext(), ":)"));
ImageGetter imageGetter = new ImageGetter()
{
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(R.drawable.e041);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Spanned cs = Html.fromHtml("<img src='" + getResources().getDrawable(R.drawable.e041) + "'/>", imageGetter, null);
// getCurrentInputConnection().commitText(cs, 1);
// this.mComposing.append(cs);
//getCurrentInputConnection().commitText(getSmiledText(getApplicationContext(), ":)"), 1);
getCurrentInputConnection().beginBatchEdit();
getCurrentInputConnection().commitText(cs, 1);
getCurrentInputConnection().endBatchEdit();
//getCurrentInputConnection().setComposingText(cs, 1); // it is giving wrong
}
And tried multiple ways to solve it ,some tried codes are shown in comments
Simple Words : I want to append emoji image to text, but when i add image to text, it remove the written text, how can i solve. full source can't be shown it is lengthy. Ask me if you need any method in my class
Related link :
add custom image as Emoji in android
https://stackoverflow.com/questions/24100615/cannot-add-an-image-to-my-keyboard-service
Implementations of Emoji (Emoticon) View/Keyboard Layouts
Thanks in Advance.
I also tried for the same but always got "obj".Atlast I found a solution try this.
Instead of adding drawables just add emoticons by their uniCodes like this..
getCurrentInputConnection().commitText((CharSequence)"\ud83c\udfb5", 1);
Its working for me perfectly.and also remove beginBatchEdit and endBatchEdit from your code.
Thanks
I have a TextView and I want to show multi-colored text in that. I am using html.fromHtml for this, but it is not showing any color.
My code is:
//statusToShow is a String
Log.d("html ==>", statusToShow) ;
RStatus.setText(Html.fromHtml(statusToShow),
TextView.BufferType.SPANNABLE);
Value from Log is:
<span style='background-color:#80B5FF;color:#fff'>C</span> <span style='background-color:#CF8DEA;color:#fff'>P</span> <span style='background-color:red;color:#fff'>E</span> <span style='background-color:green;color:#fff'>D</span>
Not all tags are supported, list of supported tags is here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html
Try using Spannable like this:
Spannable WordtoSpan = new SpannableString("Text To Span");
WordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), 0, 4,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
This will make the background color of Text Blue.
Hope this helps.