I want to change background color of a link in textview which was created using following code:
String htmlStr = "Click here: SO";
Spanned htmlSpanned = Html.fromHtml(htmlStr, this, null);
txtView.setText( htmlSpanned );
I used font tag but it didn't work.
Try using some css codes
The font tag can't change the background color of html links,
Try this code:
String htmlStr = "Click here: <a style='background-color:#00ff00' href=\'http://stackoverflow.com\'>SO</a>";
Spanned htmlSpanned = Html.fromHtml(htmlStr, this, null);
txtView.setText( htmlSpanned );
String htmlStr = "Click here: ";
String link = "<font color='blue'><a href=\'http://stackoverflow.com\'>SO</a></font>";
Spanned htmlSpanned = Html.fromHtml(htmlStr + link, this, null);
txtView.setText( htmlSpanned );
You can use the font tag to add color. Also can do something like:
int сolor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2);
Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString),
This grabs a color resource and strips it of the alpha channel value. Useful if you want to use a particular color else where in your project
how can i set half bold text by html.fromhtml i try but didn't work in append of BufferString out append its work fine please help me here is code
String amt = "AMOUNT: ";
Spanned tyyy =Html.fromHtml("<b>"+"TYPE: "+"</b>");
String Dat = "DATE: ";
String des = "DESCRIPTION: ";
buffer.append(Html.fromHtml("<b>"+amt+"</b>"+"<small>"+cc.getString(cc.getColumnIndex("AMOUNT"))+"</small>")+"\n");
buffer.append(tyyy+cc.getString(cc.getColumnIndex("TYPE"))+"\n");
If it doesn't need to be from HTML, you can use SpannableStringBuilder:
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("TYPE:");
// bold the current text
int boldStartIndex = 0;
int boldEndIndex = bulder.length();
builder.setSpan(new StyleSpan(Typeace.BOLD, boldStartIndex, boldEndIndex, Spanned.FLAG_EXCLUSIVE_EXCLUSIVE));
// append the rest, it will not be bolded
builder.append("the-rest-of-your-data");
You can then set the builder directly to a TextView via TextView.setText(builder);. This may be a better approach than using HTML, as HTML tags support isn't very well documented and may not be supported across various devices.
i have one EditText and entered some text.
view.editText.setTextColor(Color.RED);
Html.toHtml(editText.getText())
convert it to html text.
o/p
text color change
i have getting only paragragh but not the selected color.
Because,
editText.getText() just returns Editable character sequence not a Html rich string.
String sample=editText.getText();
TextView str = new TextView(null);
str.setText(sample);
str.setTextColor(Color.RED);
editText.setText(str.getText().tostring());
or use this
Html.fromHtml("<font color = #ffffff>"
+ getResources().getString(editText.getText().tostring()) + "</font>")
thank you.
In my application I have to use a some HTML stuff in a string. But HTML is not working as intended. I have to use that string (Text) to send as an email. The sequence I required of HTML is:
Title (in the center)
Image (in the center)
Description (left align)
and then this HTML string is passed to an email intent. But neither image is showing up in the email nor the title text is getting center align. This is how I am doing this all:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "");
it.setType("text/html");
String title = title;
String emailText = emailText;
it.putExtra(Intent.EXTRA_SUBJECT, title);
it.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailText));
this.startActivity(it);
and this is how the emailText is being formed:
emailText = "<p style= 'color:#000000; font:Georgia; font-size:18pt; text-align:center' align = 'center'><b>" + title +" </b></p>"
+"<br/><br />"
+"<img style=\"border:3px solid #173E8C\" src=\'" +imageUrl+"\' width=\"120\" height=\"90\"align=\"center\"/>"
+"<br/><br/>"
+"<p>" + description;
But I am unable to get the required result that I have mentioned right at the top, Any help is appreciated related to the issue. Thanks in advance..:-)
You must specify the type of email through the function setType () :
it.setType("text/html"); // for HTML
it.setType("text/plain"); // for plain text
You can't send a image as email body in android through Intent.
I am developing an application in which there will be a search screen
where user can search for specific keywords and that keyword should be
highlighted. I have found Html.fromHtml method.
But I will like to know whether its the proper way of doing it or
not.
Please let me know your views on this.
Or far simpler than dealing with Spannables manually, since you didn't say that you want the background highlighted, just the text:
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
Using color value from xml resource:
int labelColor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2); // !!strip alpha value!!
Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString), TextView.BufferType.SPANNABLE);
This can be achieved using a Spannable String. You will need to import the following
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
And then you can change the background of the text using something like the following:
TextView text = (TextView) findViewById(R.id.text_login);
text.setText("");
text.append("Your text here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);
Where this will highlight the charecters at pos 1 - 4 with a red color. Hope this helps!
Alternative solution: Using a WebView instead. Html is easy to work with.
WebView webview = new WebView(this);
String summary = "<html><body>Sorry, <span style=\"background: red;\">Madonna</span> gave no results</body></html>";
webview.loadData(summary, "text/html", "utf-8");
String name = modelOrderList.get(position).getName(); //get name from List
String text = "<font color='#000000'>" + name + "</font>"; //set Black color of name
/* check API version, according to version call method of Html class */
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
Log.d(TAG, "onBindViewHolder: if");
holder.textViewName.setText(context.getString(R.string._5687982) + " ");
holder.textViewName.append(Html.fromHtml(text));
} else {
Log.d(TAG, "onBindViewHolder: else");
holder.textViewName.setText("123456" + " "); //set text
holder.textViewName.append(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)); //append text into textView
}
font is deprecated use span instead Html.fromHtml("<span style=color:red>"+content+"</span>")
To make part of your text underlined and colored
in your strings.xml
<string name="text_with_colored_underline">put the text here and <u><font color="#your_hexa_color">the underlined colored part here<font><u></string>
then in the activity
yourTextView.setText(Html.fromHtml(getString(R.string.text_with_colored_underline)));
and for clickable links:
<string name="text_with_link"><![CDATA[<p>text before linktitle of link.<p>]]></string>
and in your activity:
yourTextView.setText(Html.fromHtml(getString(R.string.text_with_link)));
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
First Convert your string into HTML then convert it into spannable. do as suggest the following codes.
Spannable spannable = new SpannableString(Html.fromHtml(labelText));
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), spannable.toString().indexOf("•"), spannable.toString().lastIndexOf("•") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(Html.fromHtml("<font color='rgb'>"+text contain+"</font>"));
It will give the color exactly what you have made in html editor , just set the textview and concat it with the textview value. Android does not support span color, change it to font color in editor and you are all set to go.
Adding also Kotlin version with:
getting text from resources (strings.xml)
getting color from resources (colors.xml)
"fetching HEX" moved as extension
fun getMulticolorSpanned(): Spanned {
// Get text from resources
val text: String = getString(R.string.your_text_from_resources)
// Get color from resources and parse it to HEX (RGB) value
val warningHexColor = getHexFromColors(R.color.your_error_color)
// Use above string & color in HTML
val html = "<string>$text<span style=\"color:#$warningHexColor;\">*</span></string>"
// Parse HTML (base on API version)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(html)
}
}
And Kotlin extension (with removing alpha):
fun Context.getHexFromColors(
colorRes: Int
): String {
val labelColor: Int = ContextCompat.getColor(this, colorRes)
return String.format("%X", labelColor).substring(2)
}
Demo