To get a view relative to its parent, I can use getLeft(), getRight(), getTop(), and getBottom(). But how do I get a view relative to the top-most parent of the layout file?
So say my eve layout is RelativeLayout. Then say inside eve I have a number of nested layouts. And then deep within the hierarchy I have a EditText view. How do I get the location of the EditText view with respect to eve?
To find out absolute view location, you can use view.getLocationOnScreen() or view.getLocationInWindow().
View target = findViewById(R.id.target_id);
View parent = (View)target.getParent();
int x = target.getLeft();
int y = target.getTop();
while(parent != null){
x += parent.getLeft();
y += parent.getTop();
parent = (View)parent.getParent()
}
These code may be help you, because I didn't test it. I hope it would work.
Related
I want to get all views inside of an activity. like;
ArrayList<View> views = new Arraylist<>();
views.addAll(getAllViewsFromActivity());
what I want is "getAllViewsFromActivity()" function. I clearly want to get all the views even a single button. But I couldn't find any clear answer.
For the progress, it must be like this:
MainView : getWindow().getDecorView()
- -
RelativeLayout
- -
Button LinearLayout
-
TextView
How can I get this tree in Android programmatically? and also lets assume that I got this tree, Can I also identify the types of them like: view instanceof Button ?
the view you want to get is clear. so you can use the parent view(such as LinerarLayout) to get the children. so you can try this:
int count = parent.getChildCount();
for(int i =0; i< count;i++){
View child = parent.getChildAt(i);
//here you can to chage the view some proper.
//if you only want to change the background color, it belongs to view, don't
// need to cast.
}
I have two views A and B. View B is completely covered view A which means the bounding rect of view B is more than bounding rect of view A.
The best example I can give is, I have a fixed bottom tabs layout and a RecyclerView with multiple linear layouts. When I scroll, one of the linear layout will be behind the bottom tabs layout.
So, How can I programatically check If view A is completely covered by other views (other views might not be known upfront)?
Is there any possible way to identify this?
Update:
I have tried the solution mentioned here. But it didn't solve my problem. In my case, the view bounds are exactly matching and I want which view is on top.
After lot of struggle, I have found a way to identify if view is overlapped by other views in the following way.
AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfo.obtain();
viewA.onInitializeAccessibilityNodeInfo(nodeInfo);
if (!nodeInfo.isVisibleToUser()) {
// View is not visible to user. This also validates if viewA is overlapped by other views
}
nodeInfo.recycle();
Use the following method it will help you to find view overlapping:-
private boolean isYourViewOverlapping(View firstView, View secondView) {
int[] firstPosition = new int[2];
int[] secondPosition = new int[2];
firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
firstView.getLocationOnScreen(firstPosition);
secondView.getLocationOnScreen(secondPosition);
int r = firstView.getMeasuredWidth() + firstPosition[0];
int l = secondPosition[0];
return r >= l && (r != 0 && l != 0);
}
I made a linear layout and it contain some Image views . when i move one with (set X(view.get x + 10)) function, it moves... but it moves behind other views. that the view become hidden.
and the other problem is when i get X & Y of the view, its always 0,0. but the view is in the middle of the screen. what should I do??? should i give up with linear layout??
if(wichmov == "right" ){
if(martin.getX() < width){
martin.setX(martin.getX()+ deltax);
}
else if(wichmov == "left"){
if(martin.getX() > 0){
martin.setX(martin.getX()- deltax );
}
}
}
this is how i move it.
When are you trying to call getX()? This might be something to look into: View getX() and getY() return 0.0 after they have been added to the Activity
If you're making the call in onCreate, it'll be zero.
I just figured it out.
I use a relative layout and then set the linear layout as match parent. After designing the background with the linear layout, I define an image view after the linear layout and inside of the relative layout, and then in Java, I set the position of it in the exact place I want it to (front of the special box of linear layout that I wanted it move), and width and height of it too.
The following code will help you to place your view to the exact place of your screen you want it, and set its width and height:
DisplayMetrics metrics = this.getResources().getDisplayMetrics(); //getting screen size
width = metrics.widthPixels;
height = metrics.heightPixels;
view.setX(width *5/8); //setting the place
view.setY(height/4);
LayoutParams para = view.getLayoutParams(); //set size of view
para.height = (int) (height/2);
para.width = (int) (width/4);
view.setLayoutParams(para);
I have a LinearLayout which can have many child views. Child views are added programatically.
i need this distance
<---------------------->
--------------------------------wrapper ln----------------------------------
- ... cv4 cv3 cv2 cv1 -
----------------------------------------------------------------------------
Wrapper ln gravity is setted to right, so every added child view putting right.
For example when I click to "cv4" i need to get the distance from the right side of wrapper linear layout.
Child views are of LinearLayout type.
You can call getWidth() on the parent LinearLayout and getLeft() on the dynamically added view to get the position from the right:
parent.getWidth() - c4.getLeft();
You can also hook onto a OnGlobalLayoutListener so you can get the co-ordinates every time it changes its dimensions:
parent.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
int right = parent.getWidth() - c4.getLeft();
}
}
Is there any way to add a view to another at specified place? i.e I want to add a child view at (x, y) location of parent view. Please help me.
parentView.addView(childView);
You can do this by using the LayoutParams in a RelativeLayaout
An example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.leftMargin = 50; //Your X coordinate
params.topMargin = 60; //Your Y coordinate
parentView.addView(childView,params);
You can't add a view at (x,y) location of the parent. However you can trick it to achieve something like this:
Have your parent be RelativeLayout
Define your child view with android:layout_alignParentLeft="true", android:layout_alignParentTop="true", android:layout_marginLeft="X" android:layout_marginTop="Y" (or define the same things via code using LayoutParams)
Add your child to your parent: parentView.addView(childView)