Add ImageView after text - android

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.

Related

Android - Shortname to Unicode from Emojione doesn't work

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!

Create a custom emotions android keyboard

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);

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)));`

Android multi color in one TextView [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to have multiple styles inside a TextView?
I want my TextView to show some part of its text in red and others in black. It's content (the text) is created dynamically and i don't know how many words will be red colored.
Is there any way to do this like in html-css?
You can use Spannable to achieve what you want.
String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
} else {
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
}
try this way
TextView tv = (TextView)findViewById(R.id.tv);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordtoSpan);
What you basically want is SpannableString
See this for the complete example:

Change text on shape

Hi
is it possible to change the text of a shape? I have:
levelFraction = new Text(0, 300, mFont, "text", HorizontalAlign.CENTER);
then I add it to the scene:
scene.getTopLayer().addEntity(levelFraction);
but I don't see any method to change the text at runtime. Something like:
levelFraction.setText(...);
Is that possible?
if Sheet1.shapes(0).texteffect.text="Go" then
sheet1.shapes(0).texteffect.text="Stop"
else
sheet1.shapes(0).texteffect.text="Go"
end if
You could even call a sub based on the the value of the texteffect.text
I don't know anything about the AndEngine library, so this would just be a wild guess:
Could you perhaps use the ChangeableText (which extends Text) instead of Text? From the name it sounds like you should be able to change the text, and looking at the code it also have a couple of functions to set the text: ChangeableText source
font_texture_golbalValue = new Texture(128,64,TextureOptions.BILINEAR);
font4 = new Font(font_texture_golbalValue, Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD), textSize, true, Color.WHITE);
this.mEngine.getTextureManager().loadTexture(this.font_texture_golbalValue);
this.mEngine.getFontManager().loadFont(this.font4);
scoreval=new ChangeableText(24*CAMERA_WIDTH/100, score_postion, this.font2, ""+scoreValue,"Score##".length());
and You can use scorevalue.SetText();

Categories

Resources