Android layout- relative vs frame - android

I have an application where I want to achieve a layout where the product image will be in the background and top of that to the right bottom, I want to place the price of the product and to the left bottom I want to place an add button.
Should I use frame layout or relative layout ???

As a practical rule, I think it's up to you. I personally tend to use RelativeLayouts because they're more flexible, but you can achieve the same effect with either.
This SO post explains the performance differences between the two layouts in more detail: FrameLayout vs RelativeLayout for overlays

Relative layout: When you have relation between siblings or parent.
Frame layout: When siblings are placed independent of each other and are only dependent on parent.
Based on your situation, you can opt in of any of these.

My advice, If you have specific size for your background for each device, then go for relative layout, and set the background to your image. Because when using background in RelativeLayout, It'll fit the size of the relative layout itself, whether the image suits the size of the RelativeLayout or not. (Can be stretched/pixelated/Not properly added)
If you're not sure about specific size, you should use FrameLayout, with ImageView, that handles the ScaleType, which can be centerCrop, and it'll fit the layout in good shape.
And for the TextView, use layout_gravity, which will handle the position based on the parent layout.

Related

Responsive imageview positioning

I have a list item that contains an image (red) and another layout (blue)
Normally, the layout would be the first one, but the black layout's width depends on user configurations, so it may not fit the whole content.
I thought about making it a Linear Layout and changing the orientation depending on its width, but i read somewhere its possible to do such things with layout only (maybe constraint layouts).
Is it possible to achieve this result using only "layout responsiveness", or do i have to input some code also?

Layout with decreasing height from one end to another in android (inclined on one side)

Here is what I need to attain
The Application logo goes inside that blue background. Can I somehow tweak Linear or Relative Layout to attain that inclined bottom? Or is that any other solution to this, other than simply using an image itself?
No, Views in Android are all rectangular, and LinearLayout and RelativeLayout only deal in rectangular measurements. You're going to have to do some special drawing or use a background image or something along those lines.

How to put a View in a determinate pixel/position

I need to put a view (a image for example) in a custom point of the interface.
Are there a method to put a view in a exact pixel / virtual pixel (dp) - zone of the screen?
I need that the view is displayed above the other views.
I think to obligatory put a RelativeLayout and inside it all the layouts of the app and then put the view relative to this layout but i need a method more transparent to the future developers be able use it
EDIT:
I forget to say that i need to put the view programmatically and in the run time. I create the interface, make things in my app and then put a image in a concrete zone of the view above the others views for example in the half of the screen or in the top or in the 20% of the screen or ...
You can set margins for layouts and padding for views inside them, and you can specify position relative to edges, or to other views, there. Also you can specify fixed position for layouts by replacing (match_parent/fill_parent or wrap_content) with actual size( Note that you should specify all dimensions in dp or dip)
For instance you have a linear layout and you want an image view to be 20 dp to the right of the left margin you can try something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android::layout_marginLeft="20dp">
<!-- Nest your views here and if you want you can set padding for them
</LinearLayout>
If you want your views to be on a certain part of the screen, like top of the screen/ bottom of the screen etc, you can nest all of them inside a RelativeLayout. This is a nice way to do it, and it will remove any confusion.
Good luck,
Arkde
What about a FrameLayout as in the example I posted here?
You can use AbsoluteLayout, although it's deprecated.

Advanced Explanation of Android Layout Properties?

I'm on a quest to learn how to layout components properly in Android. I'm a seasoned CSS/MXML developer and am having the hardest time getting a full understanding of layout properties in Android components.
One thing is I'm not sure the differences between these:
layout_margin vs. padding
layout_gravity vs. gravity vs. ignoreGravity
Should you use one over the other with Linear, Table or Relative Layouts? An example of something I'd like to learn is having an overall margin on a layout with separate components relating to the top/middle/bottom of the screen. The sdk docs are a good start, but they don't show how things work in different situations.
Any tips on where to go to learn a more complex/comprehensive layout design?
Any attribute with the prefix layout_ is a LayoutParams attribute. While most view attributes are parsed during view construction by the view itself, LayoutParams are special arguments to the parent view that provide hints about how the parent should size and position the child view. Which LayoutParams are valid on a view depends entirely on the type of the parent view.
layout_margin is therefore an instruction to a parent view that supports margins. It says, "put this much space between me and other views or the edge of the parent." Padding is space inside a view between
the view's edges and its content.
layout_gravity is gravity for a single child within its parent. gravity affects the contents of the view it appears on.
Which one you use depends on the result you want to achieve. If you want a layout to have a fixed amount of space between its edges and all of its contents, you want padding. If you want to move the layout's own edges in by a certain distance, you want margins. When you have layouts without backgrounds set, these two can be visually equivalent. When you start creating complex UIs where layouts have 9-patch backgrounds that visually group the contents the differences become apparent.
I hope you can see the difference between padding and margin. Padding is inside spacing while margin is outside spacing.

What is the most flexible layout?

What is the best layout to use to support the app on different devices (Size of screen)?
EDIT
I am not just talking about resizing the layout, obviously the OS does that automatically. I am talking about repositioning the objects in my layout.
by repositioning I mean moving the objects according to the size of the screen. For instance i created my layout for a larger screen which looks great, but when i run the app on a smaller device (smaller screen) some of my User Interface elements were out of the bound of my screen.
There is no "best" layout. Almost all types of layouts will scale to different devices (Android is designed around this concept) other than AbsoluteLayout, which is deprecated anyway.
LinearLayout is best if you just have a row (horizontal or vertical) of content to insert. Using values such as dip values, fill_parent, or wrap_content will automatically adjust themselves to their content or screen size.
RelativeLayout, as Vladimir pointed out, is best for layouts where views are positioned relative to other objects within the layout. For instance, a TextView positioned beside a "Submit" button, is a common example.
FrameLayout is sort of a replacement for AbsoluteLayout; you can layer objects on top of each other, just specifying margin offsets from the sides of the frame.
TableLayout is, as it sounds, a layout for Table style design. You can have multiple rows and columns, and set certain columns to stretch to fit the size of the display, so that no matter the screen size, the layout fits as you designed.
EDIT: If you're having objects falling outside of the screen area, try wrapping your root layout in a <ScrollView>. This will allow the layout to be scrollable.
e.g.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
..... //and so on
</RelativeLayout>
</ScrollView>
All Layouts are flexible in terms of size... the rule is: don't use AbsoluteLayout. That's all.
And with regards to the repositioning concerns... well, use always dips instead of pixels and you are good to go. Again, don't use AbsoluteLayout, the rest of the layouts should work fine on every screen size. Sometimes you can anticipate those "disappearing acts" by wrapping your layout in a ScrollView.
RelativeLayout is what you should be looking at. It easily resizes the elements relative to their neighbors. Just make sure to include drawables for all resolutions and densities
LinearLayout,RelativeLayout,FrameLayout are import Layouts....

Categories

Resources