I want to set start margin only for the first line of TextView (Not first line of each paragraph)? I have used the following code:
SpannableString s = new SpannableString("this is a test"+"\n"+"this is a test");
s.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, s.length(), 0);
textView .setText(s);
Here start margin is getting set for all lines of the Paragraph.. I don't want that. I want start margin to be applied for first line only.. Pls help..
You actually already answered your own question.
All you need to do is to set span lenght to 1 instead of s.length().
if (s.lenght() > 0) {
s.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, 1, 0);
}
That way margin will only be applied to the first paragraph.
As it has been suggested here, I also think the best solution is to simply add a few spaces at the beginning. If you only need the margin space at the very first line of the TextView, I don't see any reason why you'd need to do something really advanced.
If you need to change or use the text in the TextView at a later point, you can just use the substring() method to get only a part of the string, i.e. s.substring(2, s.length());
It's not a perfect solution, but I think it'll do.
Related
I am using BulletSpan(BulletSpan.STANDARD_GAP_WIDTH)
How can I change its icon from a circle to ✓?
SpannableString s = new SpannableString(text+"\n");
s.setSpan(new BulletSpan(BulletSpan.STANDARD_GAP_WIDTH), 0, text.length(), 0);
I was also trying to style the bullet icon used by Android but didn't find any answers online. I ended up using an alternative approach which may help you further as well.
I didn't use BulletSpan but I used LeadingMarginSpan instead. You can add whatever character you want as icon (such as your tick character) in the text to be "spanned". By setting the first line margin and rest margins correctly you can achieve the same effect as the BulletSpan.
I want to add small upper text in android textView
how would you implement this?
two textViews both layout_below the same element and then
make the second smaller?
or is there a way in one textView to put style to one word
(can be longer or shorter in other languages)?
You can achieve that by following code
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("2580<sup>TH</sup>"));
Solution from Android Layout framework, would put two textViews in side a Linear layout and that is it.
Another way, is making the small text as an image. Than you can add it to text view directly.
you can do it like this:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("YourRank:250<sup>TH</sup>"));
also you can do it like this:
String str=getString(R.string.rank); //YourRank:250
String str2=getString(R.string.superScript); //TH
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("+str+"<sup>"+str2+"</sup>"));
it worked like charm!
You can use SpannableStringBuilder as fallows:
String text = "YourRank:250TH";
SpannableStringBuilder spannableString = new SpannableStringBuilder(text);
spannableString.setSpan(new SuperscriptSpan(), text.length() - 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
YourTextView.setText(spannableString);
Is it possible to set alignment on a single line or some lines in multiline edittext android?
If yes, then how, if no, then why not?
I get this answer from another question posted under the similar topic
"You can't apply multiple alignments within the same editText control. You'd have to solve your problem with some kind of manual padding of the text content"
So how can manual padding be inserted for selected lines?
you can use Alignment span to set alignment for a single line and multiple lines in multiline EditText provided you know the start and end index for the line. For example:
SpannableStringBuilder s = new SpannableStringBuilder("some text ");
AlignmentSpan span = new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE);
s.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t.setText(s);
You can apply alignment to multiple lines at once by covering multiple line between start and end index.
Is it possible to change the single textView size dynamically?
In the below link the text size is dynamically changed. ? can anyone guide me on how to do this. thanks in advance
Thanks for all, at-last i used three different textview to design whatever i need. Thanks for all.
Yes. You can use SpannableStringBuilder to styling the text.
Same question is registered at here
Try following code
span.setSpan(new RelativeSizeSpan(0.8f), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Here, you need to set start and end as per your requirement. 0.8f is size of text here. You need to pass float value here.
Code
textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(textView.getText());
span.setSpan(new RelativeSizeSpan(0.8f), 0, 7,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new RelativeSizeSpan(1.8f), 7,
txt.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(span);
Output
I can't seem to figure out a way to easily display a fraction in a textview. Let be more clear i need a fraction with a number over a number in a textview and then there should be able to add more text after that all in the same textview. I have seen this linke Android - displaying fractions using unicode
And the solution does not work great especially when playing it into a alertdialog the formating is lost.
I have also tried this
SpannableStringBuilder test = new SpannableStringBuilder();
SpannableString content = new SpannableString("3");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
test.append(content);
test.append("\n");
test.append("56");
This works great however if I need to add more to that line it will be displayed on the bottom and i need it on the top line. If i try to insert into the top line then the bottom line will move because the textview is centered (also it wont work by aligning the text view in any particular way since i may have text before the fraction).. *I do not want multiple textview
The solution was to use this html format from the link. However you must add a extra space on the top and bottom to keep it from being cut off.
SpannableStringBuilder test = new SpannableStringBuilder();
test.append("\n");
test.append(Html.fromHtml("<sup>5</sup>/<sub>9</sub>"));
test.append("\n");