I want to create a scalable view.
should I prefer using linear layout property: layout_weight
or using layout_width: X dp ? (which is also relative and not apolute like pixels)
what is the difference?
This highly depends on the exact use case.
layout_weight depends on the number and size of the other views in the same ViewGroup.
dp (density-independant pixels) depends on the density of the device.
Usually, dp is used to have a view displayed at the same physical size on devices with different screen densities, while weight just makes sure that a view fills a certain percentage of its parent ViewGroup.
It is my understanding that dp is just a general size that you want an object while weight is defining how much space you want something to take relative to the other things sharing the same space.
it makes it easier to create layout when you want some view to take for example one third (1/3) of the available space. How would you achieve this with layout_width? However you can easily achieve this using the weight property.
what more the weight property makes you layout looks the same on all screen sizes, even tablets. Which is not the case when you are using the weight property and usually if you do you will develop a separated layout for tablets (I'm not saying that you should do that, I only want to point out the difference).
layout_width and layout_height specify those two dimensions of a widget. You can use a dp value to give the size in a device-independent manner'
layout_weight indicates how to allocate any extra space in a LinearLayout. This means that if the orientation is set to horizontal, the LinearLayout will modify the width of the widgets it contains. On the other hand, if the orientation is set to vertical, the LinearLayout will modify the height of the children widgets.
Overrall, layout_width and layout_weight have different purposes, so it is improper to ask "which should I prefer". In a vertical LinearLayout, you can easily use both.
DP is not really relative, it's just a density-independent pixel (since tablet/phone screens have different pixel densities).
You can use layout_weight to scale a control to e.g. 1/3 of the screen, no matter how small/big the screen gets.
Say you have:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<View
android:layout_width="20dp"
android:layout_height="wrap_content"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
The first item will always be the same width on all different devices, the second one will fill 2/3 of the remaining space, and the 3rd one the remaining 1/3.
It all depends on how you want to make your layout scalable (which parts of the ui should grow/shrink, which ones should stay the same size).
First of all, there is no relationship between android:layout_weight property and dp.
dp (Density-independent Pixels) is basically a unit of measure.
An abstract unit that is based on the physical density of the screen.
These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px.
To calculate the pixels and density points you can take the following formula.
px = dp * (metrics.densityDpi / 160f);
You can see here all the supported dimensions by Android.
android:layout_weight defines the "weight" of each view inside the parent layout.
E.g. you have a LinearLayout which contains a TextViewand ListView and their weight is, respectively, .25 and .75. That means your TextView can use 25% of the available space in the screen and the ListView the other 75%.
Make sure, in the end, the sum of the total weights is equal to 1 (100%).
Related
I'm quite new with Android so I was wondering if it's a okay to give layout height or width in dp? Also if there is any other approach other than wrap_content/match_parent or dp than do tell. Thanks in advance.
Yes you can specify height and width in dp.
For conversions between px, dip, dp and sp please see stackoverflow question What is the difference between “px”, “dip”, “dp” and “sp”?
For layout I've found "match_parent" to be applicable in most cases. (To give you more context, "match_parent" used to be called "fill_parent" prior to API level 8). It basically means the view is as big as its parent, just without padding.
If your intention is the make the view just big enough for its content, then use "wrap_content".
Yes it is ok to give height & width in dp. You can also use fill_parent at place of match_parent.
Fill_parent was depreciated in API level 8. So if you are using API level 8 or above you must avoid using fill_parent.
For more information see this http://code2care.org/pages/fill_parent-vs-match_parent-vs-wrap_content/
You can give layout height and width in dp. Mostly used for custom size arrangement
Wrap content provides the view size, equal to the content size.
Match parent allows the view to be the same size, as the Relative or Linear layout.
Ofcourse not this is the not a best approach to give height and width in dps instead wrap_content and match_parent properties.
Documentation
The important thing is if you give width and heights in dp it is not in favour of supporting all different size of screens instead wrap_content and match_parent is much more favourable in adaptive designs and responsive designs where views get the provided space according to the need.
dp is defined as:
dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
Ok, i got two tablets, both got 7" screen and 1024 x 600 resolution, one is using 160dpi and the other 240dpi (probably changed on build.prop). I'm trying use a grid layout but my problem is, the layout showing bigger than my screen on the 240dpi tablet (1,5 times bigger).
What can i do in order to make the both layouts works in the same way? or at least the 240dpi renders entirely inside my screen?![enter image description here][1]
I'm making two lines of buttons on the center of the screen using this code
gl = (GridLayout) findViewById(R.id.GridLayout1);
for(int i = 0;i<11;i++)
{
for(int j =0;j<15;j++)
{
Button b = new Button(this);
b.setWidth(getWindowManager().getDefaultDisplay().getWidth()/15);
b.setHeight(getWindowManager().getDefaultDisplay().getHeight()/11);
if(j== 7 || i==5)
{
b.setBackgroundColor(Color.LTGRAY);
}
else
{
b.setBackgroundColor(Color.WHITE);
}
gl.addView(b);
}
}
But it is showing like this:
http://i.stack.imgur.com/rmVL0.jpg
My layout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="15"
android:orientation="horizontal"
android:rowCount="11" >
The software were made to work in the white one (see picture) by people who worked in my company before me but now I have to make it work on both.
Use relative values for the screen layout, not pixels or dp.
The root cause is that one of your tablets is reporting an incorrect pixel density. The native density of both displays is close to 160 dpi. The 240dpi tablet will appear to Android as if it had a 5" display, and Android will scale the fonts to be the same size.
If that causes screen real estate issues on the 240dpi tablet, do the following:
Create a copy of your layout in one of the following folders:
layout-w1024dp if width is an issue
layout-h600dp if height is the main limiting factor
In the main layout, decrease the font size to work on the 240dpi tablet.
This will reduce the font size to get more content on the screen if the screen size is below a certain minimum. However, fonts may appear tiny on a really small screen (devices with less than 7" which report correct pixel density).
As for relative values: I have done this extensively with linear layouts but not with grid layouts so far, but maybe this helps you get started.
Suppose you have a horizontal LinearLayout (child items arranged in columns) and you want them to maintain the same width relative to each other, taking up the whole screen.
For each child layout, set its layout_width to 0dp and give it a layout_weight.
The layout_weight is fairly arbitrary – it is just the ratio that matters, as the ratio of the layout_weight parameters of the child layouts equals the ratio of their widths. Hence having three child layouts with a layout_weight of 2 vs. 1 vs. 2 will have the same effect as 200 vs. 100 vs. 200.
For the LinearLayout holding all of these, specify a weightSum that is the sum of its children's layout_weight. Set the parent's layout_width to fill_parent.
I have an Image of resolution 1600 x 2400. I want it to be displayed in an ImageView as follow:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside" />
I want the image to:
Take full width
Maintain Aspect ratio
Hence the imageview height must be as per the ratio
Eg: on a 320 x 600 screen, an image of size 1600 x 2400 must be shown as 320 x 480
What I currently get is an imageview with dimensions 320 x 2400 with image centered and space on top and bottom.
save the image in hdmi folder and use
android:src="#drawable/imagename"
or you can use
android:background="#drawable/imagename"
and you can set the heigth and width with
instead of fill parent or wrap or match use sizes in dp like 100dp or whatever you like.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
example
android:layout_width="100dp"
1.Please go through the Developers guide first. Supporting Multiple Screens provides a pretty good overview of what you are trying to achieve. It's a challenging task being honest. Most of the iPhone devs enjoy the fixed screen size but we Android devs have to go through this problem. I can recomend few practices that I'm following
2.Try not to use Fixed height and weight as much as you can
Stick to Linear Layout if the layout is not complex. Go to relative layout only when its really mandatory.
Use Wrap and fill parent than giving fixed sizes..
These are the few I can think of.
3.Typically you just provide different layouts and resources for different screen densities and orientations as described in Android documentation
try using
android:scaleType="fitXY"
or
android:scaleType="fitCenter"
Assuming you have calculated how high your view should be: set it's layout parameters through code.
imageView.setLayoutParams(new LayoutParams(320, height));
This DP measure is pretty confusing, I'm trying to learn when should I use wrap_content and when should I set the height, when using ImageView.
My current issue is that I'm using wrap content on 3 images (and yes I have different resources for each screen size: mdpi,hdpi,xhdpi) BUT in some devices (both hdpi) since they have different width pixel size (480px for one and 590px for the other), in the 480px one, one of the images looks smaller cause their size is calculated cause of the wrap_content.
I could make my designer re-make all the images for the hdpi size, but I want to know when to use wrap_content and when to set DP size myself.
DP is just a unit of measure that normalizes for different screen pixel densities, which means a value like 50dp always has the same physical size no matter what device you run your app on.
As far as actually designing your layouts, you should almost always use either wrap_content or match_parent instead of setting hard numbers for width and height. Exceptions usually come about when you use layout_weight for children of a LinearLayout for doing proportional sizes, or when using the various layout anchors for children of a RelativeLayout.
This is my code for the button. Thing is, it's the same size on all devices. It's big on small ones and small on big ones. How do I alter its size relatively? Here's my code.
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/blue_gradient"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_alignTop="#+id/relativeLayout1"/>
I would like the size to be consistent, depending upon the device's resolution.
EDIT
I want the buttons to be square.
You are specifying the width and height in dp, or density-independent pixels. This unit of measure is designed to produce the same physical size on all devices regardless of pixel density, because pixel density varies by device. You can also use either of the following values for layout_width and layout_height:
wrap_content makes the view large enough to hold its content.
match_parent uses the corresponding dimension (width or height) of its parent view.
Also with any of these you can stil specify padding and margins, a la the typical box model.
Android currently does not support percentage style measurements, like making the width be 60% of the available width. The closest you can get to that is using a LinearLayout and specifying layout_weight values for its child views.
If you want finer control over the size of a view, you can subclass it and override the onMeasure method, which is where you would calculate the size of the view and set its width and height by calling setMeasuredDimension(w, h). Here's an example of someone doing just that: https://stackoverflow.com/a/3147157/1207921
the best way is to use
android:layout_width="wrap_content"
android:layout_height="wrap_content"
It manages size for all devices very well.
For square buttons you need to set width/height in the code. this will help you.
You might want to try using image buttons with different sized images according to screen resolution (Android, (newbie) different res imagebuttons).
If you use LinearLayout with android:weightSum="2.0" and you set all of your image buttons with android:layout_weight="1" you will have their width/height exactly 50% of the LinearLayout according to the LinearLayout orientation.