android display a fraction in a textview - android

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

Related

Change attributes to a selected part of text in EditText

I've searched something around here but nothing came up.
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part.
I've already tried with the spannable thing, from another question:
TextView myTV = (TextView)findViewById(R.id.test);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
I assume that I have to assign it to some onClick method..Also my problems are those two number, I should put some "selectedText" there instead I think.
Is this even possible?
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part
If you want the user to "change attributes... of a selected part", my recently-updated RichEditText offers that. I do not have color or text size going yet, though, as those will need a toolbar (rather than my current action mode support).
Also my problems are those two number, I should put some "selectedText" there instead I think
You are probably looking for getSelectionStart() and getSelectionEnd().

changing BulletSpan icon in Android

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.

how to set upper small text in android TextView?

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

Set alignment to a single line in multiline edittext android

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.

Multi Formatted TextView

I'm trying to have the layout of the comments on a progam in the following form:
I want the USER_NAME in Bold, and the DATE in Enphasis (The red part should be normal, I don't want it red). My problem comes with the red part. I cant get the cooment to be displayed like this using layouts, I've been able to get:
and
Is there any way to get the comment working like the one I'm showing. I've thought about a multi formatted textView, is it posible??
If getting the comment like this is not possible, just say it's imposible.
One possible solution is to put two TextViews into a Linear layout one after another and then use SpannableStringBuilder to assign the formatted text to the first view.
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(text);
sb.setSpan(createBoldSpan(), 0, lengthOf(userName),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
firstTextView.setText(sb, TextView.BufferType.NORMAL);
createBoldSpan should return TextAppearanceSpan
private TextAppearanceSpan userNameSpanInBold(String userName) {
return new TextAppearanceSpan(...);
}
please follow API docs for TextAppearanceSpan - you can create one using custom style.
You could use a WebView with loadData if HTML markup would be good, or alternatively delve into the realm of Spannable text.
I've not worked with Spannable text myself, but essentially it allows you to attach attributes (e.g. styles & colors) to parts of text in a TextView.
The relevant method is TextView.setText(CharSequence text, TextView.BufferType type) with BufferType of SPANNABLE. The documentation I've seen is.... confusing.

Categories

Resources