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.
Related
I have a TextView and I set text which I receive from Backend.
the text is either from 1 to 3 words.
Maximum the textview can be 2 lines.
I am using setAutoSizeTextTypeUniformWithConfiguration
and text.breakStrategy = LineBreaker.BREAK_STRATEGY_SIMPLE
And I don't have any success.
wondering is it possible if the text is single word I don't want to split it. I would like to have it in single line with small textSize. if the text is 2 words and long I am fine to show it in 2 lines. The problem is it always breaks the word in 2 lines if it is long while I don't want.
The only solution that comes to my mind is before setting the text to a textview, check whether text has spaces in it.
If it has not that that means it's a single word, so set ptogrammatically maxlines to 1, otherwise set maxlines to 2.
EDIT: I missed text size part. I use this library for autoresizing and it worked so far https://github.com/jivimberg/AutoResizeTextView
it's automatically handled in androidx.appcompat.widget.AppCompatEditText and just set android:maxLines="2"
remove all other properties you set
How can I apply spans on EditText text when the user is composing?
For example the user has activated "bold" when composing, so every character input since then should be bold.
I thought about adding text change listener to the EditText and update the text as the user composes, but I wanna know if there's a better way of doing this.
The question was already answered in the comments, but to make it more permanent I will add a fuller answer.
In order to set a span (like Bold) wherever the user is composing, you just set a span on the text at the cursor (or selection) position.
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
int flag = Spannable.SPAN_INCLUSIVE_INCLUSIVE;
editText.getText().setSpan(boldSpan, start, end, flag);
The SPAN_INCLUSIVE_INCLUSIVE flag means that any text added before or after the span will be included in the span, even if the text length is 0 when the span is added.
See also
How to set other types of spans
Meaning of the Spannable flags
You can use the text watcher, and set the span in the editable that you receive in the afterTextChanged() method, I'm currently writing a rich text editor and this is the approach I've used with styles, and it works quite well, however, so far I haven't been able to set the spans that requires the paragraphs like quoteSpan, or bulletSpan.
But if you just want simple styles like italics, or bold etc., you can use this approach.
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);
I have a edittext and some buttons like bold italics underline bullets. Now when i select some text and press any button then using spannable string i am applying that particular span to that portion of the edittext like bold italics underline etc.
I am trying to put Bulletspan in edittext. Here is the code i use to put bullet span:-
s.setSpan(new BulletSpan(), text.getSelectionStart(), text.getSelectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
where text is my reference to the Edittext.
Now the issue is that I want that my bulleted text should come at some leading margin space or indentation from start like it comes in any text editor like MS Word.
Just tell BulletSpan how much leading space you want. This example would give you 16dp leading space on the first and subsequent lines (like in the text editors you mentioned).
new BulletSpan(16);
I want to access text of particular line in a multi-line TextView. Any suggestions on how I could do this?
Fetch the Layout (android.text.Layout) of the TextView by calling mTextView.getLayout(); There you can for instance use the getLineStart or getLineEnd methods to get the offset of the text. That combined with getText and used using normal String operations should probably be enough for u!
By default, text view is multi line. Set the text with new line characters for ex. "First line \nSecond line".
Another option is listed here : https://stackoverflow.com/questions/2610280/how-to-get-multi-line-text-on-a-button-in-android