textstyle style settextappearance part code android android-textview android-textattributes

style - textview bold android code



cómo hacer un texto específico en TextView BOLD (14)

No sé cómo convertir un texto específico en TextView en BOLD.

Es como esto

txtResult.setText(id+" "+name);

Quiero que la salida sea así:

1111 neil

id y name son variables que he recuperado el valor de la base de datos, y quiero hacer que el id esté en negrita, pero solo la id para que el name no se vea afectado, no tengo ni idea de cómo hacerlo.


Creé un método estático para configurar una parte del texto Bold para TextView y EditText

public static void boldPartOfText(View mView, String contentData, int startIndex, int endIndex){ if(!contentData.isEmpty() && contentData.length() > endIndex) { final SpannableStringBuilder sb = new SpannableStringBuilder(contentData); final StyleSpan bss = new StyleSpan(Typeface.BOLD); // Span to make text bold final StyleSpan iss = new StyleSpan(Typeface.NORMAL); //Span to make text normal sb.setSpan(iss, 0, startIndex, Spanned.SPAN_INCLUSIVE_INCLUSIVE); sb.setSpan(bss, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold sb.setSpan(iss,endIndex, contentData.length()-1, Spanned.SPAN_INCLUSIVE_INCLUSIVE); if(mView instanceof TextView) ((TextView) mView).setText(sb); else if(mView instanceof EditText) ((EditText) mView).setText(sb); } }

Otro código más personalizado

/*typeFaceStyle can be passed as Typeface.NORMAL = 0; Typeface.BOLD = 1; Typeface.ITALIC = 2; Typeface.BOLD_ITALIC = 3;*/ public static void boldPartOfText(View mView, String contentData, int startIndex, int endIndex,int typeFaceStyle){ if(!contentData.isEmpty() && contentData.length() > endIndex) { final SpannableStringBuilder sb = new SpannableStringBuilder(contentData); final StyleSpan bss = new StyleSpan(typeFaceStyle); // Span to make text bold final StyleSpan iss = new StyleSpan(Typeface.NORMAL); //Span to make text italic sb.setSpan(iss, 0, startIndex, Spanned.SPAN_INCLUSIVE_INCLUSIVE); sb.setSpan(bss, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold sb.setSpan(iss,endIndex,contentData.length()-1,Spanned.SPAN_INCLUSIVE_INCLUSIVE); if(mView instanceof TextView) ((TextView) mView).setText(sb); else if(mView instanceof EditText) ((EditText) mView).setText(sb); } }


Aquí hay una mejor solución si quiere hacer que el texto en negrita sea múltiple. He mejorado el código de Eitan. gracias Eitan.

public static SpannableStringBuilder makeSectionOfTextBold(String text, String... textToBold) { SpannableStringBuilder builder = new SpannableStringBuilder(text); for (String textItem : textToBold) { if (textItem.length() > 0 && !textItem.trim().equals("")) { //for counting start/end indexes String testText = text.toLowerCase(Locale.US); String testTextToBold = textItem.toLowerCase(Locale.US); int startingIndex = testText.indexOf(testTextToBold); int endingIndex = startingIndex + testTextToBold.length(); if (startingIndex >= 0 && endingIndex >= 0) { builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0); } } } return builder; }


Basado en la respuesta de @mladj0ni, obtuve el siguiente código para que funcione. El problema es que si utiliza String.format , elimina el marcado HTML, por lo que debe escapar de los símbolos del paréntesis en strings.xml:

strings.xml:

<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>

code.java:

String unspanned = String.format(Locale.US, "%s%s", getResources().getString(R.string. welcome_messages), 99); Spanned spanned; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { spanned = Html.fromHtml(unspanned, Html.FROM_HTML_MODE_LEGACY); } else { spanned = Html.fromHtml(unspanned); } textView.setText(spanned);

Es más simple que SpannableStringBuilder. En cuanto al rendimiento, si solo muestra una cadena, el usuario no notará el milisegundo extra para analizarla.

Vea la documentación here .


Como dijo wtsang02, usar HTML es una costosa sobrecarga. Solo usa la solución nativa. Si no tiene que modificar la cadena, solo use SpannableString, no SpannableStringBuilder.

String boldText = "id"; String normalText = "name"; SpannableString str = new SpannableString(boldText + normalText); str.setSpan(new StyleSpan(Typeface.BOLD), 0, boldText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(str);


En caso de que quiera usar la cadena de XML, puede hacer algo como esto:

strings.xml

<string name="test"> <![CDATA[ <b>bold!</b> normal ]]> </string>

Código:

textView.setText(Html.fromHtml(getString(R.string.test)));


Es simple cerrar el texto especificado como este por ejemplo <b>"your text here:"</b>

<string name="headquarters">"<b>"Headquarters:"</b>" Mooresville, North Carolina, U.S.</string>

resultado: Sede: Mooresville, Carolina del Norte, EE. UU.


Haga que el primer carácter de cadena se pueda abrir mientras busca un objeto en la lista / reciclador como

r a vi y ajay

anteriormente destacando así, pero yo quería ser como a continuación

ravi y ajay o ravi y jay

para esto busqué la longitud de la palabra si es igual a 1, separé la cuerda principal en palabras y la posición de inicio de la palabra se calculó, luego busqué una palabra que comenzara con char.

public static SpannableString colorString(int color, String text, String... wordsToColor) { SpannableString coloredString = new SpannableString(text); for (String word : wordsToColor) { Log.e("tokentoken", "-wrd len-" + word.length()); if (word.length() !=1) { int startColorIndex = text.toLowerCase().indexOf(word.toLowerCase()); int endColorIndex = startColorIndex + word.length(); try { coloredString.setSpan(new ForegroundColorSpan(color), startColorIndex, endColorIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.getMessage(); } } else { int start = 0; for (String token : text.split("[/u00A0 /n]")) { if (token.length() > 0) { start = text.indexOf(token, start); // Log.e("tokentoken", "-token-" + token + " --start--" + start); char x = token.toLowerCase().charAt(0); char w = word.toLowerCase().charAt(0); // Log.e("tokentoken", "-w-" + w + " --x--" + x); if (x == w) { // int startColorIndex = text.toLowerCase().indexOf(word.toLowerCase()); int endColorIndex = start + word.length(); try { coloredString.setSpan(new ForegroundColorSpan(color), start, endColorIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.getMessage(); } } } } } } return coloredString; }


Pensé que la respuesta elegida no proporcionaba un resultado satisfactorio. He escrito mi propia función que requiere 2 cadenas; El texto completo y la parte del texto que desea resaltar.

Devuelve un SpannableStringBuilder con ''textToBold'' de ''texto'' en negrita.

Encuentro la capacidad de hacer una subcadena negrita sin envolverla en etiquetas útiles.

/** * Makes a substring of a string bold. * @param text Full text * @param textToBold Text you want to make bold * @return String with bold substring */ public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){ SpannableStringBuilder builder=new SpannableStringBuilder(); if(textToBold.length() > 0 && !textToBold.trim().equals("")){ //for counting start/end indexes String testText = text.toLowerCase(Locale.US); String testTextToBold = textToBold.toLowerCase(Locale.US); int startingIndex = testText.indexOf(testTextToBold); int endingIndex = startingIndex + testTextToBold.length(); //for counting start/end indexes if(startingIndex < 0 || endingIndex <0){ return builder.append(text); } else if(startingIndex >= 0 && endingIndex >=0){ builder.append(text); builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0); } }else{ return builder.append(text); } return builder; }


Primero: no necesita preocuparse por usar el código de rendimiento lento de la respuesta de Raghav Sood .

Segundo: no necesita escribir una función de extensión proporcionada por la respuesta de w3bshark cuando usa Kotlin.

Finnaly: Todo lo que necesita hacer es usar la biblioteca Kotlin android-ktx de Google:

// Suppose id = 1111 and name = neil (just what you want). val s = SpannableStringBuilder() .bold { append(id) } .append(name) txtResult.setText(s)

Produce: 1111 neil


Puede agregar las dos cadenas por separado en el generador, una de ellas es spannedString, la otra es una regular. De esta forma, no tiene que calcular los índices.

val instructionPress = resources?.getString(R.string.settings_press) val okText = resources?.getString(R.string.ok) val spannableString = SpannableString(okText) val spannableBuilder = SpannableStringBuilder() spannableBuilder.append(instructionPress) spannableBuilder.append(spannableString, StyleSpan(Typeface.BOLD), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) instructionText.setText(spannableBuilder,TextView.BufferType.SPANNABLE)


Si bien puedes usar Html.fromHtml() puedes usar un enfoque más nativo que sea SpannableStringBuilder , esta post puede ser útil.

SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text"); str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), INT_START, INT_END, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TextView tv=new TextView(context); tv.setText(str);


Simplemente crea tu String en HTML y configúralo:

String sourceString = "<b>" + id + "</b> " + name; mytextview.setText(Html.fromHtml(sourceString));


Vine aquí para ofrecer una solución más actualizada, porque no estaba satisfecho con las respuestas existentes. Necesitaba algo que funcionara para textos traducidos y no tiene el impacto en el rendimiento de usar Html.fromHtml() . Si está usando Kotlin, aquí hay una función de extensión que establecerá fácilmente varias partes de su texto en negrita . Esto funciona igual que Markdown, y podría ampliarse para admitir otras etiquetas Markdown, si fuera necesario.

val yourString = "**This** is your **string**.".makePartialTextsBold() val anotherString = getString(R.string.something).makePartialTextsBold() /** * This function requires that the parts of the string that need * to be bolded are wrapped in ** and ** tags */ fun String.makePartialTextsBold(): SpannableStringBuilder { var copy = this return SpannableStringBuilder().apply { var setSpan = true var next: String do { setSpan = !setSpan next = if (length == 0) copy.substringBefore("**", "") else copy.substringBefore("**") val start = length append(next) if (setSpan) { setSpan(StyleSpan(Typeface.BOLD), start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } copy = copy.removePrefix(next).removePrefix("**") } while (copy.isNotEmpty()) } }


public static Spanned getBoldString(String textNotBoldFirst, String textToBold, String textNotBoldLast) { String resultant = null; resultant = textNotBoldFirst + " " + "<b>" + textToBold + "</b>" + " " + textNotBoldLast; return Html.fromHtml(resultant); }

Prueba esto. Puede ayudar definitivamente