0

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?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • possible duplicate of [What does android:layout\_weight mean?](http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean) – Code-Apprentice Jul 25 '13 at 16:33

6 Answers6

2

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.

FD_
  • 12,947
  • 4
  • 35
  • 62
1

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.

user2483079
  • 533
  • 2
  • 10
1

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).

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
1

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

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).

DieBagger
  • 11
  • 1
1

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%).

yugidroid
  • 6,640
  • 2
  • 30
  • 45