How ist it possible to colour the newText "e"?
public void onClick(View v) {
String text = tw.getText().toString();
int n = 4;
String newText = text.substring(0, n) + "e" + text.substring(n + 1);
tw.setText(newText);
You need to use a ForegroundColorSpan
final String text = "something something text";
final int n = 4;
final String newText = text.substring(0, n) + "e" + text.substring(n + 1);
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.RED);
final SpannableString ss = new SpannableString(newText);
ss.setSpan(fcs, n, n + 1, 0);
tv.setText(ss);
yes, if you formate the String with html
You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
For example:
String text = "<font color=#cc0029>Exempl</font> <font color=#ffcc00>e</font>";
myTextView.setText(Html.fromHtml(text);
Related
working perfect but
when i am highlight text using html then some text can not be view perfect(Hindi text).
android
String str="रिश्ते भले ही कम ही बनाओ लेकिन दिल से निभाओ,\n" +
"क्योंकि आज कल इंसान अच्छाई के चक्कर में अच्छे खो देते है।";
//textview.setText(str);
textview.setText(Html.fromHtml(String.format(colorfulltext(str))), TextView.BufferType.SPANNABLE);
// highlight text
public String colorfulltext(String text) {
String[] colors = new String[]{"#fdc113", "#fdc113", "#fdc113","#fdc113", "#fdc113" ,"#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc", "#fcfcfc","#fcfcfc","#fcfcfc","#fcfcfc","#fcfcfc"};
StringBuilder finals = new StringBuilder();
int size = colors.length;
int k = 0;
for (int item = 0; item < text.length(); item++) {
if (k >= size) {
k = 0;
}
finals.append("<font color='" + colors[k] + "'>" + text.charAt(item) + "</font>");
k++;
}
return finals.toString();
}
screen
Why do you convert a String to html to apply fontcolor for a static text??
You have to follow the following steps:
Create entries in Strings.xml for each text. For instance, रिश्ते
has a different color and needs to be a separate entry in
strings.xml.
add this to a Util class:
public static void addColoredPart(SpannableStringBuilder ssb,
String word, int color,String... texts) {
for(String text : texts) {
if (word != null) {
int idx1 = text.indexOf(word);
if (idx1 == -1) {
return;
}
int idx2 = idx1 + word.length();
ssb.setSpan(new ForegroundColorSpan(color), idx1, idx2,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
apply style the following way:
String string1 = context.getString(R.string.String_id1)
String string2 = context.getString(R.string.String_id2)
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append(string2)
spannableStringBuilder.append(string2)
SpannableUtil.addColoredPart(
spannableStringBuilder,
spannableStringBuilder.toString(), color, string1, string2);
I have a text and an array (textviewArray) with 7 words. In my text exist these 7 words. I want to show these 7 words in this text in bold.
I have this code but the last word is bold, not all seven words:
textviewS.Text = "My Text ....";
strD = new SpannableStringBuilder("My Text ....");
bss = new StyleSpan(TypefaceStyle.Bold);
for (int i = 0; i < 7; i++)
{
if (textviewS.Text.IndexOf(textviewArray[i]) >= 0)
{
strD.SetSpan(bss, textviewS.Text.IndexOf(textviewArray[i]), (textviewS.Text.IndexOf(textviewArray[i]) + textviewArray[i].Length), 0);
}
}
textviewُ.SetText(strD, TextView.BufferType.Normal);
Try
String text = "text here";
String[] wordsToBold = {"word1", "word2", "word3", "word4", "word5", "word6", "word7"}
SpannableString spanString = FontUtility.getMultiFontText(context, "fonts/myfont.ttf", text, wordsToBold);
public static SpannableString getMultiFontText(Context context, int fontPathFromAssets, String text, String[] wordsToBold){
SpannableString spannableString = new SpannableString(text);
for(String word : wordsToBold) {
int startIndex = text.indexOf(word);
while(startIndex >= 0) {
int endIndex = startIndex+word.length();
spannableString.setSpan(new CustomTypefaceSpan(context, context.getString(fontPathFromAssets)), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
startIndex = text.indexOf(word, endIndex + 1);
}
}
return spannableString;
}
I want to set a selected text in an EditText bold.
It's already possible to find out what characters I selected and with getSelectionStart() and getSelectionEnd( ) I know where's the position.
But my problem is, that I want to set the selected Text bold with a Button and don't know how to set the font bold with a String (I only know how to set it with EditText).
So here is my sample code:
fettdruckButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectionStart = fullscreenEdittext.getSelectionStart();
int selectionEnd = fullscreenEdittext.getSelectionEnd();
int differenz = selectionEnd - selectionStart;
String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);
Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd);
//fullscreenEdittext.setTypeface(null, Typeface.BOLD);
}
});
Use SpannableStringBuilder.
SpannableStringBuilder stringBuilder = (SpannableStringBuilder) fullscreeneditText.getText();
stringBuilder.setSpan(new StyleSpan(Typeface.BOLD), selectionStart, selectionEnd, 0);
Please note that if you want to do this many times you should remove your original span first.
String completetext=fullscreenEdittext.getText().toString();
int selectionStart = fullscreenEdittext.getSelectionStart();
int selectionEnd = fullscreenEdittext.getSelectionEnd();
int differenz = selectionEnd - selectionStart;
String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);
Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd);
String part1=fullscreenEdittext.getText().toString().substring(0, selectionStart);
String part2=fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);
String part3=fullscreenEdittext.getText().toString().substring(selectionEnd,completetext.length() );
fullscreenEdittext.setText(Html.fromHtml(part1+"<b>" + part2+ "</b>" +part3));
Use the property of Editext fullscreenEdittext.setText(Html.fromHtml(styledText));
Use html formatting tags <b>selectedText</b> to make the selected text bold.
Please have a look at below code snippet
int selectionStart = fullscreenEdittext.getSelectionStart();
int selectionEnd = fullscreenEdittext.getSelectionEnd();
String startingText = fullscreenEdittext.getText().toString()
.substring(0, selectionStart);
String selectedText = fullscreenEdittext.getText().toString()
.substring(selectionStart, selectionEnd);
String endingText = fullscreenEdittext.getText().toString()
.substring(selectionEnd);
fullscreenEdittext.setText(Html.fromHtml(startingText + "<b>"
+ selectedText + "</b>" + endingText));
I want to add underline and colour to textview, but it is a simple text, not link, not phone number, just simple "Hello world", that I want to have with underline and that blue link-like colour.
It failed to do so:
view.setMovementMethod(LinkMovementMethod.getInstance());
Linkify.addLinks(view, Linkify.ALL);
Thank you! I just underlined the text as Const suggested and changed color of textview. But I guess xoxol_89's answer is correct in my case and should be accepted.
Do you try to use Spannable
For example
/**
* Method allocates filtering substring in all contacts yellow color,
* that satisfy the user's search
* #param inputText - DisplayName
* filtText - filtering Text
* #return String with allocating substring (Spannable)
*/
public static Spannable changeBackgroungFiltText(CharSequence inputText, String filtText, int color) {
Spannable str = null;
if(inputText != null)
{
String inputStr = inputText.toString();
String inputLowerCaseStr = inputStr.toLowerCase();
String filtLowerCaseStr = filtText.toLowerCase();
// Spannable str = new SpannableStringBuilder(inputStr);
str = new SpannableStringBuilder(inputStr);
if (filtText.length() != 0)
{
int indexStart = 0;
while (true)
{
int indexCur = inputLowerCaseStr.indexOf(filtLowerCaseStr, indexStart);
if (indexCur != -1) {
int start = indexCur;
int end = indexCur + filtText.length();
int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
str.setSpan(new ForegroundColorSpan(color),start, end, flag);
//str.setSpan(new BackgroundColorSpan(highlightColor), start, end, flag);
indexStart = indexCur + 1;
} else {
return str;
}
}
} else {
return str;
}
}
return str;
}
You can do that in 4 ways:
1.Automatically linkifies using android:autoLink=”all”
2.Link text by setMovementMethod
3.Link as html code using Html.fromHtml()
4.Link string by SpannableString
and you can find the examples here
You can use SpannableString like this:
final SpannableString text = new SpannableString("Hello World!");
final int startAt = 0;
final int endAt = text.length();
final int sampleColor = Color.parseColor("#3333ff");
text.setSpan(new UnderlineSpan(), startAt, endAt, 0);
text.setSpan(new ForegroundColorSpan(sampleColor), startAt, endAt, 0);
textView.setText(text);
Hi iam trying to change part of the text in textview to some color but iam unble to achieve it
i tried fromHtml , spannable and different methoda but its still not working.
My tries are as below:
try1
Spannable wordtoSpan = Spannable.Factory.getInstance().newSpannable("text 6 Months(180 days) or 8,000 kilometers.");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordtoSpan);
try2
String str = "#Second #Service:6 Months(180 days) or 8,000 kilometers.";
String[] str_array = str .split(" ");
boolean isExists = false;
for (int i = 0; i < str_array.length; i++) {
for (int j = 0; j < i; j++) {
if (str_array[j].equals(str_array[i])) {
isExists = true;
}
}
if (str_array[i].startsWith("#") && !isExists) {
str = str .replace(str_array[i],
"<font color='#8A0A0A'>" + str_array[i]
+ "</font>");
} else if (str_array[i].contains("#")) {
str = str .replaceAll(str_array[i],
"<font color='#000000'><b>" + str_array[i]
+ "</b></font>");
}
}
tv.setText(Html.fromHtml(str));
}
try3
myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText()));
try4
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//method1
TextView tv = (TextView) findViewById(R.id.service);
tv.setText(Html.fromHtml("<font color=\"#FFFFFF\">" + "give your string here long long longlong" + "</font>" + "<font color=\"#47a842\"></font>"));
//method 2
//SpannableStringBuilder WordtoSpan = new SpannableStringBuilder("give your string here long long longlong");
//WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//tv.setText(WordtoSpan);
}
Any help is really appreciated.Thank you
Try like this. The below code is perfectly working for me, i have tested it.
TextView ttt = (TextView) findViewById(R.id.textView11);
String styledText = "This is <font color='red'>simple</font>.";
ttt.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
and here is output.
Try this it is running in my app perfectly.. May be it help you too
SpannableStringBuilder WordtoSpan = new SpannableStringBuilder(give your string here);
WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW),starting Index, and the index of text from which you want to highlight you text,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView.setText(WordtoSpan);