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);
Related
why there ist no counterpart to getLocationOnScreen? The intuitive way would be setLocationOnScreen... I have an Image View and want to get these coordinates to give the same coordinates to another Image View.
The mentioned intuitive way would be:
int [] location = new int [2];
imageView1.getLocationOnScreen(location);
imageView2.setLocationOnScreen(location);
But this set command is not available. This would be the easiest thing :( Is there any counterpart?
PS: my Image has no margins...
Thanks for help
Because position on screen is controlled by the layout that the ImageView is in, not the ImageView itself. If you need a layout that allows you to position subviews at specific coordinates, try a FrameLayout.
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.
I would like to draw vertical lines between Numbers/Letters in my TextView. So it should look similar to
A|B|C|D
without the use of the | character and use drawLine() instead.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example. However the lines dont line up as they should.
Not sure whats not working, help appreciated.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example.
That's your problem. For starters, you haven't specified that you're using a mono-spaced font. If you're not, then the letters won't be evenly distributed. Even if you are using a mono-spaced font, likely the padding at the beginning (and possibly end) of the TextView are going to offset things. I can't remember exactly how TextView measures things, but I suspect looking at actual left padding value would be a good start to find the left padding. If you want to use this with a variable width font, you'll want to use something like Paint.measureText to measure the width of the characters.
Once you have all that, you can add the width of the character(s) to the left padding to find the position to place each line.
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.
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.