Link to home
Start Free TrialLog in
Avatar of tangteng78
tangteng78Flag for Malaysia

asked on

Is there a way to set auto height for StackLayout in Xamarin?

Hi,
I'm having issue of content being "cut" when I'm using StackLayout  (inside of a ScrollView). As my contents are dynamic, some of the contents are long, thus the height of the StackLayout  will be extended. However, the issue can be solved by setting the physical height (2000 in my case).

<ScrollView HorizontalOptions="FillAndExpand">
<StackLayout Padding="5" Spacing="5" VerticalOptions="Start" HeightRequest="2000">

However this solution is not "friendly" as for a short content, it will leave a big gap at the bottom.

If you guys have any workaround or a better idea how to do this, it would be greatly appreciated.

User generated image
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

You may set -1 instead, it will automatically determine its own height.

HeightRequest="-1"

Open in new window

Avatar of tangteng78

ASKER

@David. It doesn't work. By setting the HeightRequest="-1",the StackLayout was "cut" even prematurely, most of the bottom part of the content is cut.


        <ScrollView HorizontalOptions="FillAndExpand">
        <StackLayout Padding="5" Spacing="5" VerticalOptions="Start" HeightRequest="-1">
Noted. Try make the following changes to resolve your white gap matter:
 <StackLayout Padding="5" Spacing="5" VerticalOptions="StartAndExpand" Orientiation="Vertical">

Open in new window

Note: use "FillAndExpand" for vertificaloptions if you have declared multiple stacklayout setting for ongoing content.

If that not resolve, maybe you have complicated/nested stack layout arrangement.
Otherwise, share more code about your existing XAML settings here.  
Hi David,
Here you go my xaml code. I'd also screenshot for you the "cut off" bottom portion (my lblDescription has a long contents)

=============================================================
CODE
=============================================================
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AwesomeApp.Toy"
             NavigationPage.HasNavigationBar="False"
             >
    <ContentPage.Resources>
        <Style TargetType="Frame">
            <Setter Property="HasShadow" Value="True"></Setter>
            <Setter Property="CornerRadius" Value="5"></Setter>
            <Setter Property="BorderColor" Value="LightGray"></Setter>
            <Setter Property="Padding" Value="10"></Setter>
            <Setter Property="Margin" Value="0,5,0,10"></Setter>
        </Style>        
    </ContentPage.Resources>

    <ContentPage.Content>
       
        <ScrollView>

            <StackLayout Padding="5" Spacing="5" HeightRequest="5000" >

                <ActivityIndicator IsRunning="{Binding Path=IsBusy, Mode=OneWay}" IsVisible="{Binding IsBusy}" />
                <!--<Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>-->

                <Image x:Name="imgMainPic" IsVisible="true" Aspect="AspectFit" HorizontalOptions="FillAndExpand"/>


                <Button x:Name="btnStatus" Text="Coming Soon" IsEnabled="false"/>
                <Label x:Name="lblInStock" Text="In Stock" BackgroundColor="#9900CC" TextColor="white" FontAttributes="Bold"
                                        HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label x:Name="lblPreorder" Text="Preorder" BackgroundColor="#32cd32" TextColor="white" FontAttributes="Bold"
                                        HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Soldout" TextColor="#B00000" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Preorder Closed" TextColor="#B00000" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Preorder Soldout" TextColor="#B00000" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Discontinued" TextColor="#DDDDDD" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Coming Soon" TextColor="#666666" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <!--</Grid>-->
                <Label x:Name="lblTitle" Text="" FontSize="Title" FontAttributes="Bold" TextType="Html"/>
                <Label x:Name="lblCompany" Text="" FontSize="Title" FontAttributes="Italic" TextType="Html"/>

                <Frame>
                    <StackLayout>
                        <Label x:Name="lblRemark" Text="" FontSize="Body" IsVisible="false"/>
                        <Label x:Name="lblAllocation" Text="" FontSize="Body" TextColor="Red" IsVisible="false"/>
                        <Label x:Name="lblProductLimit" Text="" FontSize="Body" TextColor="Red" TextTransform="Uppercase" IsVisible="false" />
                    </StackLayout>
                </Frame>

                <Frame x:Name="frmDeposit" IsVisible="false">
                    <StackLayout>
                        <Label x:Name="lblPreorderNotice" Text="" FontSize="Body" TextColor="Red" TextTransform="Uppercase" IsVisible="false" />
                        <Label x:Name="lblDeposit" Text="" FontSize="Body" IsVisible="false"/>
                    </StackLayout>
                </Frame>

                <Frame x:Name="frmPoint" IsVisible="false">
                <Label x:Name="lblPoint" Text="" FontSize="Body" IsVisible="false"/>
                </Frame>
               
                <Label x:Name="lblDescription" Text="" TextType="Html" VerticalOptions="Start" FontSize="Body" />
                <Button x:Name="btnGoBack" Text="Go Back" Clicked="GoBack_Click" />

         </StackLayout>
        </ScrollView>
   
    </ContentPage.Content>
