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
Related
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
I want to add small upper text in android textView
how would you implement this?
two textViews both layout_below the same element and then
make the second smaller?
or is there a way in one textView to put style to one word
(can be longer or shorter in other languages)?
You can achieve that by following code
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("2580<sup>TH</sup>"));
Solution from Android Layout framework, would put two textViews in side a Linear layout and that is it.
Another way, is making the small text as an image. Than you can add it to text view directly.
you can do it like this:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("YourRank:250<sup>TH</sup>"));
also you can do it like this:
String str=getString(R.string.rank); //YourRank:250
String str2=getString(R.string.superScript); //TH
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("+str+"<sup>"+str2+"</sup>"));
it worked like charm!
You can use SpannableStringBuilder as fallows:
String text = "YourRank:250TH";
SpannableStringBuilder spannableString = new SpannableStringBuilder(text);
spannableString.setSpan(new SuperscriptSpan(), text.length() - 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
YourTextView.setText(spannableString);
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
Is it possible to have texts with different sizes, font-types or styles in the same TextView ?
Something like this:
| myLogin logout |
You can do this using:
textView.setText(Html.fromHtml("<b>myLogin</b> <i>logout</i>"));
For more options, look into SpannableString: Link
With SpannableString, you can apply multiple formatting to a single string.
This article will be very helpful to you: Rich-Style Formatting of an Android TextView
For anyone who wants to do this without the HTML formatting , use a SpannableString.
As in :
SpannableString styledString = new SpannableString("myLogin logout");
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, 0);
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 8, 14, 0);
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(styledString);
More examples can be found here
Unless you make a custom TextView, no. Consider using two different TextViews if you do not want to create a custom TextView.
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.