Est-il possible de définir la couleur d’une plage de texte dans un TextView?
Je voudrais faire quelque chose de similaire à l’application Twitter, dans laquelle une partie du texte est en bleu. Voir l’image ci-dessous:
Une autre réponse serait très similaire, mais n’aurait pas besoin de définir le texte de TextView
deux fois
TextView TV = (TextView)findViewById(R.id.mytextview01); Spannable wordtoSpan = new SpannableSsortingng("I know just how to whisper, And I know just how to cry,I know just where to find the answers"); wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TV.setText(wordtoSpan);
Voici une petite fonction d’aide. Idéal lorsque vous avez plusieurs langues!
private void setColor(TextView view, Ssortingng fulltext, Ssortingng subtext, int color) { view.setText(fulltext, TextView.BufferType.SPANNABLE); Spannable str = (Spannable) view.getText(); int i = fulltext.indexOf(subtext); str.setSpan(new ForegroundColorSpan(color), i, i + subtext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
Si vous souhaitez plus de contrôle, vous pouvez vérifier la classe TextPaint
. Voici comment l’utiliser:
final ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(final View textView) { //Your onClick code here } @Override public void updateDrawState(final TextPaint textPaint) { textPaint.setColor(yourContext.getResources().getColor(R.color.orange)); textPaint.setUnderlineText(true); } };
Je trouve toujours des exemples visuels utiles pour essayer de comprendre un nouveau concept.
SpannableSsortingng spannableSsortingng = new SpannableSsortingng("Hello World!"); BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW); spannableSsortingng.setSpan(backgroundSpan, 0, spannableSsortingng.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableSsortingng);
SpannableSsortingng spannableSsortingng = new SpannableSsortingng("Hello World!"); ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); spannableSsortingng.setSpan(foregroundSpan, 0, spannableSsortingng.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableSsortingng);
SpannableSsortingng spannableSsortingng = new SpannableSsortingng("Hello World!"); ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW); spannableSsortingng.setSpan(foregroundSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableSsortingng.setSpan(backgroundSpan, 3, spannableSsortingng.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableSsortingng);
Définissez le texte de votre TextView
‘spannable et définissez un ForegroundColorSpan
pour votre texte.
TextView textView = (TextView)findViewById(R.id.mytextview01); Spannable wordtoSpan = new SpannableSsortingng("I know just how to whisper, And I know just how to cry,I know just where to find the answers"); wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(wordtoSpan);
Dans certaines situations, une autre méthode consiste à définir la couleur du lien dans les propriétés de la vue prenant le Spannable.
Si votre Spannable va être utilisé dans un TextView, par exemple, vous pouvez définir la couleur du lien dans le XML comme ceci:
Vous pouvez également le définir dans le code avec:
TextView tv = (TextView) findViewById(R.id.myTextView); tv.setLinkTextColor(your_color);
Il y a une fabrique pour créer le Spannable, et évitez la dissortingbution, comme ceci:
Spannable span = Spannable.Factory.getInstance().newSpannable("text");
Définissez la couleur sur le texte en passant la chaîne et la couleur :
private Ssortingng getColoredSpanned(Ssortingng text, Ssortingng color) { Ssortingng input = "" + text + ""; return input; }
Définissez le texte sur TextView / Button / EditText etc. en appelant le code ci-dessous:
Affichage:
TextView txtView = (TextView)findViewById(R.id.txtView);
Obtenir une chaîne colorée:
Ssortingng name = getColoredSpanned("Hiren", "#800000");
Définir le texte sur TextView:
txtView.setText(Html.fromHtml(name));
Terminé
coller ce code dans votre mainActivity
TextView textview=(TextView)findViewById(R.id.textviewid); Spannable spannable=new SpannableSsortingng("Hello my name is sunil"); spannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textview.setText(spannable); //Note:- the 0,5 is the size of colour which u want to give the strring //0,5 means it give colour to starting from h and ending with space ie(hello), if you want to change size and colour u can easily
private SpannableSsortingng spannableSsortingng(SpannableSsortingng spannableSsortingng, int start, int end) { ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901}); TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); spannableSsortingng.setSpan(highlightSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableSsortingng.setSpan(new BackgroundColorSpan(0xFFFCFF48), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableSsortingng.setSpan(new RelativeSizeSpan(1.5f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableSsortingng; } SpannableSsortingng spannableSsortingng = new SpannableSsortingng("I don't like Hasina."); spannableSsortingng(spannableSsortingng, 8, 14); textView.setText(spannableSsortingng);
Sortie:
Juste pour append à la réponse acceptée, comme toutes les réponses semblent parler de android.graphics.Color
only: et si la couleur que je veux est définie dans res/values/colors.xml
?
Par exemple, considérez les couleurs de conception de matériaux définies dans colors.xml
:
#2196F3
( android_material_design_colours.xml
est votre meilleur ami)
Utilisez ensuite ContextCompat.getColor(getContext(), R.color.md_blue_500)
où vous utiliseriez Color.BLUE
, pour que:
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
devient:
wordtoSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.md_blue_500)), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Où j’ai trouvé que: