File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Can I set size for elements in layout with percent (%)?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Can I set size for elements in layout with percent (%)??" Watch "Can I set size for elements in layout with percent (%)??" New topic
Author

Can I set size for elements in layout with percent (%)??

Farshad Zeinali
Greenhorn

Joined: Apr 23, 2013
Posts: 17
Hi dears


Can I set size for elements in layout with percent (%)??
thanks
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Farshad Zeinali wrote:Hi dears


Can I set size for elements in layout with percent (%)??
thanks


No you can't. The closest you get (and what is recommended by Google) is to use DIPs (or dps). DIPs are Density Independent Pixels - by using them you ensure the UI elements are the same physical size on all screens, regardless of the number of pixels on the screen. For reference, see this page.

The problem I see with using percent is the definition of size. You could have two 4" phones, one 640 pixels wide, and the other 960 pixels wide. Alternatively you could have two device with 640 pixels wide, one could be 4", the second one 2.6". So when you are talking about percent of the size, are you saying you want the UI element to be relative to the number of pixels (on two phones of the same physical size but different number of pixels, make it look smaller on the phone with fewer pixels, on phones of different physical sizes but with the same number of pixels, make it look the same) or relative to the physical size (on phones with the same number of pixels but different physical screen sizes, make it look smaller on the phone with the smaller screen dimension, on devices with the same screen dimensions but different number of pixels, the element should be the same size).

If you want the first option, then you could choose a layout which takes weights (such as LinearLayout) and set its width or hieght to fill_ or match_parent. Set the width or height for each child View to 0dp, then set the weight to some relative value. The size of the layout gets broken down into even units, then distributed to the children according to their weight, with higher weights getting more 'units' of the layout space. For example, let's say I have a horizontal LinearLayout like below:


If I gave it three children Views with weights of 1,2,1 respectively, then the LinearLayout would be split into 4 equal parts. One part would be given to the the first View, two parts to the second view, and one part to the third view. It might look like this:

The XML might look something like this:



If you wanted the size to be relative to the physical size of the screen, then I think the best way to do that would be have different layouts for different screen sizes - see the link I posted above on how to do this.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I set size for elements in layout with percent (%)??