Android ==> Which layout should i use? - android

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.

Related

Positioning views in Horizontal Scroll View

I'm trying to build a point-and-click adventure for Android without using any pre-written engine, but I'm stuck in a really crucial point!
I have a HorizontalScrollView bigger than the screen, so the user can scroll left and right in portrait mode to search around rooms, now what I need is to insert items that the player can use inside this View.
I'm trying to use static ImageView, but I'm really confused on how to insert Views in an absolute position inside the HorizontalScrollView. All I know is that Android manages Views location relative to other views (align on top, next to, bottom of), but what I use if I need to position a View in a specified position using specific coordinates without worrying that the image will be misplaced in a different screen size for other Android devices?
I really am confused on how position views in Android :/
Consider creating your own view instead of abusing HSV.
Custom views, doDraw() method, canvas and even gesture detection is not that hard.
Here is a link that will help you with basics, simple viewport implementation for android with scrolling via key events: http://sonnygill.net/android/android-viewport/
what I use if I need to position a View in a specified position using specific coordinates without worrying that the image will be misplaced in a different screen size for other Android devices
Actually, that's exactly the reason ViewGroup (layouts) were created.
You should choose the right layout based on your requirements.
If you do want to position the views by yourself, you can create your own custom ViewGroup by extending one of the view group classes and override the onLayout(...) and onMeasure(...) methods which are used to position and measure child views (in you case, probably the ImageViews) respectively.
You can use this example of FlowLayout as a reference on how you can write your own custom ViewGroup.
Please note that if your content is bigger than the screen as you mentioned, the HorizontalScrollView should be a parent of a single child (which should be the ViewGroup containing your images).

How to create a regular, resizable grid without nested weights?

I've one of the simplest layouts imaginable: A num pad.
I want to create a fragment containing a 3 x 4 grid of buttons. The layout should automatically resize the num pad to fill the available space.
I've learned, that GridLayout is not up to the task, and TableLayout/TableRow or nesting LinearLayouts means nesting weights, which is also discouraged for performance reasons. A RelativeLayout won't work either, because that requires at least one button with given dimensions.
So, is there a clean way to create a regular grid that will resize to fill its parent?
Any help is appreciated, thx!
You will need a custom compound control.
Check the following link:
http://developer.android.com/guide/topics/ui/custom-components.html#compound
Make the control fill the available space. Make it to have 12 buttons. Calculate the size and position of them based on their position and the available space.
Depending on your needs you might also need to override onMeasure() and onLayout() defined earlier in the above document, in the "Fully Customized Components" section.

How to change a view position and size at will

I am working on an Android Advertisement SDK. This SDK will charge fetch Advertisement resource and play it in a WebView.
Depending on the content of the Advertisement, the SDK need to change the WebView's position and size. For Example, A banner advertisement will palce in the top or bottom of screen, some advertisement maybe place at center, left or right.
Now I need the users to place the WebView in FrameLayout, So I could change the position and size of WebView at will and don't affect other views. I could set FrameLayout.LayoutParameters to adjust position and size.
Obviously, this is a strong restriction for users. Maybe they want use RelativeLayout and others.
So is there anyone have ideas about my issue? I am trying to use fragment now and didn't know would it work.
There is 2 approach to do this.
One is to set a layout ( forgot the name ) which allows you to set a component at custom position, size anywhere you want. As I remember it was 2 of those and one is deprecated form a version of Android. Google it those please, if you choose this method.
Other one it is with a trick:
Establish 9 positions (like the 9 pinch) and there use a predefined layout.
For eg. use a Relative layout.
1. if the banner need to be in position 1 than do x,y,z stuff - rearrange a bit the others, do what you need if is on the top left.
2. if the banner it is a "merged position" 1 and 2 from grid (a a lot wider), than you do other corrections.
and do many cases what you need.
Good luck
I have figure out this question.
Every android app is base on a FrameLayout(android.R.id.content).When I need to change position of a view. I could remove the view from its current layout and add it to FrameLayout(android.R.id.content).
That's what I want.

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.

How to add UI components on specific coordinates

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.

Categories

Resources