I'm trying to have the layout of the comments on a progam in the following form:
I want the USER_NAME in Bold, and the DATE in Enphasis (The red part should be normal, I don't want it red). My problem comes with the red part. I cant get the cooment to be displayed like this using layouts, I've been able to get:
and
Is there any way to get the comment working like the one I'm showing. I've thought about a multi formatted textView, is it posible??
If getting the comment like this is not possible, just say it's imposible.
One possible solution is to put two TextViews into a Linear layout one after another and then use SpannableStringBuilder to assign the formatted text to the first view.
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(text);
sb.setSpan(createBoldSpan(), 0, lengthOf(userName),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
firstTextView.setText(sb, TextView.BufferType.NORMAL);
createBoldSpan should return TextAppearanceSpan
private TextAppearanceSpan userNameSpanInBold(String userName) {
return new TextAppearanceSpan(...);
}
please follow API docs for TextAppearanceSpan - you can create one using custom style.
You could use a WebView with loadData if HTML markup would be good, or alternatively delve into the realm of Spannable text.
I've not worked with Spannable text myself, but essentially it allows you to attach attributes (e.g. styles & colors) to parts of text in a TextView.
The relevant method is TextView.setText(CharSequence text, TextView.BufferType type) with BufferType of SPANNABLE. The documentation I've seen is.... confusing.
Related
I have two spannable object for same string and both have different styles. I need to merge all spannable objects and display all styles into TextView.
From one of returning from Html.fromHtml() and second return StyleSpan(Bold).
I tried TextUtils.concate(htmlSpan, boldSpan) but it displayed same string two times because it append spannable, i want to display my all styles into single string.
Use SpannableStringBuilder but you can create spans inside the append method
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(String.valueOf(Html.fromHtml(stringToConvert));
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
And then just set sb to your view
textView.setText(sb);
EDIT:
I saw your edited question, Above I rewrote this code to get it to apply two spannables on one string. I think you cannot merge different spannable objects on one string. I believe a workaround would be to apply one span, then convert in to string and then apply the second span. This way it will apply two spans to one string. Or you can manage your bold text type via xml if you always need it in bold
Finally i found my solution, Html.fromHtml() not have spannable type parameter, so firstly i got SpannableStringBuilder from Html.fromHtml() and carry forward it into create Bold StyleSpannable method then set into TextView, and it works awesome.
It retain HTML spannable into bold StyleSpannable method.
Thanks guys for your feed back and response of my answer.
SpannableStringBuilder spannable = setTextViewHTML(content);
common.convertBoldString(spannable);
textView.setText(spannable);
For the above code content is a variable, which contain words which i like to bold and it also contain HTML tags.
convertBoldString is method in my Common methods class which has parameter
SpannableStringBuilder and this object use to set bold StyleSpan.
I have a long string, with multiple bold words inside the string.
The styling of the non bold sentences are done inside the TextView XML part and this is looking fine.
But i want to make the bold words bigger and thicker.
Is this possible to change this with XML?
for example:
<string name="long_text">This should be a <b>long</b> text with different <b>styling</b></string>
The bold words are supposed to be 1.5 times bigger
Or should i do this in the Java code?
yes you have to do this in java because there is no way to as per your requirement,
you can use TextAppearanceSpan in java code with Spannable
if i am getting you right than try the following..
textview.setText(Html.fromHtml(getResources().getString(R.string.long_text)));
Mark as right if it works for you .
I've searched something around here but nothing came up.
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part.
I've already tried with the spannable thing, from another question:
TextView myTV = (TextView)findViewById(R.id.test);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
I assume that I have to assign it to some onClick method..Also my problems are those two number, I should put some "selectedText" there instead I think.
Is this even possible?
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part
If you want the user to "change attributes... of a selected part", my recently-updated RichEditText offers that. I do not have color or text size going yet, though, as those will need a toolbar (rather than my current action mode support).
Also my problems are those two number, I should put some "selectedText" there instead I think
You are probably looking for getSelectionStart() and getSelectionEnd().
Is there anyway to set the color of a string resource in android? I mean, I know I can use some html tags to change string style (or substrings) but have not found any to change color. I have seen other solutions here at stackoverflow like passing the string to Html.fromHtml(string) before setting the text but I want to do it in the string resource editor. Any possibility?
It looks like this method is working:
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
As far as I know it is not possible. I would use a SpannableString to change the color.
int colorBlue = getResources().getColor(R.color.blue);
String text = getString(R.string.text);
SpannableString spannable = new SpannableString(text);
// here we set the color
spannable.setSpan(new ForegroundColorSpan(colorBlue), 0, text.length(), 0);
Spannable is really nice. You can set thinks like fontseize and stuff there and just attach it to a text view. The advantage is that you can have different colors in one view.
Edit: Ok, if you only want to set the Color the solution mentioned above me is the way to go.
The strings themselves have no color, but you can change the color of the text in the textView they appear in. See the textview documentation, but there are 2 ways to do it.
XML
android:textColor
Code
setTextColor(int)
I have recently made a flexible solution for this problem. It enables me of easily add multiple styles to substrings by using method chaining. It makes use of the SpannableString. When you want to give a certain substring a color, you can use ForegroundColorSpan.
public StyledString putColor(String subString, #ColorRes int colorRes){
if(getStartEnd(subString)){
int color = ColorUtil.getColor(context, colorRes);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
fullStringBuilder.setSpan(foregroundColorSpan, startingIndex, endingIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return this;
}
For full code see gist.
i want to put large text inside textView. The text has multiple section with multiple colours. i have seen Html.fromHtml() function and also know how to use setSpan(). But any of these not work for me. In my case i dont know the id of text View at runtime. As i am inflating different Views at runtime each View has many textViews. It will be better if i can find a way to set styles in strings.
Please some one help me out i have spend so much time here.
you can set id of text view weather the view is find by inflater
when you get text view using inflater then you can set id of that particular text view
and you can set textSize() and other attributes
This method creating spannables directly with the attributes you need will work.
Basically you can set colors like this, given a TextView called text:
String greeting = "Hello World!"
SpannableString str = SpannableString.valueOf(greeting);
str.setSpan(new ForegroundColorSpan(0xffffffff), 0, greeting.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xff0099ff), 0, greeting.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.append(str);