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);
Related
I am implementing the Facebook Login with my app.
I can set the text of a Facebook login button through the .xml file.
Like This
<com.facebook.login.widget.LoginButton
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:id="#+id/login_button"
facebook:com_facebook_login_text="Custom Text Here"/>
I have seen questions that answer how to set text through the xml file, but is there anyway to do it programatically?
Edit The reason I want to do it programatically is because I'm using a SpannableStringBuilder that is using FontAwesome icons.
Like so:
SpannableStringBuilder facebook = new SpannableStringBuilder(getString(R.string.fa_icon_facebook) + " Facebook");
facebook.setSpan(new CustomTypefaceSpan("", fontAwesome, this), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
facebook.setSpan(new CustomTypefaceSpan("", champagneRegularFont, this), 1, 10, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
Can't you do this ?
Spannable span = new SpannableString("Facebook Login");
span.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// some other customizations...
loginButton.setText(span);
I don't think this is possible - at least from what I can tell Facebook handles a lot of that magic behind the scenes. The solution is a programmatic login from any button style you like.
First of all, I don't know whether I should ask this here or in the repo, but as I saw other questions for other libraries like.. Picasso, so here goes :
I'm trying to implement Emojione into my Android app. I've downloaded a small static class to convert all short name to unicode from Emojione Github Repo.
The problem is, when I try to use it to convert :smile: to unicode..
Emojione.shortnameToUnicode(postMessageText, true); // postMessageText is ":smile:"
It always return.. A square, as if it failed to convert. I tried other short names too like :smiley:, :grinning:, but they also failed. :(something): is really a short name right?
The answer is actually in front of my eyes all the time. It really did convert to unicode but, I might be wrong here, in a font unsupported by Android. So I tried using SpannableStringBuilder to span the emoji part to use emojione-android.ttf
Here's how :
int firstEq = sb.length();
Typeface font = FontCache.getTypeface("emojione-android.ttf", context);
String convertPart = Emojione.shortnameToUnicode(part, true);
sb.append(convertPart + " ");
int lastEq = sb.length();
sb.setSpan(new CustomTypefaceSpan("", font), firstEq, lastEq, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
And that's how I got the TextView to show emojis.. Just use the font!
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
I want to achieve this functionality. Please provide a suggestion for it.
Try this, this adds a drawable in a TextView after text.
TextView text=(TextView)findViewById(R.id.textView);
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("This is text with image!").append(" ");
Drawable myIcon = getResources().getDrawable(R.drawable.ic_new);
myIcon.setBounds(0, 0, test.getLineHeight(),test.getLineHeight());
builder.setSpan(new ImageSpan(myIcon, ImageSpan.ALIGN_BASELINE),builder.length() - 1, builder.length(), 0);
text.setText(builder);
I think you are looking for the Spannable interface, which can allow you to add images to a TextView.
If you want to know more about its usage, please read this post. Its a really good read.
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