Android scaling dimensions of included view - android

I have a view where I defined sizes using dip.
I want to include this in other layouts in different sizes. I thought setting the size in the include tag
<include
android:id="#+id/abc"
android:layout_width="100dip"
android:layout_height="100dip"
layout="#layout/myInclude"
/>
would make that my included layout is resized proportionally but it doesn't. So everything I defined using dip in the included layout appears with the same size and breaks the layout.
Is this expected behaviour and what can I do to solve this?
A possible solution in to use LinearLayout with weights in the included layout, instead of hardcode in dip. But with dip I have more control, and don't have to use LinearLayouts everywhere...
Thanks in advance.

If you specify a size in dip or any other fixed unit, it won't resize automatically. That would be weird, generally when you hardcode a size you don't want your view to be resized.
Layout weights (and LinearLayouts) are the only way to go.

Scaling the included view can be made programmatically in your activity/fragment. For now that’s what I’m using.

Related

Size of my ImageViews is changing between different screen size. RelativeLayout

I'm using a RelativeLayout in the layout of my app.
When I'm choosing different screen size, my ImageViews doesn't keep the same ratio. I'm defining my layout size in hardcoded dp. I think the problem come from the fact that I'm using dp to define my layout and because of that when I'm using my app on different screen with different dpi.
Do you have tips to make my app fits different screen sizes with the same ratio?
Thanks a lot
Please check the height and width of your parent Relative layout , and define each child view with a relation to each other , Or you can move to constraint layout or Linear layout. You should add you layout XML file to understand the actual issue

android set button height [duplicate]

I'm using several buttons in my app, but both layout_width/height "wrap_content" and "fill_parent" looks weird. The former being to small and the latter too large - both looks weird, and the former is not easy to hit with your finger.
How should I size buttons? Is it typical to define their sizes in dip? Or should I use "fill_parent" with a padding? Buttons looks weird in my app, not so in others.
That is difficult to answer in the abstract. Here are some techniques to consider:
Use android:padding="4dip" (or some other value) to make a wrap_content Button a bit bigger
Use android:textSize on the Button to make the content bigger (use some size in scaled pixels, or sp)
If you want the buttons to fill the space but divide it among themselves, use a LinearLayout, give each button a height (or width, depending if column or row) of 0px, then use android:layout_weight to allocate space between them on a percentage basis. Here is a sample project outlining this technique.
I think it is better to use fill_parent with a padding/margin instead an exact width value. So you are more flexible when the size of the parent view changes.

Android: custom views with proportional widths and same heights

I try to implement a good reusable color picker for my Sketcher application. Instructions and screenshots are here: http://bit.ly/sketcherapp
The problem is I'm stuck with a good "resizable" UI which enable me to support wide range of devices with different screen sizes.
The top two widgets should be the same height and have proportional widths: 80 to 20. Also it would be nice to specify paddings in XML.
Current implementation is not good. I hardcoded some values into code and also it looks bad on Xoom devices because of inaccurate layout measurements.
Is there any way to implement this behavior? Ideally, I need some way to do it like with HTML tables (pseudocode):
table.width=100%, td1.width=80%, td2.padding=5px, ...
or something like that.
Current implementation:
code:
https://github.com/wargoth/Sketcher/tree/master/src/org/sketcher/colorpicker
layout:
https://github.com/wargoth/Sketcher/blob/master/res/layout/color_picker.xml
Thank you.
The top two widgets should be the same height and have proportional widths: 80 to 20.
Use a horizontal LinearLayout, android:layout_width="0dip" for both widgets, and android:layout_weight="80" and android:layout_weight="20", respectively.
Also it would be nice to specify paddings in XML.
Use android:paddingLeft and kin.
OK. I stopped boring with it and created dedicated layouts for each screen size.

Android: How to size buttons?

I'm using several buttons in my app, but both layout_width/height "wrap_content" and "fill_parent" looks weird. The former being to small and the latter too large - both looks weird, and the former is not easy to hit with your finger.
How should I size buttons? Is it typical to define their sizes in dip? Or should I use "fill_parent" with a padding? Buttons looks weird in my app, not so in others.
That is difficult to answer in the abstract. Here are some techniques to consider:
Use android:padding="4dip" (or some other value) to make a wrap_content Button a bit bigger
Use android:textSize on the Button to make the content bigger (use some size in scaled pixels, or sp)
If you want the buttons to fill the space but divide it among themselves, use a LinearLayout, give each button a height (or width, depending if column or row) of 0px, then use android:layout_weight to allocate space between them on a percentage basis. Here is a sample project outlining this technique.
I think it is better to use fill_parent with a padding/margin instead an exact width value. So you are more flexible when the size of the parent view changes.

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