The Toast is showing the string wodString underlined and bolded like I have it in the string.xml. On the next line when I try to set the text of wod_type it presents the text without bolding or underlining. I've tried casting, Html.fromHtml(), etc. Anybody know what else I can try?
PS: wod_type is a TextView
CharSequence[] s = getResources().getTextArray(R.array.wod_style_array);
CharSequence wodString = s[position];
Toast.makeText(v.getContext(), wodString, Toast.LENGTH_SHORT).show();
wod_type.setText(wodString + m.wodScoring[position]);
wodString + m.wodScoring[position] is the problem. The + operator concatenates CharSequences to a single String. However, a String cannot hold the styling information.
You can use TextUtils.concat to avoid these problems:
text.setText(TextUtils.concat(wodString, m.wodScoring[position]));
If m.wodScoring[position] is not a String, use an appropriate method to create one (e.g. Integer.toString).
You need to use spannable string class
http://developer.android.com/reference/android/text/SpannableString.html
String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);
Related
Is there a way to set multiple colors to the edittext's hint when wrapped by android.support.design.widget.TextInputLayout without compromising the behaviour of floating edittexts?
like,
Headline*
Headline and * with different colored hint
Try this! If you want to use it for setText just add BufferType.SPANNABLE (see below)
String redPart = "Hello";
String bluePart = "World";
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString redColoredString= new SpannableString(redPart);
redColoredString.setSpan(new ForegroundColorSpan(Color.RED), 0, redPart.length(), 0);
builder.append(redColoredString);
SpannableString blueColoredString= new SpannableString(bluePart);
blueColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, bluePart.length(), 0);
builder.append(blueColoredString);
myEditText.setHint(builder)
//do following for setText
myEditText.setText(builder,BufferType.SPANNABLE)
Use SpannableString class which allows you to use different styles on parts of your string ... If I remember correctly, TextAppearanceSpan class is used for coloring a text..
See below code this is work for me.
EditText editText = (EditText) findViewById(textView);
Spannable wordtoSpan = new SpannableString("Hello world");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(wordtoSpan);
You can acheive this by programtically
SpannableString spString = new SpannableString("HEADERLINE*");
spString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 11, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
metUserName.setHint(spString);
How to change the color of NavigationDrawer title.I'm using two words in single item of title using strings.xml. How to change two different color of two words in NavigationDrawer title using android.
Example:
1.settings page
how to put this color given below
settings-#FFFFFF
page-#c00000
define following string in your strings.xml
<string name="str_settings"><Data><![CDATA[ <font color=#FFFFFF>settings</font> <font color=#c00000>page</font> ]]> </Data></string>
Then you can display this html in TextView using:
myTextView.setText(Html.fromHtml(getString(R.string.str_settings)));
For this you need to use SpannableString and ForegroundColorSpan.
This should look something like this:
SpannableStringBuilder builder = new SpannableStringBuilder();
String red = "this is red";
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);
String white = "this is white";
SpannableString whiteSpannable= new SpannableString(white);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0);
builder.append(whiteSpannable);
String blue = "this is blue";
SpannableString blueSpannable = new SpannableString(blue);
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
builder.append(blueSpannable);
mTextView.setText(builder, BufferType.SPANNABLE);
I have the following requirement:
i> User enters a string in an EditText.
e.g: aaaaaaa bbbbbbbbb ccccccccccccc
ii> User now selects the substring "bbbbbbbbb" and adds a bold style to the substring.
Selection of the substring is done with the following code:
EditText content = (EditText) layout
.findViewById(R.id.txt_content);
int startSelection = content.getSelectionStart();
int endSelection = content.getSelectionEnd();
Now , applying the bold style to the substring , the following code has been added to reflect the style on the EditText:
final SpannableStringBuilder str1 = new SpannableStringBuilder(content.getText().toString());
str1.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
content.setText(str1);
iii> Now the user modifies the original string.
Lets assume the new string to be
e.g: aaaaaaa bbbbbbbbb ccccccccccccc ddddddddd
Now user wants to apply underline style on the substring "ddddddddd"
Now , before underline , the current text is fetched from the EditText using the following code:
content.getText()
The issue is the text fetched using content.getText() doesnot contain the BOLD style which has been applied on the substring "bbbbbbbbb".
Basically the requirement is to apply different styles(bold/italic) on individual characters/words in a sentence by selection. Modification(addition / removal of characters/words) of the sentence is also possible.
How to resolve the issue ?
EditText content = (EditText) layout.findViewById(R.id.txt_content);
int startSelection = content.getSelectionStart();
int endSelection = content.getSelectionEnd();
// important. dont use getText()
Spannable sb = new SpannableString( content.toString() );
/** for bold for example. substract the endselection from startselection to get the length **/
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection - startSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
/** do more styles ...**/
sb.setSpan(new UnderlineSpan(), 20, 30, 0);
content.setText(sb);
to show this in TextView
textview.setText(sb);
You can also grab all Assigned Styles with:
StyleSpan[] mSpans = content.getText().getSpans(0, content.length(), StyleSpan.class);
and read the different styles by checking the class.
for (StyleSpan mSpan : mSpans) {
if (mSpan instanceof StyleSpan) {
int start = content.getSpanStart(mSpan);
int end = content.getSpanEnd(mSpan);
int flag = content.getSpanFlags(mSpan);
Log.i("SpannableString Spans", "Found StyleSpan at:\n" +
"Start: " + start +
"\n End: " + end +
"\n Flag(s): " + flag);
}
}
You know if you are not appending text you don't have to use SpannableStringBuilder you can use a Spannable text. Anyways I think your problem is your converting your text to String.
Spannable spannable = new SpannableString(mEditText.getText());
spannable.setSpan(new StyleSpan(Typeface.BOLD),
mEditText.getSelectionStart(),
mEditText.getSelectionEnd(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mEditText.setText(spannable);
But if your code is going to overlap styles you will have to handle it in a different way.
I have a problem with a SpannableString object.
Below's a short example:
SpannableString spanstr = new SpannableString("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), 0);
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(spanstr);
sb.append("\n"); // A newline
sb.append("The first line is bold. This one isn't.");
CharSequence cq = sb.subSequence(0, sb.length());
// ^There's no such method to return a SpannableString,
// so I try to return a CharSequence instead.
// And then, at last:
TextView contentView = (TextView) findViewById(R.id.some_id);
contentView.setText(cq);
What the example's trying to do is showing this:
Bold please!
The first line is bold. This one isn't.
But the problem is: the first line of the text won't show up in bold.
Why doesn't it do it expected?
Use the spannable string builder for setting as text in textview :
contentView.setText(sb);
or else you can do like this :
SpannableStringBuilder spanstr = new SpannableStringBuilder("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanstr.append("\n");
spanstr.append("The first line is bold. This one isn't.");
contentView.setText(spanstr);
use SpannableStringBuilder instance itself.
contentView.setText(sb);
output with your code:
Try the below. You need to set the spannable string to the textview. So set the spannable string to your text as below
String s= "The first line is bold. This one isn't";
String title="Bold Please!";
TextView tv = (TextView) findViewById(R.id.some_id);
tv.setText("");
SpannableString ss1= new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(s);
I tried the above and you can see the resulting snapshot below.
If you use custom font in your device. There is such a silly bug i think. Please change your custom font to default in your device and try again.
late for the answer but for future reader who facing this problem,
make sure you don't call toString() when setting builder into textview
val builder = SpannableStringBuilder()
builder.setspan(xxxxxxx)
do this :
textview.text = builder
and dont do this :
textview.text = builder.toString() //will remove the styling
alas, the styling from SpanableStringBuilder wont work
Just a quick question:
Do you know why i can do that:
pnrCode = (TextView)findViewById(R.id.pnr);
SpannableString pnrContent = new SpannableString("PNR: ");
pnrContent.setSpan(new UnderlineSpan(), 0, pnrContent.length(), 0);
pnrCode.setText(pnrContent + BookingSettings.getBookingSegment().substring(0, 6));
The text is not underlined.
Whereas, if i do only that:
pnrCode = (TextView)findViewById(R.id.pnr);
SpannableString pnrContent = new SpannableString("PNR: ");
pnrContent.setSpan(new UnderlineSpan(), 0, pnrContent.length(), 0);
pnrCode.setText(pnrContent);
The text is underlined.
I have to create two textview ?
Do you know another solution ?
Any help is appreciated ;)
I have to create two textview ?
No.
pnrContent + BookingSettings.getBookingSegment().substring(0, 6) is probably going to give you a String back, as I believe the + operator will convert both sides to String objects, then perform the concatenation. Hence, you will lose your formatting.
Instead, use new SpannableString("PNR: "+ BookingSettings.getBookingSegment().substring(0, 6));, and set the length of the UnderlineSpan as needed.