Published May 29, 2017 by with 18 comments

View Sizing In Xamarin Forms


Let's start with the view side of the layout negotiation and look at how a view tells a panel its preferred size and position in all three platforms .



These tend to size themselves just large enough to fit around their content
For example, label and button both typically size themselves just large enough to show their text.
In this case, the label shown here that says hello.


There are many other factors that influence the final size both settings on the view and the layout algorithm. We will see all the detail shortly. Four properties influence the views rendered size. There are all requests and may be overridden by the layout container. The first two are for sizing you can set a width and a height value on the view. Notice that word requests in the property name you use these properties to tell the layout panel its preference but the layout makes the final sizing decision.



The other two properties give positioning information vertical options and horizontal options influence how the view is positioned within its rectangle allocated by the layout container.
You can specify you want to be aligned to one of the four edges of the rectangle. or that you want to occupy the entire rectangle .

 

Notice when we set width request and height request we use literal values like one hundred. In xamarin forms, these values have no unit. They are not points or pixels. They are just values of type double.


Xamarin Forms passes these values on to the underlying operating system.
The operating system provides the context needed to decide what these numbers mean.

   

On Windows, these values are interpreted as effective.
                                  
 On Android there density-independent pixels  
                                 
And on iOS the values are called points
                     
           

Now let's look at how the translation from literal values to concrete units plays out. Your xamarin forms app will likely run on multiple operating systems and on a wide range of devices.
 The width request and height request value set near xamarin source code are interpreted at runtime. According to the rules of the operating system based on the screen size and resolution.
The typical behavior for each OS is to apply a scale factor to your values to determine the number of physical pixels for review



For example if this app will run on the Windows Device shown the one hundred label by three hundred label .would render as 200 by 600 pixels since the devices are the scale factor of two .
On an iPhone 6s we would again see two hundred by six hundred physical pixels.
 Since it also has a scale factor of two. The screen of the Nexus has a higher pixel density so the scale factor is three point five resulting in a render size of 350 by 10520 pixels



 Those values like width requests and height request properties might not be respected at runtime. They cannot be used to see the result of your layout algorithm. for example if you set width request to 100 and its value will still be  100 after the layout algorithm runs regardless of whether the view is actually given units by the layout panel .
Instead, several other read-only properties tell you the actual size of a view in platform-independent unit’s .x and y give you the distances from the upper left corner of the layout

 Width the height tell you the size

And bounds as a convenience property that returns a rectangle containing all four values X, Y width and height.

 These properties are updated every time the layout algorithm runs.
Remember that the layout container gives each child view a rectangle. This rectangle is the area where the child will be rendered with these horizontal options and vertical options control the placement of the child within this rectangle.

 It’s actually a bit trickier than that simple statement the layout options struct has two properties.
 One used by all layouts and one which is only used for stack layout. Let’s look at the details next. The layout option struts two properties are both related to layout but they're not related to each other. Layout options is complex despite only having two properties. Alignment controls how the child view is positioned within the rectangle given to it by its layout container. Remember for each view it has two layout options values. One for horizontal and one for vertical. These two values let you align the child due to any of the edges of its rectangle or to Center, fill is different than the other values it means a child should fill its entire rectangle. Again each of us two of these values so you can independently control whether the child fills a rectangle in just one direction or both.


The expands property lets the view request extra space if the parent stack layout has any available. Expands is only used by stack layout the value is completely ignored by the other layout container’s. We will explore  expands more when we talk stack layout.


 The layout alignment Enums has four values start Center end , fill. When you choose one of these options for either the horizontal or vertical directions two things happen . The width requests or height requests setting is used to determine the view size in that direction if the view doesn't have a sizes default size is used for that access. The view has been aligned to the side of the layout rectangle you specify .in this case the fact layout gives each label or layout rectangle spanning the entire width start Center and end position within that rectangle. Fill however ignore any width request or height request setting and instead size of view so it occupies the entire rectangle in that direction.

 A view can have more than one property setting that influences its size for example it can have on width request of 100 and horizontal options of fill.  In these cases the two settings might conflict suppose a layout panel at 300 units wide the panels are not simultaneously make the view units 100 wide and have it filled a layout width. The fill options will take precedence in this case and the width request is ignored.

This can be confusing since fill is also the default .

Let’s look at that next. Horizontal options and vertical options in both default to fill making these two declarations identical. It’s important to keep in mind this when debugging a UI. It’s easy to forget and it can make it very hard to figure out why your view renders the way it did.

You may want a bit of extra space around a view. For example suppose you have some buttons next to each other you may want to add room around them to avoid accidental touches this is exactly what the margin property is for. Now what you include extra space or in any view. Margin is of type thickness so you have several options on how you would like to specify the value. the most flexible option is to give individual values for all four sides the order is top left right and bottom. Another option is to give two values the first is used on the left and right side the second is use for the top and bottom. Finally you can give a single value used for all four sides.

 There’s another option to add some extra space but this one's only available to layout panels. The padding property has space inside a layout panel. It reduces the area available to layout children. Padding is also a thickness type so you have the exact same options for setting it that we saw with margin.


18 comments:

  1. I know this is an old article, but this is still wrong: the order is top left right and bottom. Your image contradicts it. It's left, top, right,bottom

    ReplyDelete
    Replies
    1. It depends if you using commas or not, for example "10, 5, 10, 5" will be left, top, right, bottom (like author using), while "10 5 10 5" will be top, right, bottom, left (same order, but starting from top). It's a bit strange and easy to make mistake, but that's how it is. I guess it's some kind of bug that no one is reporting and probably it's too late to report, bc if they change it it will mess up existing applications.

      Delete
  2. I don't know much about it as a fresher this blog helped me a lot and may help Xamarin.Forms 4.0 developers .

    ReplyDelete
  3. Nice blog. Thanks for sharing such great information.Develop xamarin application Xamarin services in India , Hire xamarin developer

    ReplyDelete
  4. Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks. 웹툰보기

    ReplyDelete
  5. This was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself..
    taxi app development company

    ReplyDelete
  6. Wow, that's a lot of information; come back to see what else we have to offer on our website. Xamarin Development Company

    ReplyDelete
  7. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging..
    build taxi app

    ReplyDelete



  8. sublime text license key Click on Help > Enter License. In the License window, copy and paste one of the keys below (Check the version you have) ...
    ---------------------------------------
    Advanced SystemCare Ultimate 15 Key
    ---------------------------------------

    wps office crack WPS Office Premium Crack is a software that comes with intuitive tools for viewing, creating, editing, and organizing ...
    ---------------------------------------
    Advanced SystemCare Pro 15 Key

    ReplyDelete
  9. I enjoyed your article, I am also writing the article on gaming software if you are interesting in gaming then you can definitely visit our website Emulator PC

    ReplyDelete
  10. Thanks for the article
    Gamzen India is the construction equipment manufacturer in India. Company holds rich experience in manufacturing skid steer loader, skid steer attachments ,batching plant ,piling tools and many more. Gamzen India Is One Of The Best Construction Equipment Industry Which Serves Its Best To The Construction World In Both Domestic And International Market.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. As a reputable digital marketing company in Jaipur , we understand the unique challenges and opportunities that local businesses face. We have a deep understanding of the Jaipur market and can tailor our strategies to suit your specific goals and target audience. Whether you are a startup or an established brand, we have the right solutions for you.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Fantastic post! The content is truly engaging and filled with valuable insights. I can definitely relate to the points you've made. Well done!

    The Cornea Impression Blogger

    Car News and Reviews Blog

    All Watch News and Review Blogs

    Bike News and Review Blog

    Aircraft News and Reviews Blog

    ReplyDelete