How to add UI components on specific coordinates - android

I have to create a UI with components(TextView, Bottons etc) placed at particular coordinates as specified in an XML coming from a server. I am currently using AbsoluteLayout but since its deprecated i want to use some other method.

Use FrameLayout, this way you can position x/y exact anywhere using the margin (i.e. marginTop, marginLeft) properties of the components.
http://developer.android.com/reference/android/widget/FrameLayout.html

You can use the AbsoluteLayout. There you can set the exact coordinates of the UI element. However, I would no really recommend to use in any situation, because you can get in trouble with different screen size/resolution.

Related

Calculate margin using AccessibilityNodeInfo

I am developing an accessibility service on android and I want to check the space between on-screen elements. Currently I am already using AccessibilityNodeInfo and its getBoundsInScreen() method to read the touch target size of clicked elements.
Do you know if there is a way to retrieve the margin of an element?
There is not. Anything that you came up with would be a hack.
You could compare the on screen coordinates of elements. But, then you're not going to get a "margin" just the space between them. There is no way to get a "margin" property.

Android UI based on top and left property

I was working with Android UI in Eclipse and found it to be bit hectic. Designing layout using layout managers is bit time consuming. So i was wondering whether it is possible to specify the position of the UI elements based on (x,y) system i.e top and left property which is widely used in Visual Studio IDE for VB,C# etc ?
Positioning element based on top and left property would provide much flexibility.
How would that be flexible? Yes, doing layout correctly takes time, but if you do it right, it will scale properly to any screen size. If you're using X/Y coordinates, you will be hardcoding to a specific screen size, which is an especially bad idea on Android (as there are a multitude of screen sizes available).
If you need x, y positioning, you can use a FrameLayout with foregroundGravity set to top|left, and use layout_marginLeft for the x value, and layout_marginTop for the y value.
You can use AbsoluteLayout and suppress deprecation warnings in your code, but think of how will it look on different screen sizes?
I would advise to use RelativeLayout in your case.
As far as I know, there is no built-in layout that is based on (x, y) coordinates. You might be able to find 3rd party libraries that can do this for you. However, I'm skeptical that they will provide satisfactory results. Remember that Android is deployed on a wide variety of devices which include a range of different screen sizes and resolutions. This means that you can make the UI look pretty on one device using specific coordinates but it won't look very good on other devices.
Personally, I edit my UI layouts directly in the XML files. I find that this provides me better control than using the Eclipse UI editor. You still have to learn how the layout managers themselves work.
Android tries to ensure that your layout components are arranged nicely so that they:
don't overlap with each other
don't go off the screen space
look similar on different screen sizes
etc
It gives you nice XML Attributes to help you arrange your layout. I would recommend you use RelativeLayout for this application, because it allows you to put your layout components in positions RELATIVE to each other.
Some XML attributes you can specify are given here: Android Reference, RelativeLayout.LayoutParams

Adding UI elements(textview, webview...) to a layout in code and be able to size and place them manually (dp or px)

I'm working on a UI class for an app. The goal I'm trying to accomplish is to be able to at runtime create UI elements and add them to the layout. I then want to be able to size and move them manually (in code). For example I want to add a webview whose size is 234px by 450px and is placed at 32, 32.
Currently I'm using a relative layout and placing objects in it and then moving them around. I can move things fine by using view.setX() and view view.setY() but using the view.scaleX() and scaleY() doesn't work in the way that I want.
Should I be using a relative layout or is there another option.
I'm building for API 15
I switched to an absolute layout, though it is deprecated it allows me the exact freedom and control that I wanted.

Android SDK, Placeing widgets/buttons etc at will?

Hey Im new to android developing and I have a quick question. Is there a way to place buttons where I want them on the view? Right now they only seem to be placed where there is specific spots for them. Why cant i drag and place them where ever I want like in the iphone sdk for example?
Is there a way to do so or does this functionality not exist? thanks.
Android (eg. similar to Qt) uses a concept of layouts. This is especially useful when you're creating UIs that can be displayed on different devices with different DPIs, different screen resolutions, etc.
So instead placing your buttons at pixel coordinates you put them, independent of device screen resolution, into layouts.
Read more in User Interface documentation. Using Eclipse ADT plugin you can visually create layouts. You can even embed one layout into other, creating eg. LinearLayout in RelativeLayout. This gives much more possibilities of creating screen scalable applications (one app on phone and tablet for example).
There is AbsoluteLayout, but that class is deprecated. The recommended strategy is to use a RelativeLayout (you can control the position of views by setting layout margins) or build your own custom layout class.
An android UI screen is build for various screen sizes , due to which you can not specify an exact location for the UI component .
Android instead uses the concepts of Layouts where each layout has a different behavior. Here are a few of them.
LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute
TableLayout positions its children into rows and columns
RelativeLayout , one of the most used layouts , lets child views specify their position relative to the parent view or to each other (specified by ID).
Android uses layouts to design the UI. For example, a vertical linear layout stacks one element on top of the other.
AbsoluteLayout is the most precise, but it's also harder to maintain and can get messy.
I recommend RelativeLayout. It positions UI elements relative to other elements.
If you don't really need that much precision, one more option is nesting layouts within layouts. But doing that too much gets messy and RelativeLayout becomes the better option.
A UI element's position is determined by the layout you choose. See this link.

Android ==> Which layout should i use?

I want o be able to add items to my layout, and set the exact position of these items.
x,y,width,height.
I am currently using AbsoluteLaout which is deprecated, is there any other layout that allows me to locate objects on the screen, by specifying their x,y?
I also need to be able to read the x,y of all these objects on the screen.
I already use getLeft(), and getTop().
You can use RelativeLayout and set margins to position. This is going to kill various screen size compatibility.

Categories

Resources