How to get only visible text from a EditText - android

I have an EditText for a code editor running on Android. I'm applying syntax highlighting to the content, it's working well except for large Strings where the EditText scrolls off screen - the syntax highlighting is being applied to the entire Spannable on every key press.
Does anyone know how I'd go about capturing only the text that is visible to the user?
I can only think of nasty hack based on cursor position but that would break when the user scrolls.

Can use the function below to get the visible text offsets. You'd input the raw x,y corners of the edit text, and it will return the nearest text offset (i.e. character position).
EditText et = ;
et.getOffsetForPosition(x, y);
Once you know start / end of the visible text, you set your span accordingly. If you are just changing coloring (i.e. not height / width) then there shouldn't be any circular dependency.

Related

Zooming text alone inside an android textview

I have a text view with numbers displayed on it. It shows a bill amount value. I would like to have a zoom animation on it, on click. The text view is round shaped and presently zooming zooms the entire circular text view. I used an anim file that uses scaleX and scaleY to zoom. But I just want to zoom the text alone when a user clicks on it. How to zoom text alone inside a textview in android and not the entire textview?
may be you can change textSize property in onClick event of the textView.
void setTextSize (float size) , or this answer may be useful
https://stackoverflow.com/a/20303367/4233036

How to show large text without "Font size too large to fit in cache" and not broken shared-element transition

I have shared element transition for TextView with some text. Text size changes from 25sp to 200sp.
If text contains colored Emoji (☺) I get "Font size too large to fit in cache".
As I know there is android property "ro.hwui.text_large_cache_height" which determine max height of gliph texture, but it is not equal textSize, so I don't know how I can use it.
When I set textview.setLayerType(View.LAYER_TYPE_SOFTWARE, null) - this solve the problem with showing text, but broke the transition - text appears only at the end.
How could it be fixed? Thanks.

jump to some line of EditText or TextView with multiline

I have a multi-line EditText/TextView, say 1000 line.
The contents can't be shown on one page, actually there will automatically be a vertical scroll bar.
I want to make a button that can jump to line 500, and the result view start from exactly at line 500.
Anybody know how to achieve this?
You should be able to calculate the scroll in the Y direction using TextView.getLineHeight(), TextView.getLineCount() and TextView.getHeight().From there you can call TextView.scrollTo(0, calculatedY).I'm assuming you always want to have the left side of the view visible. Of course, if your lines were longer than the current View could display, you could similarly calculate an X scroll position also.

Android ListView fast scroll with sections: section text too long

I am using ListView to implement a timeline. I enabled FastScroll and used SectionIndexer so that user could drag the scrollbar and see the section text displayed.
The problem is all these are built-in UI. The textview that displays the section text is too small for me, I am trying to display 05pm and it's too long for the textview(or other UI?).
Any easier way to resolve this? For instance, a method I can set the font size of the section text or the textview layout?
Thanks!
Looking through the source code for AbsListView, you can find the class that handles the fast scrolling mechanism which ends up being FastScroller. FastScroller actually draws the image drawable and text on the canvas that was provided to the AbsListView like so...
canvas.drawText(mSectionText, (int) (rectF.left + rectF.right) / 2 - hOff, (int) (rectF.bottom + rectF.top) / 2 + mOverlaySize / 4 - descent - vOff, paint);
The code above actually draws the text on top of the bottom image drawable so it does not respect the bounds of the bottom drawable, which is why the text is actually overflowing and not being cut off. As to why this is designed this way? My guess is that the intention of the index feature was to be mainly used for single characters such as A, B, C, etc... and therefore the fast scroll index feature was designed to fit that.
So, to give a definite answer to your question, there's really no way to change the text size or change how the text is being drawn unless you modify AbsListView and FastScroller to suit your need.

Android: Auto-Scale Text in ListView item

I have built a ListView and my items - at least in part - contain titles of various (text) lengths.
In order to enable the user to read as much of the title as possible, I'm trying to change my adapter to auto-pick a feasible font size for my texts.
So I'm working with the TextView's paint object to measure the text in a basline font size (14dp) and try to compare against the available space. If the text is too big, I reduce the font size to 12dp (later I might think about reducing it even further).
// Note: vh.filmTitleTextView is my TextView, filmText contains the title I want to display
filmTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
float textWidth = vh.filmTitleTextView.getPaint().measureText(filmText);
if (textWidth > vh.filmTitleTextView.getWidth())
vh.filmTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
The issue is that on first run, vh.filmTitleTextView.getWidth() always returns zero. I guess this is because the layout has not been rendered before and the size is not yet known.
I can't just go with the full size of the ListView because the textView doesn't have the same width (despite the fact that it is set to layout_width="fill_parent") - there are some elements around it.
Any ideas?
Had a similar problem that was my bane for a long time - this might help ya: Auto Scale TextView Text to Fit within Bounds

Categories

Resources