I have an EditText view which I am using to display information to the user. I am able to append a String and it works correctly with the following method:
public void PrintToUser(String text){
MAIN_DISPLAY.append("\n"+text);
}
The application is an RPG game which has battles, when the user is fighting I would like to display the String in RED. So I have attempted to use a SpannableString. (changed the method)
public void PrintToUser(String text, int Colour){
if(Colour == 0){
//Normal(Black) Text is appended
MAIN_DISPLAY.append("\n"+text);
}else{
//Colour text needs to be set.
//Take the CurrentText
String CurrentText = MAIN_DISPLAY.getText().toString();
CurrentText = CurrentText+"\n";
SpannableString SpannableText = new SpannableString(CurrentText + text);
switch(Colour){
case 1:
SpannableText.setSpan(new ForegroundColorSpan(Color.RED), CurrentText.length(), SpannableText.length(), 0);
break;
}
//You cannot append the Spannable text (if you do you lose the colour)
MAIN_DISPLAY.setText(SpannableText, BufferType.SPANNABLE);
}
This new method works and the new line is displayed is red.
Problems
Only the last line formatted is red. If a new line is set to be red the previous formatting is lost due to the way I retrieve the text
String CurrentText = MAIN_DISPLAY.getText().toString();
When I was ONLY appending Strings to the EditText it was displaying the bottom of the EditText box (the last input) now when using SetText the users see's the Top.
Finally My Question:
Is there a way to Append the formatted text?
OR... Can I retrieve the formatted text from the EditText and then when I use .setText I can keep the current format?
AND if so... Is there a way to focus the Display at the Bottom opposed to the Top?
Thank you! Please comment if additional information is needed I've been stuck on this for a while now.
My suggestion would be to use the method:
myTextView.setText(Hmtl.fromHtml(myString));
Where the myString could be of the format:
String myString = "Hello <font color=\'#ff0000\'>World</font>";
The following modifications to PrintToUser() should work..
public void PrintToUser(String text, int Colour){
...
...
Editable CurrentText = MAIN_DISPLAY.getText();
int oldLength = CurrentText.length();
CurrentText.append("\n" + text);
switch(Colour){
case 1:
CurrentText.setSpan(new ForegroundColorSpan(Color.RED),
oldLength, CurrentText.length(), 0);
break;
}
}
Try to change
SpannableText.setSpan(new ForegroundColorSpan(Color.RED), CurrentText.length(), SpannableText.length(), 0);
on
SpannableText.setSpan(new ForegroundColorSpan(Color.RED), 0, SpannableText.length(), 0);
Related
In my Application,
the user should be allowed to change the alignment , color ,size and other texting attributes ,but it implements alignment change to all text content, and size and color are returning to first view ,when I touch out of box
So i want to know how to solve that problem.
you have to use spandable .
for your text color use ForegroundColorSpan :
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello Everyone");
ForegroundColorSpan colorSpan = new ForegroundColorSpan(
context.getResources()
// Specify your color
.getColor(R.color.your_font_color));
realPrice.setSpan(colorSpan,
0, // Start index of the single word
4, // End index of the single word
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
yourEditText.setText(ssb);
and for your text size do like this :
String s= "Hello Everyone";
SpannableString ss1= new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1);
put your string instead of "Hello Everyone"
So I am trying to add the functionality that when you click on a phone number it would take you to the Dialer app with the pre-populated number. I have the code below:
mContactDetailsText.setText(phonetextBuilder.toString());
Pattern pattern = Pattern.compile("[0-9]+\\s+[0-9]+");
Linkify.addLinks(mContactDetailsText, pattern, "tel:");
and the Text is currently "T. 0123 4567890"
The current outcome is just having the above string without it being clickable. I have even tried added the following line, but to no luck:
mContactDetailsText.setAutoLinkMask(0);
Anyone got any ideas or can see what I am doing wrong?
Thanks
The autolink mask needs to include a search for phone numbers:
mContactDetailsText.setAutoLinkMask(Linkify.PHONE_NUMBERS);
Then you'll need to set the links to be clickable:
mContactDetailsText.setLinksClickable(true);
You might also need movement method set like so:
mContactDetailsText.setMovementMethod(LinkMovementMethod.getInstance())
You should be able to accomplish what you want with the other answers,
but this will definitely work and will give you more control over the display of the text and what will happen when you click the number.
String text = "T. ";
StringBuilder stringBuilder = new StringBuilder(text);
int phoneSpanStart = stringBuilder.length();
String phoneNumber = "0123 4567890"
stringBuilder.append(phoneNumber);
int phoneSpanEnd = stringBuilder.length();
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
startActivity(intent);
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
ds.setColor(Color.BLUE);
}
};
SpannableString spannableString = new SpannableString(stringBuilder);
spannableString.setSpan(clickableSpan, phoneSpanStart, phoneSpanEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannableString);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
You need to set an onClickListener() on your TextViews. Then they will respond to clicks.
I have a CheckBox with a string that says "I have read and understood the terms and conditions". Now I want to make the words "terms and conditions" to a link which opens a alertdialog where the terms and conditions can be read. Nothing special.
I'm thinking something in the line of:
<string name="cont_agree">I have read and understood the <a ref="open alertdialog">terms and conditions.</a></string>
Is it possible, and what should I use where it now says "open alertdialog"?
If it can't be done this way, how should I do?
Addition:
To open a url you would use this code:
<string name="cont_agree"><a ref="http://www.stackoverflow.com">Stackoverflow</a></string>
But how do you open a alertdialog, or say another screen, from a string? I have seen apps who does this so it is possible, of course, but how?
EDIT:
This is the code I use for the SpannableStringBuilder:
SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.before));
//Now create a ClickableSpan
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
d.show(); //Here dialog will be displayed
}
};
//Now append the TOS string
text.append(getString(R.string.popup));
//Declare that the "TOS" string is a clickableSpan
text.setSpan(clickableSpan, getString(R.string.before).length(),getString(R.string.before).length()+getString(R.string.popup).length(), 0); //Check on API setSpan method
((CheckBox)findViewById(yourview)).setMovementMethod(LinkMovementMethod.getInstance());
((CheckBox)findViewById(yourview)).setText(text, BufferType.SPANNABLE);
I still get some markers at the first "text.append" line.
Multiple markers at this line:
Return type for the method is missing
Syntax error on token ")", { expected after this token
R.string.before cannot be resolved to a type
Syntax error on token ")", invalid VariableDeclaratorId
Syntax error on token "append", Identifier expected after this token
First setup your dialog
Dialog d = new Dialog(context);
d.setTitle... etcetc
In your values.xml create 2 string
<string name="before">I have read and understood the</string>
<string name="popup">TOS</string</string>
Now you can use SpannableStringBuilder
SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.before));
//Now create a ClickableSpan
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
d.show(); //Here dialog will be displayed
}
};
//Now append the TOS string
text.append(getString(R.string.popup));
//Declare that the "TOS" string is a clickableSpan
text.setSpan(clickableSpan, getString(R.string.before).length(), getString(R.string.before).length()+getString(R.string.popup).length(), 0); //Check on API setSpan method
((CheckBox)findViewById(yourview)).setMovementMethod(LinkMovementMethod.getInstance());
((CheckBox)findViewById(yourview)).setText(text, BufferType.SPANNABLE); //AAAAND WE'RE DONE!
I have a problem with the Font Color for the Website link in an Android App. Please see the code below:
Email.setText(Html.fromHtml("W : "+"<u>" +Email1+ "</u>"));
Can i change the Font Color for Underlined Email1 Text without changing the W : Color?
Is there any HTML Tags can be used inside "<u>" +Email1+ "</u>" to change the Font Color. Please help me with your ideas/code. Thanks in advance.
You can use like this
Email.setText(Html.fromHtml("W : "+"<u><FONT COLOR=\"#80776b\" >"+Email1+"</Font></u>"));
Use color code what you want.
Just because we can I added the 'manual' method to generate the exact same output using a SpannableStringBuilder:
String wText = "W : ";
String underlineText = "email#address.com";
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(wText);
ssb.append(underlineText);
ssb.setSpan(new UnderlineSpan(), ssb.length()-underlineText.length(), ssb.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new TextAppearanceSpan("normal", android.R.style.TextAppearance_Medium, 14,
ColorStateList.valueOf(Color.RED), ColorStateList.valueOf(Color.RED)),
ssb.length()-underlineText.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv1 = (TextView) findViewById(R.id.spannable_text1);
tv1.setText(ssb);
TextView tv2 = (TextView) findViewById(R.id.spannable_text2);
tv2.setText(Html.fromHtml(wText + "<u><font color=\"#FF0000\">" + underlineText + "</font></u>"));
By the way, in stead of only underlining the email address, you could also make it a clickable link. Just so you know. :)
use setTextColor tag to change the color of the text
eg:
Email.setTextColor(Color.RED);
Email.setText(Html.fromHtml("W : "+"<u>" +"this is test"+ "</u>"));
I am sure this should work for you, You want like this?
String styledText = "W: "+"<u>" + "<font color='red'>Email1</font> "+"</u>";
Email.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
This is the simplest solution i guess...
Email.setText(Html.fromHtml("W : "+"<u style=\"color:#80776b\">"+Email1+"</u>"));
I'm trying to use the Android Html.fromHtml method to parse a text like a tweet with # mentions in it. However, I have the username of the current user and I wish to highlight this to the user as being themselves.
I'm trying to with this code, which doesn't work:
in = in.replace("#" + u.username, "#<font background=#0099ff color=#fff><a href='dailybooth://user/"+
u.username+"'>"+u.username+"</a></font>");
In theory, I think it should work, however it doesn't color at all!
This turned out to be a doozy.
Neither the font tag nor inline styles on the a tag worked for me. I had to resort to using Spannables directly:
TextView text = (TextView) findViewById(R.id.text);
text.setText("");
String linkName = "#appamatto";
String linkUrl = "http://twitter.com/appamatto"
SpannableString str = SpannableString.valueOf(linkName);
str.setSpan(new URLSpan(linkUrl), 0, linkName.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(0xffffffff), 0, linkName.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xff0099ff), 0, linkName.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.append(str);
This will, for example, set the colors of the link in the way you wanted. You can look for other Spannables in android.text.style to achieve other effects.
Edit for replace you could do something like:
String[] components = in.split("#" + u.username, -1);
text.append(components[0]);
for (int i = 1; i < components.length; i++) {
text.append(str); // using the spannable from above
text.append(components[i]);
}
The -1 in split() is what you would need to deal with matches coming at the end of strings, as per the split docs.