I want to code finish string format, I have a
String s="19831014"+"linknum"+i+"cool";
I want to this s have a format link this:
s=<red>19831014</red>+"//n" //line break
+<green>"linknum"</green> +i+"cool"
Can you help?
String s="19831014\n"+"linknum"+i+"cool";
"\n" is a line break.
String doesnot have Color but the thing that displays a String has.
But you can convert your String object to SpannableString which allows user to add effects like Bold, italics, Underline, Colored Text Portions etc.
if you want to display String with different colors you have to use ForegroundColorSpan
For example :
SpannableString colouredString =new SpannableString("Red, Green, Blue.");
colouredString.setSpan(new ForegroundColorSpan(0xFFFF0000), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
colouredString.setSpan(new ForegroundColorSpan(0xFF00FF00), 5, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
colouredString.setSpan(new ForegroundColorSpan(0xFF0000FF), 12, colouredString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
For changing the Background Color you have to use BackgroundColorSpan :
For Example :
coloredString.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
now you can use this colouredString for showing it in EditText and TextView
For giving different styles you can use StyleSpan
Reference Example: http://developer.android.com/resources/faq/commontasks.html#selectingtext
Related
I have this code
TextView text1 = (TextView) view.findViewById(R.layout.myLayout);
Spanned myBold = (Html.fromHtml("<b>Test<b>", Html.FROM_HTML_MODE_LEGACY));
If I do
text1.setText(myBold);
Then myBold is in bold,which is ok. But when I want to add a string more, like
text1.setText(myBold+"bla");
Then the whole TextView is not bold anymore. Why does the new String "bla" affect this?
Thanks.
Why does the new String "bla" affect this?
Because what you are really doing is:
text1.setText(myBold.toString() + "bla");
A String has no style information. A Spanned object does.
Use TextUtils.concat() instead:
text1.setText(TextUtils.concat(myBold, "bla"));
A better choice would be to use a Bold StyleSpan. In the next sample only the "hello" world will be set to bold by using such technique:
Java:
final SpannableString caption = new SpannableString("hello world");
// Set to bold from index 0 to the length of 'hello'
caption.setSpan(new StyleSpan(Typeface.BOLD), 0, "hello".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(caption);
Kotlin:
yourTextView.text = SpannableString("hello world").apply {
// Set to bold from index 0 to the length of 'hello'
setSpan(StyleSpan(Typeface.BOLD), 0, "hello".length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE))
}
This would be a more optimal solution rather than using the Html.fromHtml technicque, as it doesn't have to go through the overhead of parsing/interpreting the HTML tags.
In addition, it allows you to combine more styles, sizes, etc, in the same SpannableString.
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);
Ok, so I gather that this should be a fairly straightforward process.
I've read the following questions:
Multiple Typefaces in the same TextView
Multiple Typefaces in the same TextView
Put in bold some parts of a TextView
Making part of a string bold in TextView
The advice seems to be pretty similar across all of these questions and answers. I'm trying to avoid the HTML techniques and use SpannableString and SpannableStringBuilder instead. Ultimately, I'd like to be able to use multiple different typefaces in a single TextView, but for now, I'd just like to figure out how to get multiple colors working.
I'm trying to implement those techniques in this way:
// Get a typeface for my custom font
String regularFontPath = "fonts/Abel-Regular.ttf";
Typeface regularTf = Typeface.createFromAsset(getActivity().getAssets(), regularFontPath);
// Set the label's typeface (this part is working)
mItemCodesLabel.setTypeface(regularTf);
// Create a spannable builder to build up my
// TextView's content from data
SpannableStringBuilder builder = new SpannableStringBuilder();
// These colors are defined and working well in other parts of my app
ForegroundColorSpan ltGraySpan = new ForegroundColorSpan(R.color.light_gray);
ForegroundColorSpan dkGraySpan = new ForegroundColorSpan(R.color.dark_gray);
// mCodesList has good data and the actual data output from this
// loop is correct. Just the styling is wrong
for (int i = 0; i < mCodesList.size(); i = i + 1) {
ParseObject code = mCodesList.get(i);
String value = code.getString("value") + " | ";
if (i > 0) {
// I want new codes to be on a new line (this works)
value = "\n" + value;
}
SpannableString valueSpan = new SpannableString(value);
valueSpan.setSpan(ltGraySpan, 0, value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(valueSpan);
String loc = code.getString("location");
SpannableString locSpan = new SpannableString(loc);
locSpan.setSpan(dkGraySpan, 0, loc.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(locSpan);
}
mItemCodesLabel.setText(builder);
The net result is that the TextView contains the correct text contents. The TextView is the correct typeface. But the entire contents of the TextView are my #color/light_gray color. I'm not sure why, because in the XML layout file, I had specified my #color/dark_gray color (which I expect to be overridden by setting the text with a Spannable). Even if I change both ForegroundColorSpan objects to use R.color.dark_gray, the TextView still comes out light gray. I don't see anywhere else in my code where I'm setting the color of the text, so I'm really at a loss.
I'm running this on an LG Optimus G Pro, which is running 4.4.2. I have another TextView where I need to get multiple colors and font working and even underline some parts of the text, so this is a pretty big deal for me. Where am I going wrong?
use getResource().getColor(R.color.light_gray) to retrieve the color you are passing to the ForegroundColorSpan. I doubt it is retrieving it internally for you. You probably need to instantiate a new ForegroundColorSpan at every iteration. It is not possible to reuse it
You may Use SpannableStringBuilder because it implements from spannable and CharSequence, also you may do anything with following
TextView txtTest = (TextView) findViewById(R.id.txt);
String text = "This is an example";
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(new TypefaceSpan("monospace"), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new TypefaceSpan("serif"), 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.white)), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.grey)), 6, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtTest.setText(str);
I have add colors.xml in values
<color name="black">#000000</color>
<color name="grey">#DCDCDC</color>
<color name="white">#FFFFFF</color>
I am trying to create a function that will take a string "word" as an argument, apply span styling to the string and return it as a spannable string that I can ""+insert+"" into a string in a TextView. Thank you in advance for any help you can offer with this.
Here is the function I have so far.
public Spannable fr(String word){
Spannable WordtoSpan = new SpannableString(word);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0,
word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return WordtoSpan;
}
and the code that should display the spannable inline with my text.
TextView tview = (TextView) findViewById(R.id.accents_view);
tview.setText(Html.fromHtml("<strong>Text String</strong> "+fr("style_this"));
The application will either load the text without styling or the app will crash. Not sure what I'm doing wrong but I'm guessing it has something to do with mixing fromHtml text with spannables. Any work arounds?
If I remove the Html.fromHtml() and wrap the entire text with my fr() function it works. I get bold blue text. But I need to be able to make only certain words styled... It would also be nice to be able to reference vars from my string.xml file instead of hard coding them in, for multilingual support.
I think you may find this helpful,
TextView tview = (TextView) findViewById(R.id.accents_view);
tview.setText(Html.fromHtml("<font color='blue'>Text</font> "
+ "<font color='red'>String</font>"));
I have a button in my application. the text in the button goes as "Type: Location" something like that.
I'm wondering whether its possible to change the text on the button as "Type: Location"
i.e Bold the text partially on the button??
Thanks for yoru time in advance.
we have a more better choice like this :android:textStyle="bold"
android api support bold
Simply put your string in strings.xml and change it like this,
<string name="hello"><b>Hello</b> World, fh!</string>
and set this text to your button like this
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/hello"
/>
Sometimes the above approach will not be helpful when you might have to use Dynamic Text. So at that case SpannableString comes into action.
String tempString="Copyright";
Button button=(Button)findViewById(R.id.button);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
button.setText(spanString);
You can set it using Html.fromHtml() and give as a string, a string resource with HTML elements. Hope this helps!
Using spans:
SpannableStringBuilder builder = new SpannableStringBuilder("Type: your type here!");
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
builder.setSpan(boldStyle, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
((Button) findViewById(R.id.button)).setText(builder);
You can use basic markup directory in strings, e.g.
"<b>Type</b>: Location"
See Styling with HTML markup
If want to set text programmatically then use this method
Button
First you have to set button's property in XML
android:textAllCaps="false" // very important without this property might be it won't show effect
public SpannableString setSpanableString(String textString, int start, int end){
SpannableString spanString = new SpannableString(textString);
spanString.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);
return spanString;
}
Button btn; // get your button reference here
String text = "Hi, Dharmbir";
btn.setText(setSpanableString(text, 4, text.length));// set here your index
TextView
TextView tv; // get your TextView reference here
String text = "Hi, Dharmbir";
tv.setText(setSpanableString(text, 4, text.length));
Output
Hi, Dharmbir