Android - Shortname to Unicode from Emojione doesn't work - android

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!

Related

Characters UTF-16LE not showing on my Android Nougat

I am developing an Android Application and I need to display on a TextView special UTF-16LE characters like æ or Œ, or the one in the picture I posted.
I can't understand why everything is displayed correctly on Nougat Emulator and also on an Lollipop and Marshmallow Smartphone while on my Personal Android Nougat I can't see those characters, I can only see ": :" characters instead of the right ones.
(Careful, In the left picture, that ' character is not a normal accent and that I is not a normal I)
Why can't I display those special characters on my Smartphone? How should I proceed with that?
I debugged my application and characters are properly set on my TextView; but still, I can't read them correctly.
It seems that my Smartphone (OnePlus3 on latest Software Updates) can't read UTF-16LE characters, but still, It's really strange for me to face something like that.
Any help would be very appreciated,
Have a nice day.
UPDATE: I downloaded https://play.google.com/store/apps/details?id=org.madore.android.unicodeMap on my Smartphone and it seems that I don't have many of those unicode characters on my Smartphone, is there a way to fix it? Do I just need to install a better font?
I solved my issue, that's what I did. (As usual, thanks also to #Claffolo, my teammate)
I imported a free copyrighted font like Gentium in my android assets.
/assets/fonts/
On my project, I imported the new font and init a variable like that
Typeface plain = Typeface.createFromAsset(getActivity().getAssets(), "fonts/GentiumPlus-I.ttf");
final TextView text_view = (TextView)view.findViewById(R.id.text_view);
text_view.setTypeface(plain);
text_view.setText("[String with special characters]");
When I loaded my ArrayList with all the special characters, let's say my wordlist.
I did it in the following way:
//Loading my Wordlist
BufferedReader line_reader = new BufferedReader(new InputStreamReader(activity.getResources().openRawResource(R.raw.ipautf16le), Charset.forName("UTF-16LE")));
String line;
try {
while ((line = line_reader.readLine()) != null) {
WordList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(WordList);
That's all.

AutoLink link square brackets of URL in TextView

I have a url:
https://<site name>/pallavi/[Songs.PK]%2002%20.mp3
I have a text view, with property: android:autoLink="all"
If I simply set the text to the text view, my text view simply highlights the portion preceding the [. It looks something like this:
https://< site name >/pallavi/[Songs.PK]%2002%20.mp3
What i want is, the whole link should be highlighted like:
https://< site name >/pallavi/[Songs.PK]%2002%20.mp3
What I have tried till now:
Used the < pre > tag and Html.fromHtml, but it doesn't seem to work! (I don't even know if the < pre > is supported in android though.)
Used Jsoup.parser. But that too doesn't seem to work for me.
UPDATE
I have tried this answer too: https://stackoverflow.com/a/12376115/1320263
Please let me know if the issue is with android that the text view's linkAll property itself does not consider parenthesis as a valid character or not? If it is supported, how do i hyperlink that too?
Also NOTE:
The text(or link) I have written in the question is just a sample text. In reality, I am getting a block of text, from where it would be very difficult to identify where exactly the hyper link starts and where it ends. Also, the number of links present in the block would be un-known. Hence I cannot use the < a href = "" > thing...
If some one else happens to have the same issue, following is the solution which worked for me:
Pattern pattern = Pattern.compile("(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");
SpannableString spannable = new SpannableString(html);
Matcher matcher = pattern.matcher(spannable);
// Create ActivitySpans for each match
while (matcher.find())
spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Create a new TextView with these spans and enable the clickable links
mTxtEventDescription.setText(spannable);
You can try this:
((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));`

Typeface is not rendered in Android

I'm trying to set a font like this:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myfont.ttf");
textView.setTypeFace(tf);
However, it works for some fonts like Helvetica, or like that, but as soon as I try to load some other fonts, it ignores my typeface and it loads android's default one.
I've checked a looot of answers, and an user stated
Android at the moment when can't set a typeface, instead of throwing an exception, just places system default typeface.
But this is obviously not an answer.
What can I do?
I've even checked sourcecode of TextView, which leaded me to TextPaint, and TextPaint leaded me to Paint but couldn't really find a solution about my issue.
Help would be sooo appreciated.
Thanks.
But this is obviously not an answer.
Well, grammatically it has issues, but it is the answer. Android has had problems loading fonts since Android 1.0, and the API for Typeface does not report success or failure.
What can I do?
Test your typefaces and see which ones work.
If you have licensed the rights to modify the font, you could try loading it into a font editor and saving it again, to see if it is a question of the format of the font file.
Or, contribute changes to the AOSP to improve typeface handling, then wait a few years for those changes to make it out to the majority of Android devices.
Try typeface using SpannableStringBuilder it will work,,
String stringResult="Your unicode string";
SpannableStringBuilder SS = new SpannableStringBuilder(stringResult);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myfont.ttf");
if(tf != null)
{
SS.setSpan (new CustomTypefaceSpan("", tf), 0, stringResult.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
}
textView.setText(SS);

Android handle HTML links and typed links in a TextView

I am trying to handle both HTML and typed links in TextViews and I am unable to find a combination of the built in tools to do this. I can make one or the other work but not both.
Given the following formats
http://google.com
Google!
Using .setMovementMethod(LinkMovementMethod.getInstance()) I can make the anchor tag turn into a link and open a webpage on click. Using .setAutoLinkMask(Linkify.ALL) I can make the typed link work as expected. The problem is setAutoLinkMask disables the setMovementMethod functionality and removes the highlighting it creates on the html link as well as its clicking functionality.
I tried searching for others with this issue and I believe I am being blocked by a lack of proper terms for this situation. Has anyone else come across a solution to handle both cases seamlessly?
This is what I have currently, only the typed link is linked in the TextView, the anchor just displays the text it wraps.
mTextViewBio.setText(Html.fromHtml(htmlstring));
mTextViewBio.setAutoLinkMask(Linkify.ALL);
mTextViewBio.setMovementMethod(LinkMovementMethod.getInstance());
mTextViewBio.setLinksClickable(true);
TextView output:
http://google.com
Google!
Problem is that when Linify.addLinks() is called first thing what this method does is removing all spans. When you use Html.fromHtml() Spanned is return, so when Linkify parse text again firstly remove "html links". I wrote a simply class LinkifyExtra. It has one extra method
public class LinkifyExtra extends Linkify {
public static Spanned addLinksHtmlAware(String htmlString) {
// gather links from html
Spanned spann = Html.fromHtml(htmlString);
URLSpan[] old = spann.getSpans(0, spann.length(), URLSpan.class);
List<Pair<Integer, Integer>> htmlLinks = new ArrayList<Pair<Integer, Integer>>();
for (URLSpan span : old) {
htmlLinks.add(new Pair<Integer, Integer>(spann.getSpanStart(span),
spann.getSpanEnd(span)));
}
// linkify spanned, html link will be lost
Linkify.addLinks((Spannable) spann, Linkify.ALL);
// add html links back
for (int i = 0; i < old.length; i++) {
((Spannable) spann).setSpan(old[i], htmlLinks.get(i).first,
htmlLinks.get(i).second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spann;
}
}
and use it like that
String htmlstring = "http://google.com Google!";
textView = (TextView)findViewById(R.id.textView1);
textView.setText(LinkifyExtra.addLinksHtmlAware(htmlstring));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
And it works. But you can't use mTextViewBio.setAutoLinkMask(Linkify.ALL);because it triggers addLinks() and remove "html links". Depends of what you want to do in bigger picture, this method may need some changes. I skip checking if spans are overlapping because I suppose it can't happen but if I am wrong you can simply copy this method.

How to Add Smiley/Emojis in Edittext?

How to Add Smiley/Emojis in Edittext?
Any Source code is Available on Internet, if yes Please Give me Link.
Thanks in Advance.
I am using below code for add Smiley/Emojis in edittext.
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(R.drawable.happy);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
cs = Html.fromHtml("<img src='" + getResources().getDrawable(R.drawable.happy) + "'/>", imageGetter, null);
edttxtemoji.setText(cs);
Android may not support some special characters, but here is the tutorial anyways:
On a PC, I believe either Alt + 1 or Alt + 2 makes a smiley face.
This website provides an excellent tutorial for how to do alt codes, along with what alt code combination does what.
For a Mac, you can hold "Option + Command + T" down to open a list of special characters. From there, open the category "Miscellaneous." Your smiley is in there.
Is this what you are looking for? If not, just let me know :).
When all else fails, you could just use an image of a smiley-face.
EDIT: I'm using a Mac. I opened TextEdit, followed my above tutorial for the Mac, generated a smiley face, cut the smiley face, and pasted it into the Java code. It worked for me. If it's not working for you, try copying this:
EditText mEditText = null;
mEditText.setText("☺");
EDIT 2: I thought Martin was looking for just a smiley, not a window of Emoji's. Currently, there is no source code to open this window - only a separate app that does this, which can be found at this link or this link.

Categories

Resources