How To HighLight Particular Sentence in Android Using TextView - android

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

Related

Cannot Set Multi Text color for text view using Html.fromText in < 24

I have seen all around stackoverflow to use
setText(html.fromHtml("<font color=\"#c5c5c5\">Hello</font>"))
However none of these work for android < 23
here is a sample code:
TextView mTextView = new TextView(context);
mTextView.setText(Html.fromHtml("<p style=\"color:red;\">Test</p>"));
This sets font color in > 23. Is there any alternatives that work for < 23? or is this a bug with the following dependency?
implementation 'androidx.appcompat:appcompat:1.1.0'
Some more info: I am testing on Lolipop x86 image
No need for HTML, just use spans.
Spannable text = new SpannableString("hello");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
mTextView.setText(text);
If you're using appcompat, you probably want to new AppCompatTextView(context) rather than just new TextView(context).
use following
setText(Html.fromHtml("<![CDATA[<font color='#c5c5c5'>Hello</font>]]>"));
for xml layout remember to add
android:textAllCaps="true"

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.

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:

Categories

Resources