how to get horizontally position of edittext by get line number? - android

I want to get y position of edittext line with enter line number and it return y position of it
like as this:
Int YpositionOfLineNumber = getYposition(int lineNumber);
Can you help me?

An EditText holds a Layout object that handles the text layout, and exposes all sorts of info about line size, spacing, offset, etc. This object has several methods to get coordinate information for lines in an EditText. Depending on exactly what you mean by "y position", there is the Layout#getLineTop(int line) method, or the Layout#getLineBottom(int line) method.
For example, to get the y-coordinate of the top of the 23rd line of an EditText named edit:
Layout layout = edit.getLayout();
int topLine = layout.getLineTop(22);
Please note that these methods measure with respect to the Layout object's area. If your EditText has top gravity, then the results should be accurate. If not, then the results will have to be adjusted by the vertical offset of the Layout.

Related

How to get only visible text from a EditText

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.

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.

How to set the four parameters of View.layout(l, t, r, b)

I am facing the same problem in this question, but I have difficulty in understanding the exact meaning of the four position parameters in layout(int l, int t, int r, int b). I know they represent left, top, right and bottom respectively relative to parent, but "relative" to where exactly?
For example, if I translate the button down 100 pixel, in order to move the clickable area down 100 pixel should I set
// l t r b
button.layout(0, 100, 0, button.getHeight()+100)
? Does the integer 100 of second parameter mean 100 pixel down relative to the top of parent? Is the fourth parameter means button.getHeight()+100 relative to the top of parent also??? I tested on my device, the clickable area does not move down as I want. I'm very confused here, any help is greatly appreciated.
The parameters to layout are relative to the parent view's upper left corner. X increases as you move to the right and Y increases as you move down. It is not relative to the view's current position.
You probably don't want to call layout on a child view outside of its parent's onLayout method. There are several other bits of internal bookkeeping that happen as a result of calling layout that will change internal state in a way you don't want for simple animations.
Typically layout is called like this:
child.layout(x, y, x + child.getMeasuredWidth(), y + child.getMeasuredHeight());
The parameters are absolute, with (0, 0) at the parent's upper left corner.
But if you're simply moving a view around for animation you probably want to do something more like:
// Adjust horizontally; dx = change in x position
child.offsetLeftAndRight(dx);
// Adjust vertically; dy = change in y position
child.offsetTopAndBottom(dy);
The parameters to the offset* methods are relative to the view's current position.
If you write a ViewGroup class that moves its child views around in this way, remember that you can get a layout request at any time. At that point your ViewGroup's onLayout method will try to position the child based on its usual layout rules and it will need to adjust this positioning to reflect any other accumulated movement you want to preserve.

Finding the coordinates of a letter placed on TextView

I have a textview which contains longs text that covers half of the screen,
In this textview I have two words at different position above which I have to place a blank layout which will give it a glass effect.
I can do this by placing the textview in a framelayout and placing the layout above it.
The problem is how will I get the coordinates of the words, as the text may be arranged at different position on different devices.
I want to place a layout at the place marked white. Can some one please help
Thanks.
This will be helpful for you
public void getLocationOnScreen (int[] location)
Since: API Level 1
Computes the coordinates of this view on the screen. The argument must be an
array of two integers. After the method returns, the array contains the x and y
location in that order.
Parameters
location an array of two integers in which to hold the coordinates
See this code, this link, and also this link.
I'm not sure you can get the coordinates for the text directly from TextView - you should search inside TextView class source code. But.... if you render the textview as bitmap and then to seach inside the bitmap the coordinates of the text (if the searched text is blue should be easy to get the rectangle!
What you really want here is a backgroundspan :
span = new BackgroundColorSpan(Color.WHITE);
text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Android - fix TextView width by string

I think the answer to this question is probably so simple, but I'm struggling....
I have a TableLayout with multiple columns. I want the last column to be of a fixed width, but I want to define that width to just be able to hold the widest possible string from my program. i.e. it is always wide enough to contain "THIS STRING" without wrapping, or wasting any space.
I would like to do this as I have these TableLayouts within a ListView, so it looks very poor when the last column is of variable widths.
I have tried obtaining the string width, even going so far as to put it into a TextView, call getTextSize() then setWidth() on all appropriate TextViews. The problem I hit there is that gettextSize() returns pixels, but setWidth uses ScaledPixels.
I'm sure there is a really simple solution. Can anyone help?
Are you using android:width="wrap_content" in your XML layout to define the width of that last column?
Edit: I think I just understood, you have a list view, that holds a table and you want all rows of the list view to have the same length for the last row of the table. Right?
I can only think of one, very unelegant solution right now and it involves going over all strings before building the list view.
The general logic would be as follows:
Im going to suppose you are getting al strings from an array, lets call it data.
Establish a global float variable to represent the longest string you have, lets call it maxLength.
Create a textview (lets call it invisibleText) in your layout that wont be visible, you can do this by setting
android:visibility="gone"
Then:
int size = data.length;
maxLength = 0.0f;
for(int i = 0;i<size;i++){
invisibleText.setText(data[i]);
float thisLength = invisibleText.getTextSize();
if(thisLength>maxLength) maxLength = thisLength;
}
In you list view constructor:
TextView text = (TextView)findViewById(R.id.the_text_view_you_want);
text.setText(data[position]);
text.setWidth(maxLength)
The table columns should use android:width="wrap_content"
I didnt test this code, but it should work, i've done similar stuff before.

Categories

Resources