</ContentPage>
=======================
END OF CODE
=======================

The bottom is still cutoff using your latest suggestion.
User generated image

It suppose to be like this (where the last button shown at the bottom).
User generated image
one request, please use code snippet feature to mark code as it helps reading the code.
User generated image 
Hi Patel,
Oh great, didn't know such feature exists. Ok, here you go :) 

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AwesomeApp.Toy"
             NavigationPage.HasNavigationBar="False"
             >
    <ContentPage.Resources>
        <Style TargetType="Frame">
            <Setter Property="HasShadow" Value="True"></Setter>
            <Setter Property="CornerRadius" Value="5"></Setter>
            <Setter Property="BorderColor" Value="LightGray"></Setter>
            <Setter Property="Padding" Value="10"></Setter>
            <Setter Property="Margin" Value="0,5,0,10"></Setter>
        </Style>        
    </ContentPage.Resources>

    <ContentPage.Content>
        
        <ScrollView>

            <StackLayout Padding="5" Spacing="5" HeightRequest="5000" >

                <ActivityIndicator IsRunning="{Binding Path=IsBusy, Mode=OneWay}" IsVisible="{Binding IsBusy}" />
                <!--<Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>-->

                <Image x:Name="imgMainPic" IsVisible="true" Aspect="AspectFit" HorizontalOptions="FillAndExpand"/>


                <Button x:Name="btnStatus" Text="Coming Soon" IsEnabled="false"/>
                <Label x:Name="lblInStock" Text="In Stock" BackgroundColor="#9900CC" TextColor="white" FontAttributes="Bold"
                                        HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label x:Name="lblPreorder" Text="Preorder" BackgroundColor="#32cd32" TextColor="white" FontAttributes="Bold"
                                        HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Soldout" TextColor="#B00000" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Preorder Closed" TextColor="#B00000" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Preorder Soldout" TextColor="#B00000" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Discontinued" TextColor="#DDDDDD" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <Label Text="Coming Soon" TextColor="#666666" FontAttributes="None"
                                        HorizontalTextAlignment="Start" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center"
                                        Grid.Column="0" Grid.Row="3" IsVisible="false"/>

                <!--</Grid>-->
                <Label x:Name="lblTitle" Text="" FontSize="Title" FontAttributes="Bold" TextType="Html"/>
                <Label x:Name="lblCompany" Text="" FontSize="Title" FontAttributes="Italic" TextType="Html"/>

                <Frame>
                    <StackLayout>
                        <Label x:Name="lblRemark" Text="" FontSize="Body" IsVisible="false"/>
                        <Label x:Name="lblAllocation" Text="" FontSize="Body" TextColor="Red" IsVisible="false"/>
                        <Label x:Name="lblProductLimit" Text="" FontSize="Body" TextColor="Red" TextTransform="Uppercase" IsVisible="false" />
                    </StackLayout>
                </Frame>

                <Frame x:Name="frmDeposit" IsVisible="false">
                    <StackLayout>
                        <Label x:Name="lblPreorderNotice" Text="" FontSize="Body" TextColor="Red" TextTransform="Uppercase" IsVisible="false" />
                        <Label x:Name="lblDeposit" Text="" FontSize="Body" IsVisible="false"/>
                    </StackLayout>
                </Frame>

                <Frame x:Name="frmPoint" IsVisible="false">
                <Label x:Name="lblPoint" Text="" FontSize="Body" IsVisible="false"/>
                </Frame>
                
                <Label x:Name="lblDescription" Text="" TextType="Html" VerticalOptions="Start" FontSize="Body" />
                <Button x:Name="btnGoBack" Text="Go Back" Clicked="GoBack_Click" />

         </StackLayout>
        </ScrollView>
    
    </ContentPage.Content>
</ContentPage>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial