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);
Related
I have searched internet and tried the following code but its not working
SpannableString ss1 = new SpannableString("Health: ");
ss1.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, ss1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview1.setText("\n"+ss1+strhealth+"\n\n");
i want output to be like this
Health: good
where strhealth = good
But it is coming out Health: good
What is the mistake ?
I am using android studio 2.1.1
String txt1="Health: ";
SpannableString txtSpannable= new SpannableString(txt1);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
txtSpannable.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(txtSpannable);
String txt2="good";
builder.append(txt2);
textview1.lblStatus.setText(builder, TextView.BufferType.SPANNABLE);
The easiest way is
textview1.setText(Html.fromHtml("<b>Health:</b> good"));
The mistake in your code is to use string concatenation here: "\n"+ss1+strhealth+"\n\n" which strips out all formatting because the components are taken as normal strings.
In kotlin, you can do this. I used this to bold characters/word within a string. For example:
item = "Philippines"
query = "Phil"
result = Philippines
val spannable = SpannableString(item)
val indexStart = item.indexOf(query)
val indexEnd = indexStart + query.length
spannable.setSpan(StyleSpan(Typeface.BOLD), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
and use it like this
textView.text = spannable
I am bit late to answer, but I created a method for easy use by using answer already provided here.
private void setSpanString(String string1, String string2, TextView textView) {
SpannableStringBuilder builder=new SpannableStringBuilder();
SpannableString txtSpannable= new SpannableString(string1);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
txtSpannable.setSpan(boldSpan, 0, string1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(txtSpannable);
builder.append(string2);
textView.setText(builder, TextView.BufferType.SPANNABLE);
}
I would use a string resource such as this:
<string name="health_status"><b>Health:</b> %1$s</string>
When you want to set the health status just use this code:
String ss1 = getString(R.string.health_status, strhealth);
Below I have mentioned the code which one through you can create spannableString in Kotlin
val spannableStringBuilder = SpannableStringBuilder()
val boldSpan: StyleSpan = StyleSpan(Typeface.BOLD)
sp_text.setSpan(boldSpan, firstIndex, lastIndex,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
sp_text.setSpan(clickableSpan, firstIndex, lastIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
sp_text.setSpan(ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.colorPrimary)), firstIndex, lastIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
I think you should use 2 different textView, a label and one for the data. it's common and good practice
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>
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);
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
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