I was wondering how i could set the exact x,y positions of a button in a relative layout. I want to try and not use an absolute layout. When i looked at similar posts most of them only showed how to align things exactly in the center or to the left of the screen, is there a way to align things at an exact position maybe by subtracting the distance from a margin by w/e dpi
RelativeLayout imply the concept of relative position. You can only set a position of a widget in Relation with other widgets positions (aligned to the left, under...) or the container (bottom, top etc etc).
you can position the widget and then try to adjust the distance with the padding attribute (there is a padding for every direction, left,right,bottom,top). With these maybe you can do it.
I was wondering how i could set the exact x,y positions of a button in
a relative layout
Those 2 things are diametrically opposed concepts. What is it you're trying to do? Why do you need things to be at exact x,y positions?
There might be but I don't know it. However I would think that simply placing a relative layout inside of an absolute layout but not filling it and placing your buttons on the exposed part of the absolute layout would work. Unless you REALLY don't want to use absolute at all.
Related
I'm building a simple app for a game that has labels, textfields and buttons. Every time I try to run the app the elements on the screen keep moving around in the emulator even if they look find in the editor. Picture of layout
I've tried adjusting them by dragging and dropping and experimenting by changing XML. I think I'm missing something, but is positioning elements on the screen the way you want usually this difficult?
If you use lots of views in vertical direction use Linear layout, if more on horizontal use relative layout as you can position elements left-right or center w.r.t each other or parent easily, use a combination of both to get best results.
Overhead schematic of Docks map in Modern Warfare
Overhead schematic of Docks with one callout name
I'm creating an app that will display names - "callouts" - for various specific areas of each map in the video game Modern Warfare. I want to be able to place the text (or a placeholder) in the same location, regardless of scale, transformations, etc. Is there a way to do this besides getting the (x,y) coordinates of each location in an image editor, then using those coordinates in my layout? That method seems extremely tedious because there's 10 - 20 callouts per map, and approximately 20 maps! I also want to avoid adding the text graphically so that I can change it as necessary in Android Studio.
If I place TextViews visually in the [Design] tab in Android Studio, will they stay in the same position relative to the background ImageView, regardless of scaling, transforms, etc.? Please advise, and I'll be happy to clarify if necessary. Thanks!
I think I've found the solution I'm looking for, folks:
Within a ConstraintLayout, have an ImageView to hold the map graphic, and TextViews for each callout label.
Constrain each TextView to the Start, End, Top, and Bottom of the ImageView.
Drag the TextViews to the desired location in the [Design] tab of the layout editor. This will automatically adjust the Horizontal and Vertical Bias, which is essentially a percentage of the parent's width and height, relative to the left and top of the parent.
In my preliminary testing, this seems to preserve the location of each callout label relative to the map, which is what I was hoping for. If anyone has anything to add, please do so!
I am using a Java application to display an image on the screen. I also am using an eye-tracker device which records the absolute pixel X,Y locations where the person is looking on the screen.
However, what I need to do is convert these X,Y coordinates from the screen positions into the X,Y locations of the image. In other words, somehow I need to figure out that (just an example) 482, 458 translates to pixel 1,1 (the upper left pixel) of the image.
How can I determine the image's placement on the screen (not relative to anything)?
I saw a few posts about "getComponentLocation" and some other APIs, but in my experimentation with these, they seem to be giving coordinates relative to the window. I have also had problems with that because the 1,1 coordinate that they give is within the window, and there is actually a bar at the top of the window (that has the title and the close and minimize buttons) whose width I do not know, so I cannot easily translate.
Surely there must be a way to get the absolute pixel location on the screen of a component?
If we are talking about Swing/AWT application than class java.awt.Component has method getLocationOnScreen which seemed to do what you want
And yes as #RealSkeptic mentioned in comments to question:
SwingUtilities.html#convertPointFromScreen
will do all this work for you considering components hierarchy
I realize that there's another question relating to "infinite" JScrollPanes, however I think that what I'm looking for is something that is subtly different.
Basically, I have a collection of objects which can be dragged in any direction, and the extent of the scrolling viewport should always encompass the bounding rect of all those objects. To put it another way, the document has no fixed "origin". Thus, if you drag an object to the left, off the edge of the screen, then the viewport extent should expand in the negative direction to encompass that object's new position. (It should also auto-scroll as you drag, but that's a separate problem I realize.)
I'm not sure how to do this using the JScrollPane API, which seems to want you to set the preferred size of the underlying component, but doesn't seem to have the concept of an offset or origin for that component. (The underlying JViewport seems like it would be able to do it, but I can't really figure out the JViewport API.)
The scroll pane and view port actually have nothing to do with it. What you need to is change the preferred size of the view ports view and let the scroll pane/view port take care of this rest.
Basically, what you need to do, is calculate the widest and highest points on your component and invalidate the view port, to force to recalculate it's layout requirements.
Create yourself a custom component (using a JPanel for example) and override the getPreferredSize method. This method should return the required size of your component.
When the size requirements change, call revalidate()
OK so it turns out that the simple answer is that scroll panes cannot scroll to negative coordinates. What you have to do, as #MadProgrammer suggested, is maintain a separate offset coordinate which is a Point. The offset stores the top/left coordinates of the entire document. When rendering individual objects, set the transform of the Graphics2D object such that they are shifted down and to the right by the offset amount, so that all object are drawn at coordinates that are positive numbers even though in reality they may be located at negative coordinates. Similarly, when calculating the preferredSize for the scroll pane, add in the offset to the document width and height. Coordinates from scroll bar events also need to be offset as appropriate.
This allows you to maintain the illusion that the document bounds are not constrained to be positive numbers, and the document's boundary can expand infinitely in any direction.
I have two views called x and y they are both black lines (for example I made the height of the x line is 1dp and width 230dp and as background filled with the color black).
Now i want to move the position of the lines programmatically (for example I want the y line 50dp to the right of the orginal position).
Can someone help me how to do this?
I have tried things such as setpadding but the line doesn't move.
Thanks in advance!
(ps: my minimum sdk is set for 7 so i can't use the newest api's).
Old Answer
Have a look at the Absolute Layout, it allows you to position
child elements using x, y coordinates. It is deprecated but it's the
only way in Android to do real x,y coordinate positioning.
I would ask what the main point behind what you are trying to do is
though? It sounds like you started with a goal, were led down a path
and now are asking how to get to the end of that path, rather than
asking how to do what you need to do.
Edited
For drawing graphs have a look instead at https://stackoverflow.com/questions/2271248/how-to-draw-charts-in-android.
Using a Layout class to draw a chart will only lead to a really slow app, since the layout classes are designed for creating relatively static layouts, not drawing full graphics.
Instead use either the Canvas and draw your drawables yourself or use the graphing packages listed in the SO question I linked above.