I'm using a TextSwitcher, however the setText() method takes in a CharSequence rather than an resID integer. If I use getString(resID) the formatting is stripped out. Is there a way to get formatted text (with bold and italics) to show in a TextSwitcher?
Learn about SpannableStringBuilder, this is so useful in producing styled text.
Then just create your formatted strings like:
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append("Hi, abc!");
sb.setSpan(new ForegroundSpan(0xffff0000), 4, 7, 0); // set characters 4 to 7 to red
yourTextWidget.setText(sb, BufferType.SPANNABLE);
Edit: TextSwitcher is just a small wrapper to ViewSwitcher. Examining the sources of TextSwitcher reveals:
/**
* Sets the text of the next view and switches to the next view. This can
* be used to animate the old text out and animate the next text in.
*
* #param text the new text to display
*/
public void setText(CharSequence text) {
final TextView t = (TextView) getNextView();
t.setText(text);
showNext();
}
So, just call this instead of setText:
final TextView t = (TextView) yourWidget.getNextView();
t.setText(sb, BufferType.SPANNABLE);
yourWidget.showNext();
In order to set a formatted text to a TextView, you need to use Html.fromHtml(String). Keep in mind, Spannedimplements from CharSequence.
You can also use Resources.getText(int) to get a styled CharSequence from your application resources.
Related
I need to add a country code to phone numbers to dial it from Android, but I would like to display the phone numbers without the country code. I can get the dialler working using setText() for the phone number (with country code) in Java and adding Autolink in the XML.
However I can't seem to find a way of displaying the phone number without the country code i.e. when using <a href= you can have different text to the actual hyperlink.
So I want to display 01234 567890, but dial +441234567890.
I've tried using this HTML in a string but it doesn't work:
01234 567890
You can remove first three characters of your string line:
numberString.substring(3);
and it will return you:
01234 567890
Hope it will help you :)
Use a ClickableSpan:
final SpannableString span = new SpannableString("01234 567890");
final ClickableSpan onClick = new ClickableSpan() {
#Override
public void onClick(View textView) {
//Launch your Intent to dial the number, with whatever number you like
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
span.setSpan(onClick, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
final TextView tv = (TextView) findViewById(R.id.text_view);
tv.setText(span);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.GREEN);
You may need to add clickable:true in your XML as well.
Or, if your TextView just contains the number, you can set a click listener on the entire TextView and launch your dial from there
I am making a Mad Libs game of sorts for practice, and so I will have things like EditTexts so the user can input data.
However right now the only way I can get this to sort of work is if I use a horizontal LinearLayout and then do something like a TextView followed by an EditText followed by another TextView, but then if the user enters something long in the EditText, the text does not "wrap nicely" but rather it squishes the rightmost TextView. This is to be expected, but it is not my end-desired behavior.
Is there a better way to put EditTexts inside of TextViews?
Just have one TextView and then you can use a ClickableSpanand even add multiple ClickableSpan(s) to a given TextView. This gives you the effect of multi-line TextView with certain spots acting like an EditText.
If an object of this type is attached to the text of a TextView with a
movement method of LinkMovementMethod, the affected spans of text can
be selected. If clicked, the onClick(View) method will be called.
SpannableString madLibString = new SpannableString("Our school cafeteria has really adjective food");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Toast.makeText(getApplicationContext(), "The text 'adjective' has been clicked. Do something.",
Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
// NOTE: set styling here to give UI feedback that this is text that can be clicked and edited.
ds.setColor(getResources().getColor(android.R.color.holo_red_dark));
}
};
madLibString.setSpan(clickableSpan, 32, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(madLibString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
I want to change this string to the color red
String isFalse = False;
For some reason every tutorial seemed more complicated than I expected and I don't understand them. Is there a simple way to do this? Also, would this override the color of a textview? Because I would like it to.
String is not View, so it has no color at all.
Maybe what you want is to change the appearance color of it host TextView. To achieve this you can use:
TextView text;
//the initialize of this TextView
text.setText(isFalse);
text.setTextColor(Color.Red);
The parameter of the color could be resource from your color XML values file or android.R.color resource file, or from Color class, etc.
Please use this code.
SpannableStringBuilder builder = new SpannableStringBuilder();
String isFalse = "False";
SpannableString redSpannable= new SpannableString(isFalse);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, Tru_score.length(), 0);
builder.append(redSpannable);
TextView text1 = (TextView)findViewById(R.id.textView1);
text1.setText(builder, BufferType.SPANNABLE);
It is not complicated or hard
Addressing both possibilities..
You want to change the color for your text at runtime.
that would be like TextView.setTextColor()
i.e.
falseTextView.setTextColor(getResources().getColor(R.color.red))
if you wanted a segment of the same textview to have a different color,by which I mean that the isFalse string is only apart of the content of your TextView ,you need to use as mentioned in the other answer.
Try this...
string.xml
<string name="isFalse"><![CDATA[<b><font color=#FF0000>False</b>]]></string>
MainActivity.java
TextView textView1 = (TextView)findViewById(
R.id.textView1);
textView1.setText(Html.fromHtml(isFalse));
And result, you might get like this...
How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}
I would like to make a TextView to be entirely underlined but I can't use a text resource and <u> tag because it is dynamic text.
Related: Can I underline text in an android layout?
So far the only way I know to do this is at runtime. Is this really the only way? Is there a way I could do it in the XML files?
The easiest solution is probably to create a custom UnderLineTextView component deriving from TextView, override setText() and set the entire text as underlined, something like this (underline code from the link you referred to above):
#Override
public void setText(CharSequence text, BufferType type) {
// code to check text for null omitted
SpannableString content = new SpannableString(text);
content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
super.setText(content, BufferType.SPANNABLE);
}
It's then just a matter of using your new component in the layout and setting the text as usual. The rest is handled automatically.
More info on custom components:
http://developer.android.com/guide/topics/ui/custom-components.html
You can underline text over Html.fromHtml(String source)
Example:
textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
You can also do this via the /res/values/string.xml file if you prefer: For example, in the /res/values/string.xml you could add an entry like:
<string name="createAccount"><u>Create Account</u></string>
And then in the onCreate(Bundle savedInstanceState) method of your activity you would add the following code to cause "Create Account"> to appear as underlined in the UI that you set for the createAccountText TextView that you defined in the xml file in /res/layout/ for your activity:
TextView createAccountText = (TextView) findViewById(R.id.createAccountText);
Resources res = getResources();
CharSequence styledText = res.getText(R.string.createAccount);
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE);
peter3 wrote before to extend TextView class and override setText method.
This solution won't work as setText method is marked as FINAL.
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
Not sure if the code could work with some modifications.
SpannableString content = new SpannableString(name);
content.setSpan(new UnderlineSpan(), 0, name.length(), 0);
geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);