output i'm getting.I'm creating app in which I'm setting background color of textview using BackgroundColorSpan. But it's output different than i want.
Code
Spannable spannable = new SpannableString(inputTextView.getText());
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(a);
spannable.setSpan(backgroundColorSpan, 0, inputTextView.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inputTextView.setText(spannable);
And
String s ="<span style='background-color: #FFFFFF; line-height: 2.0;'>"+inputTextView.getText()+"</span>";
inputTextView.setText(Html.fromHtml(s));
Screenshot of website text i want to make in android
BackgroundColorSpan() uses hex value. You need to convert your hash string to hex and use it.
Spannable spannable = new SpannableString(inputTextView.getText().toString());
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#ff0000"));
spannable.setSpan(backgroundColorSpan, 0, inputTextView.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inputTextView.setText(spannable);
Related
I want to change background color of textView with match parent instead of text length in spannable string in android side navigation menu... How can I achieve this? Below is my code:
MenuItem quickAction = menu.findItem(R.id.quickAction);
SpannableString s = new SpannableString(quickAction.getTitle());
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
//for background
s.setSpan(span, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//for background
I have defined TextInputLayout programmatically in my Activity. I have added EditText inside that TextInputLayout, that to programmatically.
Now, I have to set the color of hint of that edittext. Here I just have to change the color of single alphabet of the hint.
Example - Hint is like, Firstname *. Here i want to change the color of "*" to Red and remaining hint as black in color.
I have already tried Html and Spannable String, but both are not working for TextInputLayout.
For Spannable i did
SpannableString redSpannable = new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
Spanned hint = Html.fromHtml(string + redSpannable);
etDefault[j].setHint(hint);
For HTML,
I used "font-color"
If anyone knows this, then share your opinion.
As per the Android Issue Tracker,
Setting the hint to the editText respects the span, but the label does
not float on focus.
editText.setHint(hint);
Setting the hint to the textInputLayout ignores the span
textInputLayout.setHint(hint);
TextInputLayout uses internal class CollapsingTextHelper to draw the
hint, which does not support spans.
And as they said,
They have passed this defect on to the development team and will
update this issue with more information as it becomes available.
In TextInputLayout change the color of hint for single letter or alphabet
String firstNameText ="firstName *";
Spanned redStar;
String html = "<string style=\"color:firstNameText;\">firstName<span style=\"color:red;\">*</span></string>";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.L) {
redStar = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
redStar = Html.fromHtml(html);
}
I suggest you using just SpannableStringBuilder without Html.fromHtml:
SpannableStringBuilder sb = new SpannableStringBuilder("Hint with first letter black");
CharacterStyle cs= new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.black));
sb.setSpan(cs, 0, 1, 0);
((TextView) findViewById(R.id.description_messages)).setHint(sb);
In this piece of code the first letter becomes black. If you would like it to be bold too, use this:
CharacterStyle cs = new StyleSpan(android.graphics.Typeface.BOLD);
That just puts hint to EditText. There will be no floating text effect.
Is it possible to have a TextView with different background colors. Eg. If the text is "This is a test", is it possible to set different background color for the four words?
Yes.
SpannableString spannableString = new SpannableString(getString(R.string.hello_world));
Object greenSpan = new BackgroundColorSpan(Color.GREEN);
Object redSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(greenSpan, 0, 6, 0);
spannableString.setSpan(redSpan, 6, spannableString.length(), 0);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(spannableString);
Produces:
EDIT: There are a lot of different spannable types, you can do much nicer looking things than my basic example. Check out this article.
I'm trying to apply color to different spannablestrings but when I append the strings to the textview only the first one is colored and the rest are ignored.
the variables orange and red are color ints
final ForegroundColorSpan orangeSpan = new ForegroundColorSpan(orange);
final ForegroundColorSpan greySpan = new ForegroundColorSpan(grey);
String orignalName = post.getUser().getUsername();
SpannableString s = new SpannableString(orignalName+"'s");
s.setSpan(orangeSpan, 0, orignalName.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
postView.tvUsername.append(s);
SpannableString endStr = new SpannableString(" post");
endStr.setSpan(greySpan, 0,3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
postView.tvUsername.append(endStr);
In my layout, I have defined a TextView and have given it the id - textName.
Programmatically, I have set its text to My name is Blah. I was wondering if it is possible to programmatically increase the textsize of just Blah and not the whole TextView?
Or simply using the Spannable class:
String text = textView.getText();
Spannable span = new SpannableString(text);
span.setSpan(new RelativeSizeSpan(1.5f), text.indexOf("Blah"), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
Yes!! you can do by formatting the string with HTML, refer https://stackoverflow.com/a/1533512/603233
like eg
String text = "<h5>My name is </h5><h1>Blah</h1>";
yourtextview.setText(Html.fromHtml(text));