How to get the view coordinates in relativelayout in android - android

I want to get position of view located inside a RelativeLayout ?

You can use getLocationOnScreen method to detect position of your view and layout on screen. Then just calculate relative coordinates.

Related

Scrolling in all directions

I've been trying to implement an activity where I can scroll horizontally and vertically in all directions, I've tried out using gesturelistener and the "scrollby" method in View, however the screen is still static. I've also done a nested horizontal and vertical scroll in xml, however this only gives me scrolling to the right of the screen and bottom. It seems i cant move in the negative x direction.
Thanks.
Customize ViewGroup
Override OnTouchEvent
Detect MotionEvents
Track Finger Points
Calculate Distance Moved
Modify the X and Y of the canvas Content Rectangle.
Finally in your layout add child views you want to scroll inside this ViewGroup
Source code: https://gist.github.com/jayakrishnan-pm/ScrollingLayout.java

getMetrics() from a Scrollview?

How to get Metrics from a ScrollView ? I need it to put my Views (Button, TextView) inside my scrollview and i want to set x and y position. Anybody have an idea ?
Thank you guys
First of all, you can't put more than one view into a scroll view.
And you shouldn't use absolute positioning by manually setting x and y.
Try to wrap your views into some layout (looks like it should be LinearLayout with vertical orientation in your case).
Check this: http://developer.android.com/guide/topics/ui/declaring-layout.html

How I check an imageview ontop of another imageview or not

suppose i have two image view v1,v2.
imageview v1,v2;
v1=(Imageview)findviewbyId(R.id.view1);
v2=(Imageview)findviewbyId(R.id.view2);
How do I check that v1 ontop of v2 or not.
You can set onclick listener for both of it so that when the user clicks so that the top of the imageview only gets clicked. By this u can get which imageview is up top.
you may check by getting co-ordinates of the image by using this link
and can check which image is top and which is bottom with that co-ordinates
one suggestiton: get their location and height
minus them and configure if on top of
You can calculate it with the position and the width and height of the View.
Position: Use View.getLocationOnScreen() and/or getLocationInWindow().
Width/height: Use View.getWidth() and View.getHeight().
The view that is defined first in your xml layout will be drawn first. The next view will be drawn on top of the previous view if there is any.
By that principle, you can ask your frame/relative layout to check the ordering where the child views are ordered:
int indexV1 = layout.indexOfChild(v1);
int indexV2 = layout.indexOfChild(v2);
if(indexV1 < indexV2) {
// v1 is below v2
}

How to get the offset of a view inside a ScrollView?

There is a ScrollView and which has many children views. Is there any way to get the offset-y to the top of the ScrollView for every child?
Hmmmm, not sure if this is the quickest, best way to do this, but if you get the view's location on screen, and you know the top of the ScrollView, you can calculate the offset of the view relative to the ScrollView. To get the position of a View in the screen:
int[] location = {0,0};
view.getLocationOnScreen(location);
location[1] has the Y value. You will get negative values when the view is "above" the viewport.

Local Co-ordinate of Image view w.r.t to the root View

I have relative layout as a main View and it has few Image views which are place w.r.t to margin top and margin left. Now I want to check the imageViews opacity or transparent region at the pixel where touch is happened. Say I touched some where 533,240 .. I want to check whether that position is transparent or transculent or opaque.. Since imageview will always be square and i guess there will be part of image will be transparent but it should not be included in onTouch event thats why I aim for this that if i get the position of my touch w.r.t to local co-ordinate of the Imageview then I can give that pixel to the drawable.getTransparentRegion.container(int x , int y )
Explaining further.. .what I am trying to do is I am getting View on click or on touch.. From that view I am getting imageview ....and from that imageview I am trying to get Drawable of that imageview ..I have reached till here ... Now I want to get the touch event position w.r.t to the imageview so I can check it in drawable.getTransparentRegion ? ... I hope you understood
At the end I want to differentiate between the transparent part of the imageView i.e.(not needed part of the Imageview ) and the Transculent part that will be there of the drawable in the imageView...
get coordinates of imageview on window by
public void getLocationInWindow (int[] location),
pass integer array of two size to this method. Invoke this method after layout has been drawn, use location array to get imageview's left and top co-ordinates, add transparent co-ordinates to it, and you will get co-ordinates which can be compared to touchevent co-ordinates.

Categories

Resources