Is it possible to have a TextView with different background colors. Eg. If the text is "This is a test", is it possible to set different background color for the four words?
Yes.
SpannableString spannableString = new SpannableString(getString(R.string.hello_world));
Object greenSpan = new BackgroundColorSpan(Color.GREEN);
Object redSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(greenSpan, 0, 6, 0);
spannableString.setSpan(redSpan, 6, spannableString.length(), 0);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(spannableString);
Produces:
EDIT: There are a lot of different spannable types, you can do much nicer looking things than my basic example. Check out this article.
Related
I have used one textview and value set for it is like ex:11,234.45 and after decimal values like 45 should be shown with reduced front like 12sp and different colour like grey and 11,234 with black colour with front 16sp. how to achieve this using single textview in android?
You can achieve that if you follow this steps
First you need to split you string into 2 strings with this code lines
String s ="11,234.45";
String[] split = s.split(".");
String firstSubString = split[0];
String secondSubString = split[1];
Then you change the font size and color of secondSubString like this
SpannableString ss= new SpannableString(secondSubString);
ss.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
You can change firstSubString size and color like this
SpannableString ss1= new SpannableString(firstSubString);
ss1.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, 0);// set color
Finally you set your string to the TextView
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1 + "." + ss);
output i'm getting.I'm creating app in which I'm setting background color of textview using BackgroundColorSpan. But it's output different than i want.
Code
Spannable spannable = new SpannableString(inputTextView.getText());
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(a);
spannable.setSpan(backgroundColorSpan, 0, inputTextView.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inputTextView.setText(spannable);
And
String s ="<span style='background-color: #FFFFFF; line-height: 2.0;'>"+inputTextView.getText()+"</span>";
inputTextView.setText(Html.fromHtml(s));
Screenshot of website text i want to make in android
BackgroundColorSpan() uses hex value. You need to convert your hash string to hex and use it.
Spannable spannable = new SpannableString(inputTextView.getText().toString());
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#ff0000"));
spannable.setSpan(backgroundColorSpan, 0, inputTextView.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inputTextView.setText(spannable);
I am displaying text in android like this:
TextView textview p new TextView(this);
textview.setMovementMethod(new ScrollingMethod());
textview.setText("Today: 2\nTomorrow: 8\nNext two weeks: 45");
textview.setTextSize(16);
textview.setTypeFace(null, TypeFace.BOLD);
setContentView(textview);
I want to make the 2, 8 and 45 from the text green while leaving the rest black. I know how to do it for all the text but not individual characters. Can someone help? I have looked through other similar questions but none seem to be setting the textview the way I am here.
Use SpannableString and add ForegroundColorSpans to the text. You'll have to find the indexes of the charactes/substrings that you want to be spanned so you can call addSpan
SpannableString spannedText = new SpannableString(originalText);
int start = ..., end = ...;
spannedText.addSpan(new ForegroundColorSpan(Color.GREEN), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
You can try this :
TextView textview p new TextView(this);
textview.setMovementMethod(new ScrollingMethod());
textview.setText(Html.fromHtml("Today: <font color=green>2</font>\nTomorrow: <font color=green>8</font>\nNext two weeks: <font color=green>45</font>");
textview.setTextSize(16);
textview.setTypeFace(null, TypeFace.BOLD);
setContentView(textview);
This will set green colors to 2,8,45. simillarly you can use any html tags for textview text.
Hope this will help you.
in my application i have to show some informations.i would structured these informations like this
Battery level //red textcolor
80% // black textcolor
Is it possible? Because for now i have created two separated textview; one for the "title" and another for the information with different colors. Thanks
You can use Spannable to achieve what you want:
TextView textView = (TextView) findViewById(R.id.textView);
String text = "<font color='red'>Battery level</font> <font color='black'>80%</font>"
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
You can use SpannableString to overcome your issue.
Here is what you can do with your textviews
TextView tc_text = (TextView) findViewById(R.id.signup_TC_text);
SpannableString text = new SpannableString("Points are awarded when you confirm your email. By signing up, you agree to our Terms & Conditions.");
text.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 80, 0);
final Context context = this;
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
}
};
text.setSpan(clickableSpan, 80, 98, 0);
// make our ClickableSpans and URLSpans work
tc_text.setMovementMethod(LinkMovementMethod.getInstance());
// shove our styled text into the TextView
tc_text.setText(text, BufferType.SPANNABLE);
Here what I have done is, I have counted number of characters from where to where I want some particular color.
Hope this will help you.
I would like to use Canvas.drawText() to display multi-color text. More specifically, I want to highlight a substring of the text passed to the drawText() method.
The text is in the form of a SpannableString with 0 or more ForegroundColorSpan objects.
Looking at the Canvas code, it appears that a .toString() call on the passed CharSequence, means that this is not possible.
Is there an alternative way?
EDIT: The text may occasionally change (total changes, not incremental). Also, there are potentially multiple texts positioned in different unrelated locations in the custom view.
Yes it is possible by using one of the Layout classes. These are helper classes for drawing text to a canvas and they support Spannables. If your text doesn't change use a StaticLayout.
Example
Add this to your custom view class
private StaticLayout layout;
put this code into your onLayout or onSizeChanged
Spannable wordtoSpan = new SpannableString("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);
TextPaint paint = new TextPaint();
paint.setTextSize(20f);
paint.setColor(Color.RED);
layout = new StaticLayout(wordtoSpan, paint, getWidth(), Alignment.ALIGN_NORMAL, 1, 0, false);
Then in your drawing method simply call
layout.draw(canvas);
In case your text changes often you can use a DynamicLayout.
Editable.Factory fac = Editable.Factory.getInstance();
Editable edit = fac.newEditable(wordtoSpan);
DynamicLayout layout = new DynamicLayout(edit,paint,getWidth(),Alignment.ALIGN_CENTER,1,0,false);
change text by using the edit object
edit.append("hello");
Try something like this, if you use TextView
String multiColorText = "<font color=0xff0000>Multi</font><font color=0x000000>Color</font><font color=0xccffff>Text</font>";
textView.setText(Html.fromHtml(multiColorText));
Edit :
For SpannableString, check if the below helps you
Spannable WordtoSpan = new SpannableString("partial colored text");
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Whenever you write that text for that view you can set thatView.setBackgroundResource(R.drawable.multicolor); and
In multicolor.xml write
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/tabBgStart"
android:endColor="#color/tabBgEnd"
android:angle="270"/>
</shape>
Hope it will works definitely
To Change the text color you can use yourView.setTextColor(R.drawable.multicolor);
i hvn't used in with Canvas. see below code how i used it in textview.
public TextView getTextClipArt1(){
TextView textView = new TextView(context);
Typeface tf = new MyTypeface(context, 0).getTypeface();
Shader textShader=new LinearGradient(0, 0, 0, 30,
new int[]{Color.GREEN,Color.BLUE},
new float[]{0, 1}, TileMode.CLAMP);
textView.setTypeface(tf);
textView.getPaint().setShader(textShader);
textView.getPaint().setStyle(Paint.Style.STROKE);
textView.getPaint().setStrokeWidth(2);
textView.setText("ABC");
textView.setTextSize(30);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return textView;
}
you can now draw textview as bitmap on canvas, Although i think these methods are also exist in paint class.
Hope useful to you.