I tried a lot and did R&D, but couldn't manage to show own custom error drawable under EditText.
Here is my code:
String str = "Please enter a valid email address”;
// This line is used to give color to error text.
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.YELLOW);
// This line is used to give background color to error text.
BackgroundColorSpan backColorSpan = new BackgroundColorSpan(Color.BLUE);
// This s basically a StringBuilder that implements CharSequence interface.
SpannableStringBuilder strBuilder = new SpannableStringBuilder(str);
strBuilder.setSpan(colorSpan, 0, str.length(), 0);
EditText et = (EditText)findViewById(R.id.et);
Drawable drawable = getResources().getDrawable(R.drawable.btn);
// This line shows our own icon in place of default icon.
setError(CharSequence, Drawable);
So, strBuilder in next line is an implementation of CharSequence.
et.setError(strBuilder, drawable);
Any Solution?
I found the solution.
You need to call setBound() method on drawable object.
You need to put this line:
drawable.setBounds(0,0,drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
You can use a TextInputLayout, it has this feature inbuilt.
Check this link out, it has exactly what you need:
http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168
For details check google devs
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
Related
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>
This may be a blunder,But I need to know... :)
Iam develeoping an android application,In in I want to display two type face in a single textview and found this One very useful,A custom typeface span that extends typeface span,
As per my understanding , we are creating a spannable as follows using two words
String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
then sets two type face to that words using our custom span like this
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Now my question, I have a large amount of text so If i am able to put the spans directly in my text its will be a more easy for us ,is that possible to put spans directly to the text like we put <b>bold </b> as my need is to use my custom typeface span like <typeface1> sample </typeface1> <typeface2> Doc </typeface2>.
WHAT I NEED IS, DIRECTLY APPLY THE CHANGE TO TEXT WHAT THE SET SPAN DO TO THE TEXT
Is that possible, if possible what i need to write as span in my text,How can I achieve this.. or am i compleatly wrong ?
Thank you for all
Very late answer but what you asked for is pretty easy.
For instance, lets say we want to change everything between quotation marks into italic font.
String s = "\"Hello my name is Edward\" Hola, my nombre es Edward";
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(text);
Spannable spannable = new SpannableString(s);
while (m.find()) {
int textLocation = text.indexOf(m.group(1));
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text of a textView with the spannable object
TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);
}
textbox.setText( spannable );
Result of s would be
"Hello my name is Edward" Hola, my nombre es Edward
Now instead of quotation marks you would use a regex pattern for tags like <b>bold </b>;
And you could also remove the tags afterwards with a simple .replace();
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 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
How do you get a red asterisk in a entry so that you can display it at the end of the text to indicate its a required field, like: Enter your name * (asterisk will be red). Or, for that matter, anywhere within the text.
You can't do that through xml string resources. This can only be done via code. For this you need to use SpannableStringBuilder and ForegroundColorSpan.
Here is small example:
TextView text = (TextView)findViewById(R.id.text);
String simple = "Enter your name ";
String colored = "*";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(simple);
int start = builder.length();
builder.append(colored);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setText(builder);
Refer to this for examples on how to style portions of a textview. Here's how you could do it for a red asterisk.
EditText editTxt = new EditText(this);
editTxt.setText("Testing asterisk *");
Spannable str = editTxt.getTxt();
int loc = editTxt.getTxt().toString().indexOf("*");
str.setSpan(new ForegroundColorSpan(Color.RED), loc, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
Alternative for showing asterisk in android Textview
txtvw2.setText(Html.fromHtml("<sup>*</sup>"+"enter you name"));
Initialize variable in strings.xml <string name="date_of_incident">Date of Incident <p><font color="red">*</font></p></string>
Write into strings.xml file
<string name="KeyWord">KeyWord <font color='red'>*</font></string>
this helped me solve this problem.