How to set gravity center of SpannableStringBuilder? - android

I'm using two different fonts in one TextView: first is SVG font , second TTF font.
To set icon with text to TextView I'm using this:
String icon =getContext().getString(iconId);//""
String text = getResources().getString(labelId);//"AnyText"
int iconLength = icon.length();
String fulltext = icon + " " + text;
SpannableStringBuilder spLabelWithIcon = new SpannableStringBuilder(fulltext);
spLabelWithIcon.setSpan(new CustomTypefaceSpan(TypefaceManager.ICONS.getTypeface()),
0, iconLength, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spLabelWithIcon.setSpan(new CustomTypefaceSpan(TypefaceManager.GOTHAM_BOOK.getTypeface()),
iconLength, fulltext.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
myTextView.setText(spLabelWithIcon);
Result: the icon is not on vertical center of the line(or maybe text is not on vertical center):

Related

How to modify SpannableString to string below icon?

Currently I have a spannableString like this, the icon is at the left of the title string. The problem is, is there any way to make the icon above the string , while the icon is align center?
Sample like this:
[] <==== This is the icon
TitleTest <=== This is the title
ABCD
Thanks
Here is my code attempted:
Drawable image = context.getResources().getDrawable(R.drawable.ic_launcher);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
SpannableString sb = new SpannableString(" " + tabTitles[position]);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
Thanks for helping
I was looking at https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout today and had the same question. Adding a newline to the SpannableString worked for me, so the following modification will do the trick:
SpannableString sb = new SpannableString(" \n" + tabTitles[position]);
Put the three views (ImageView for icon and 2 TextViews) in a vertical LinearLayout and use the android:layout_gravity property in XML to centre the views in the LinearLayout. Additionally set the android:gravity property on the TextViews to be "center" to centre the text within the TextView.
If you don't want to use XML (which seems to be the case) layout_gravity can be set using the LinearLayout.LayoutParams constructor with parameters for width, height AND GRAVITY. Text alignment can be done using mTextView.setGravity(Gravity.CENTER);
Just use the drawableTop attribute for this.
<TextView
android:drawableTop="#drawable/my_drawable"/>
Or from code:
myTextView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.my_drawable, 0, 0);

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

create spaces between image and string in spannable setSpan

I need to place an image and string in the center of the button. Need to create spaces between them.
a Hello (%1$d)
My code:
String textValue = getResources().getString(R.string.hello_text);
final Spannable buttonLabel = new SpannableString(String.format(textValue, 2));
//buttonLabel.setSpan(new ImageSpan(getApplicationContext(), R.drawable.hello_imagel,
// ImageSpan.ALIGN_BOTTOM), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mButton.setText(buttonLabel);
I need give spaces between the image and the text. I tried giving spaces in the strings.xml but it doesn't display the spaces. After adding the below xml, the image is to the left and the string is in the center. I want the image and string to be in center with few spaces between image and string
xml:
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#2A2A2A"
android:drawableLeft="#drawable/hello_image1"
android:gravity="center"
If you don't need to show an icon inside a string you can add drawable to your button.
i.e. an icon at the left side of the text:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/icon"
android:drawablePadding="20dp">
where #drawable/icon is your icon and 20dp is padding between icon and text
Shure you can do the same from code - take a look at setCompoundDrawables and setCompoundDrawablePadding
sorry to be late, I hope this method helps you, is on kotlin:
fun setLeftIcon(roundButton: RoundButton, iconId: Int) = with(roundButton) {
if (iconId == DEFAULT_IMAGE_ID) return
val currentText = text.toString()
// The spaces are a workaround to add some space between the image and the text
val buttonLabel = SpannableString(" $currentText")
buttonLabel.setSpan(
ImageSpan(context, iconId),
0,
1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
text = buttonLabel
}

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

TextView with different sizes

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));

Categories

Resources