TextView with different sizes - android

In my layout, I have defined a TextView and have given it the id - textName.
Programmatically, I have set its text to My name is Blah. I was wondering if it is possible to programmatically increase the textsize of just Blah and not the whole TextView?

Or simply using the Spannable class:
String text = textView.getText();
Spannable span = new SpannableString(text);
span.setSpan(new RelativeSizeSpan(1.5f), text.indexOf("Blah"), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);

Yes!! you can do by formatting the string with HTML, refer https://stackoverflow.com/a/1533512/603233
like eg
String text = "<h5>My name is </h5><h1>Blah</h1>";
yourtextview.setText(Html.fromHtml(text));

Related

In TextInputLayout change the color of hint for single letter or alphabet

I have defined TextInputLayout programmatically in my Activity. I have added EditText inside that TextInputLayout, that to programmatically.
Now, I have to set the color of hint of that edittext. Here I just have to change the color of single alphabet of the hint.
Example - Hint is like, Firstname *. Here i want to change the color of "*" to Red and remaining hint as black in color.
I have already tried Html and Spannable String, but both are not working for TextInputLayout.
For Spannable i did
SpannableString redSpannable = new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
Spanned hint = Html.fromHtml(string + redSpannable);
etDefault[j].setHint(hint);
For HTML,
I used "font-color"
If anyone knows this, then share your opinion.
As per the Android Issue Tracker,
Setting the hint to the editText respects the span, but the label does
not float on focus.
editText.setHint(hint);
Setting the hint to the textInputLayout ignores the span
textInputLayout.setHint(hint);
TextInputLayout uses internal class CollapsingTextHelper to draw the
hint, which does not support spans.
And as they said,
They have passed this defect on to the development team and will
update this issue with more information as it becomes available.
In TextInputLayout change the color of hint for single letter or alphabet
String firstNameText ="firstName *";
Spanned redStar;
String html = "<string style=\"color:firstNameText;\">firstName<span style=\"color:red;\">*</span></string>";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.L) {
redStar = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
redStar = Html.fromHtml(html);
}
I suggest you using just SpannableStringBuilder without Html.fromHtml:
SpannableStringBuilder sb = new SpannableStringBuilder("Hint with first letter black");
CharacterStyle cs= new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.black));
sb.setSpan(cs, 0, 1, 0);
((TextView) findViewById(R.id.description_messages)).setHint(sb);
In this piece of code the first letter becomes black. If you would like it to be bold too, use this:
CharacterStyle cs = new StyleSpan(android.graphics.Typeface.BOLD);
That just puts hint to EditText. There will be no floating text effect.

Android XML: L shape text view

I have 2 TextViews:
txtView1: User first name.
txtView2: User comment.
The design should be as shown in the picture below:
The problem is that i can't find a way to implement this design without adding a third TextView.
With 2 TextViews I got either overlapped text views or side by side text views.
Any ideas?
You can use even 1 TextView by using spans.
Here is what I got (I have only 1 text view):
And here is the code:
String firstName = "Pavel";
firstName = firstName.toUpperCase();
String firstPart = firstName + ": ";
String finalText = firstPart + "Text text text text text text text text text text text text text text text text text text text text text text text text text";
//0 - first line margin, 50 - other lines margin. Should be taken from resources.
LeadingMarginSpan paragraphSpan = new LeadingMarginSpan.Standard(0, 50);
//Bold span for the first name
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
//Color span for the notes text. Should be taken from resources
ForegroundColorSpan colorSpan = new ForegroundColorSpan(0x77000000);
Spannable spannableString = new SpannableString(finalText);
spannableString.setSpan(paragraphSpan, 0, finalText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(boldSpan, 0, firstPart.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(colorSpan, firstPart.length(), finalText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
Really recommend to get yourself familiar with this article to better understand spans

Different font width in #string of TextView

I have the string <string name="getStarted">Get Started!</string>.
I have set it's textview to android:fontFamily="sans-serif-light".
Can I have the Started! as a regular fontFamily where the rest of the string (Get) having a light weight?
I tried:
<string name="getStarted">Get <n>Started!</n></string> like <b></b> but well... n is not a tag (n for normal).
final SpannableStringBuilder sb = new SpannableStringBuilder("Get Started!");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
sb.setSpan(bss, 4, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make them also bold
yourTextView.setText(sb);
For consistency, based on #Gavin2058 's answer.
SpannableString sb = new SpannableString("Get Started!");
sb.setSpan(new TypefaceSpan("sans-serif"), 4, 11, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
yourTextView.setText(sb);
SPAN_EXCLUSIVE_INCLUSIVE helps you add more setSpan of the same style if you have a big string with multiple width words.

Seeking the Simplest way to have a TextView's text colored in diffrent colors

I want to have some of the string that is shown by a text view to be red, and some black, how can i do that?
is there a way to mention it simply on the XML file? (strings.xml)
I think you can use span. Text Style
For Example,
final SpannableStringBuilder sb = new SpannableStringBuilder(" text must be here ");
final ForegroundColorSpan fc1 = new ForegroundColorSpan(Color.rgb(255, 0, 0));
final ForegroundColorSpan fc2 = new ForegroundColorSpan(Color.rgb(255, 255, 255));
sb.setSpan(fc1 , 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(fc2, 5, 8, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
yourTextView.setText(sb);
(or)
In styles XML, declare the color values as shown below
<color name="redColor">#ff0000</color>
and use it in the layout xml, for the textColor attribute,
android:textColor="#color/redColor"
you can try this,
String.replaceAll(textToHighlight,<font color="red">textToHighlight</font>);
Textview.setText(Html.fromHtml(String));
I believe the only way of doing it is to use a spanned type as below.
TextView textview = (TextView)findViewById(R.id.myTextview);
String firstName = "FirstName";
String lastName = "LastName";
Spanned details = Html.fromHtml(firstName + "<br />" + "<small><font color=\"#767676\">" + lastName + "</b></small>");
textview.setText(details);
Hope this helps

Change color of textview in listview?

After I have a small list in a listview how do I go through and change the color of any text matching a string? They will always be the same textview. Do I have to do anything custom to do this or just loop through the listview?
use this to have different color
TextView tv = new TextView(xx);
SpannableStringBuilder text = new SpannableStringBuilder("your string");
text.setspan(new ForegroundColorSpan(Color.RED),
startIndex, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setspan(new ForegrounColorSpan(Color.GREEN),
startIndex,endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
and use fromHTML(String ) method for Styles
if you want to get the index of a specified string use indexOf("your string")
If you are making a custom list View than you have to inflate it and after that you can setTextColor(Color.parse("#ffffff"));
or you can take reference from this
http://developer.android.com/resources/tutorials/views/hello-listview.html
Thanks

Categories

Resources