Android multi color in one TextView [duplicate] - android

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:

Related

Facebook Login Button Text

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.

Add ImageView after text

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.

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

Html.fromHtml not working for span

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.

How To HighLight Particular Sentence in Android Using TextView

I am developing eBook Reader.Kindly suggest me how can i hightlight particular sentence of my string in android Like iBook In IOS ,MoonReader in android & many more.
There is any library for it.Kindly suggest me any example or tutorial .
try this code hope this help you
textView.setText("This Is Android", TextView.BufferType.SPANNABLE);
Spannable WordtoSpan = (Spannable) textView.getText();
WordtoSpan.setSpan(new BackgroundColorSpan(FF0000), 6, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);
It will highlight the text from character 6 to 15

Categories

Resources