I have a Textview with some texts. So if the line count is more than two i have to fade some characters at the end of second line.
How can we do something like that?
You can use html font color like that :
String text = textView.getText().toString();
String a = text.substring(0, text.length()-2);
String b = text.substring(text.length()-2, text.length()-1);
String c = text.substring(text.length()-1, text.length());
text = "<font color=#000000>"+a+"</font><font color=#777777>"+b+"</font>"+"<font color=#AAAAAA>"+c+"</font>";
textView.setText(Html.fromHtml(text));
Result screenshot :
Related
I want to change text color before displaying it to TextView.
For example,I am getting a text from server like
<b>Pratik<b/> has shared photo with <b>you</b>.
Now the requirements are to display Pratik and you with Bold style and Blue text color. I tried several ways using <span> tag, but I am not getting clear way to display it.
String htmlBody = "<b>Pratik<b/> has shared photo with <b>you</b>.";
String formattedBody = "<span>" + htmlBody + "</span><style>b { color: #309ce8 ; } span {font-family: 'Avenir', 'Heavy'; font-size: 17; }</style>";
SpannableString text = new SpannableString(formattedBody);
tvMessage.setText(text, TextView.BufferType.SPANNABLE); // This is not working as expected.
tvMessage.setText(Html.fromHtml(htmlBody)); // This is not working as expected.
Help me achieve that.
String htmlBody = "<b style='color:blue !important;'>Pratik</b> has shared photo with <b style='color:blue !important;'>you</b>.";
Your Solution is:
String styledText = "<b><font color='red'>Pratik</font><b/> has shared photo with <b><font color='red'>you</font></b>";
You have to use like this
public void methodName() {
String html = "Some text <b>here</b>, and some <b>there</b>";
String result = html.replace("<b>","<font color=\"#ff0000\"><b>").replace("</b>", "</b></font>");
textView.setText(Html.fromHtml(result));
}
I have a String I have to display in a TextView with Html.fromHtml(), but it contains sometimes a few \n at the end of the text, so TextView shows empty lines.
I tried to remove \n by
text = text.replace('\n', ' ');
text = text.replace("\n", "");
text = text.replace("\\n", "");
text = text.replaceAll("\\n", "");
text = text.replaceAll("\\\\n", "");
than I insert the text into TextView like this
tv.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY, null, new CustomTagHandler(context, cp)));
But non of these removed the \n from the end of the text. When I check my layout at Layout inspector I can see the TextView's content, and the \n at the end of the text.
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.
everyone. I am trying to set the color of a few words in a string using spannablestring.But for some reason I cant get the color of the string to change.
I have attached the relevant code
String color = "some string"
SpannableString span_color= new SpannableString(color);
span_color.setSpan(new ForegroundColorSpan(Color.GREEN),0,11,0);
String print = "This is the whole string"+span_color;
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(reminder);
In the Text View, I want the "This is the whole string" to be in the default color and the "some string" to be in green. but this is not happening.I am getting the entire string in the default color.I seem to be missing something important , but I cant figure out what .
Any suggestion?
Using "+" operator you get just string, not SpannableString. You should do something like this:
String color = "This is the whole string, color string";
SpannableString spanColor = new SpannableString(color);
spanColor.setSpan(new ForegroundColorSpan(Color.GREEN), color.indexOf("color string"), color.length(), 0);
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(spanColor);
I need to print the below string which is in Arabic on TextView in Android. It is printing good but when the Arabic text and digits falls in a same string, Android put the digit at end!
here is my code
String str = "مقر 44";
TextView textView = (TextView)findViewById(R.id.test);
textView.setText(str);
Here is the output
you can try with this workaround
String str = "44 "+"مقر ";
TextView textView = (TextView)findViewById(R.id.txt_title);
textView.setText(str);
its not Android who put digit at the end, its because of Arabic writing standard