How can I highlight the textview in android? - android

This is what I have done!
spannableString.setSpan(new BackgroundColorSpan(ContextCompat.getColor(mContext, R.color.highlightColor)), 0, div.getText().length(), 0);
textview.setText(spannableString);
Result Achieved!
Desire Result!

Maybe this link can help you: Can I select a color for each text line I append to a TextView?
But instead of the ForegroundColorSpan try using the BackgroundColorSpan

Related

How to change Edit text font color when character limit exceeds?

[I want to change edit text color when character limit exceeds just like in this image.]
[[1]: http://i.stack.imgur.com/fHTCA.png]
How to give this effect on Text Change Listener ???
I'd suggest following this link, which I believe show you how.
Android: Different colours for different characters in EditText
You can use Spannable :
EditText tv = (EditText )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);
Or html format Html.fromHtml :
String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";
tv.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
try This I think this will help you:
if(edit_text.length> limit){edit_text.settextcolor("your color code");}

edittext with multi style text

I need to add multiple styles for text in my edittext, after searching I have found two ways
1- using this and it is working for me
str.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.correct_email_color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
2- using HTML format, and I need to be able to use this to add padding and margin to my text :
str.setSpan(new SpannableString(Html.fromHtml(String.format(EMAIL_HTML_FORMAT, "ffA9F5BC" , splittedEmail[i]))), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
but the second way is not working, can any one help here ??
Not sure but I think you have to use
android.text.style.LeadingMarginSpan
For e.g:-
str.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, s.length(), 0);
Look into the following..http://developer.android.com/reference/android/text/style/LeadingMarginSpan.html
Hope this will helps you.
String text = "<body>By clicking Sign Up i agree with Facebook<b>Terms and Conditions</b> and <b>Privacy Policy</b></body>";
Spanned spanned1 = Html.fromHtml(text);
tvTerms.setText(spanned1);
above code is just snippest to use style through html tag hope it wll help you to achieve your requirement that how to give style through html tag in android

Set text of a textview with different sizes

I have TextView and I want each word of the text to be with different size and font.
I know that you can set part of the text to be bold or with underline with the and tags.
But what if I need part of the text to be the size of 18sp and have a custom typeface(from assets) and another part to have a different custom typeface and a size of 14sp.
I can split the TextView to individual TextViews, but I want to avoid that.
You should use RelativeSizeSpan ,for Reference RelativeSizeSpan for different font sizes.
TextView tv;
final SpannableString text = new SpannableString("Hello World");
text.setSpan(new RelativeSizeSpan(2.0f), 0, 5,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new RelativeSizeSpan(1.5f), 5, 11,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
For FontStyle use StyleSpan for reference StyleSpan
So use can use custom typeface with the help of StyleSpan.
text.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start,
end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Hope this will help you.
You can write your code in HTML with all the styling you like and then use Html.fromHtml() in the setText() method:
E.g.:
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
Or:
Refer to this question. it might help you:
Custom Textview with Html text and Custom Font whole Android application

Dynamically change Textview size

Is it possible to change the single textView size dynamically?
In the below link the text size is dynamically changed. ? can anyone guide me on how to do this. thanks in advance
Thanks for all, at-last i used three different textview to design whatever i need. Thanks for all.
Yes. You can use SpannableStringBuilder to styling the text.
Same question is registered at here
Try following code
span.setSpan(new RelativeSizeSpan(0.8f), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Here, you need to set start and end as per your requirement. 0.8f is size of text here. You need to pass float value here.
Code
textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(textView.getText());
span.setSpan(new RelativeSizeSpan(0.8f), 0, 7,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new RelativeSizeSpan(1.8f), 7,
txt.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(span);
Output

Color is lost after adding string format

I need to add some text in a textview with some text with red color and some are black color with a specific formatting
Spannable wordtoSpan = new SpannableString(temp.substring(start, i));
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//failedToSolve.append(wordtoSpan); // was working when I used no formatting
failedToSolve.append(String.format("%7s", wordtoSpan)); // coloring is not working after I apply String.format on it
I know I am formatting Spannable object with String class method. Is there any alternatives?
How can I do the formatting and coloring together? I used HTML tag but no effects. Thanks in advance.
Is there any alternatives?
Call String.format() first, then create the SpannableString from the result and apply the ForegroundColorSpan.

Categories

Resources