How to get a red asterisk in a <string> entry - android

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.

Related

Bold Text in TextView does not work with combination of Spanned and String

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.

How do I change color of a certain part of my textview?

I am using 2 parts in a textview, 1st part is date another is name and email.
They are both referenced in the same textview. I would like to change the color of the date to give it a different visual it from name and email. is it possible to do this without actually adding a whole new textview for name and email?
Here's my code so far:
String nameandemail;
holder.mytext.setText(String.valueOf(dateFormat.format(new Date(msg.getDate())) + " " + nameandemail + ": "));
How do I make it such that I can set the color of date with
holder.mytext.setTextColor(Color.white) and for the nameandemail string something like green?
Thanks!
You can Use spans.
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
// Set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// Set the text color for first 6 characters
sb.setSpan(fcs, 0, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
You can also use html like below
myTextView.setText(Html.fromHtml(text + "<font color=white>" + some_text + "</font><br><br>"
+ some_text));
you could define a String in your strings.xml file
<string name="test2"><font color=\'#FFFFFF\'>%1$s</font> -- <font color=\'#00FF00\'>%2$s</font></string>
and then programmatically
TextView tv = (TextView) findViewById(R.id.test);
tv.setText(Html.fromHtml(getString(R.string.test2, String.valueOf(dateFormat.format(new Date(msg.getDate())), nameandemail)));
My recommendation would be to use Spannable.
Here is a short utils method I wrapped up for you to use. You simply need to pass your TextView, your full text and the single part to be re-colored from the full text.
You can place this method to a Utils class and call it whenever you want, or keep it in a single Activity or Fragment(or wherever else) if you use it in a single class:
public static void colorText(TextView view, final String fullText, final String whiteText) {
if (fullText.length() < whiteText.length()) {
throw new IllegalArgumentException("'fullText' parameter should be longer than 'whiteText' parameter ");
}
int start = fullText.indexOf(whiteText);
if (start == -1) {
return;
}
int end = start + whiteText.length();
SpannableStringBuilder finalSpan = new SpannableStringBuilder(fullText);
// finalSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(view.getContext(),R.color.your_own_color_code)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
finalSpan.setSpan(new ForegroundColorSpan(Color.WHITE), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
view.setText(finalSpan);
}

Change Text style format in EditText at runtime

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.

How to change some words of a string to BOLD?

This is the string which i am getting from web using json api in my code:
String content = "<strong><em>India is the world’s hub for child sex trafficking</em>
</strong></p>\n<p style=\"text-align: center;\"><strong><em>Nearly 40,000 children are
abducted every year… </em></strong></p>\n<p style=\"text-align: center;\"><strong<em>
Every8 minutes a girl child goes missing in India!</em></strong></p>\n<p><strong>Priti
Pathak</strong></p>\n<p>MEET Shivani Shivaji Roy, Senior Inspector, Crime Branch, Mumbai
Police, as she sets out to confront the mastermind behind the child trafficking mafia,"
In this string whatever the text is in
"<strong><em> TEXT </em></strong>"
I want to display it BOLD. So for the above string
<strong><em>India is the world’s hub for child sex trafficking</em></strong>
will be displayed as
India is the world’s hub for child sex trafficking
. This should happen for entire string. This is the code which i am using:
int startIndex = content.indexOf("<strong><em>")+13; // 13 because <strong><em> has 13 characters
String substring = content.substring(startIndex, startIndex+200);
int subendIndex = substring.indexOf("</em></strong>");
int endIndex = startIndex + subendIndex;
SpannableString s = new SpannableString(content);
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
textview.setText(s, BufferType.SPANNABLE );
This code is working but it is setting bold text only to the text whichever is first within strong tag and not to the rest. Because I am setting bold text only to the first tag.
How should i get the startIndex, endIndex of all the "strong tags" and so i can set
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
to all the texts and also the number of times i would have to set the span.
I though of using regex also but i dont know how to get indexes using it. Any help would be nice. Thankyou !
The easier way is to use the Html class:
s = Html.fromHtml(content);
See the documentation for the details.
This is the simple way
TextView TV = (TextView)findViewById(R.id.Tv);
String Title="I N <big>D</big> I A";
TV.setText(Html.fromHtml(Title));
If you want to only bold some strings then I recommend Html.fromHtml. A short sample:
String str = "<b> bold text</b> unbold one";
textView.setText(Html.fromHtml(str));
Here's a list of html tags supported by textview.
Use regular HTML tags in Strings, This text uses bold and italics by using inline tags such as within the string file.. Refer this link Hope this Link would be helpful for you!

How to make part of the text Bold in android at runtime?

A ListView in my application has many string elements like name, experience, date of joining, etc. I just want to make name bold. All the string elements will be in a single TextView.
my XML:
<ImageView
android:id="#+id/logo"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/logo"
android:padding="5dp"
android:textSize="12dp" >
</TextView>
My code to set the TextView of the ListView item:
holder.text.setText(name + "\n" + expirience + " " + dateOfJoininf);
Let's say you have a TextView called etx. You would then use the following code:
final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC); //Span to make text italic
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
etx.setText(sb);
Based on Imran Rana's answer, here is a generic, reusable method if you need to apply StyleSpans to several TextViews, with support for multiple languages (where indices are variable):
void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start = text.indexOf(spanText);
int end = start + spanText.length();
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
Use it in an Activity like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
setTextWithSpan((TextView) findViewById(R.id.welcome_text),
getString(R.string.welcome_text),
getString(R.string.welcome_text_bold),
boldStyle);
// ...
}
strings.xml
<string name="welcome_text">Welcome to CompanyName</string>
<string name="welcome_text_bold">CompanyName</string>
Result:
Welcome to CompanyName
You can do it using Kotlin and buildSpannedString extension function from core-ktx
holder.textView.text = buildSpannedString {
bold { append("$name\n") }
append("$experience $dateOfJoining")
}
The answers provided here are correct, but can't be called in a loop because the StyleSpan object is a single contiguous span (not a style that can be applied to multiple spans). Calling setSpan multiple times with the same bold StyleSpan would create one bold span and just move it around in the parent span.
In my case (displaying search results), I needed to make all instances of all the search keywords appear bold. This is what I did:
private static SpannableStringBuilder emboldenKeywords(final String text,
final String[] searchKeywords) {
// searching in the lower case text to make sure we catch all cases
final String loweredMasterText = text.toLowerCase(Locale.ENGLISH);
final SpannableStringBuilder span = new SpannableStringBuilder(text);
// for each keyword
for (final String keyword : searchKeywords) {
// lower the keyword to catch both lower and upper case chars
final String loweredKeyword = keyword.toLowerCase(Locale.ENGLISH);
// start at the beginning of the master text
int offset = 0;
int start;
final int len = keyword.length(); // let's calculate this outside the 'while'
while ((start = loweredMasterText.indexOf(loweredKeyword, offset)) >= 0) {
// make it bold
span.setSpan(new StyleSpan(Typeface.BOLD), start, start+len, SPAN_INCLUSIVE_INCLUSIVE);
// move your offset pointer
offset = start + len;
}
}
// put it in your TextView and smoke it!
return span;
}
Keep in mind that the code above isn't smart enough to skip double-bolding if one keyword is a substring of the other. For example, if you search for "Fish fi" inside "Fishes in the fisty Sea" it will make the "fish" bold once and then the "fi" portion. The good thing is that while inefficient and a bit undesirable, it won't have a visual drawback as your displayed result will still look like
Fishes in the fisty Sea
if you don't know exactly the length of the text before the text portion that you want to make Bold, or even you don't know the length of the text to be Bold, you can easily use HTML tags like the following:
yourTextView.setText(Html.fromHtml("text before " + "<font><b>" + "text to be Bold" + "</b></font>" + " text after"));
<string name="My_Name">Given name is <b>Not Right</b>Please try again </string>
use "b" tag in string.xml file.
also for Italic "i" and Underline "u".
Extending frieder's answer to support case and diacritics insensitivity.
public static String stripDiacritics(String s) {
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}
public static void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style, boolean caseDiacriticsInsensitive) {
SpannableStringBuilder sb = new SpannableStringBuilder(text);
int start;
if (caseDiacriticsInsensitive) {
start = stripDiacritics(text).toLowerCase(Locale.US).indexOf(stripDiacritics(spanText).toLowerCase(Locale.US));
} else {
start = text.indexOf(spanText);
}
int end = start + spanText.length();
if (start > -1)
sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}
If you are using the # srings / your_string annotation, access the strings.xml file and use the <b></b> tag in the part of the text you want.
Example:
<string><b>Bold Text</b><i>italic</i>Normal Text</string>
I recommend to use strings.xml file with CDATA
<string name="mystring"><![CDATA[ <b>Hello</b> <i>World</i> ]]></string>
Then in the java file :
TextView myTextView = (TextView) this.findViewById(R.id.myTextView);
myTextView.setText(Html.fromHtml( getResources().getString(R.string.mystring) ));
To better support translations and remove any dependency on length of the string or particular index, you should use android.text.Annotation in you string defined strings.xml.
In your particular case, you can create a string like below
<string name="bold_name_experience_text"><annotation type="bold">name</annotation> \nexpirience dateOfJoininf</string>
or if you want to substitute these in runtime, you can create a string as follow
<string name="bold_name_experience_text"><annotation type="bold">name</annotation> \n%d %s</string>
You must apply this bold_name_experience_text in your text view label. These annotation class spans get added to your string and then you can iterate on them to apply the bold span.
You can refer to my SO answer which shows the Kotlin code to iterate through these spans and apply the bold span
Remember all the above answers has one of the following flows:
They are using some hard-coded index logic which may crash or give wrong results in some other language
They are using hardcode string in Java code which will result in lots of complicated logic to maintain internalisation
Some used Html.fromHtml which can be acceptable answer depending on the use-case. As Html.fromHtml doesn't always work for all types of HTML attributes for example there is not support of click span. Also depending on OEM you might get different rendered TextView

Categories

Resources