Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

ListView rendering on generic emulator M-DPI 5.4in — too much whitespace between two text rows

OsoBOsoB Member ✭✭✭
edited July 2020 in Xamarin.Android

I wondered if others use the Generic Android M-DPI 5.4in emulator and if so, how do you fix the ListView rendering which appears to add an unnecessary blank line between the heading line and detail line of each row.

The other emulators, such as the Pixel 3A XL, are consistent with real devices but they are slow to work with. Is there a way to fix this, while retaining our RowHeight="75", which is perfect for other devices?

        <ListView x:Name="listView" RowHeight="75" ItemSelected="OnListViewItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Text1}" 
                        Detail="{Binding Text2}">
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

First example is the Generic M-DPI 5.4in, second is the Pixel 3A XL.

.

Best Answer

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Different devices have different resolutions.
    We could check it using:

    // Get Metrics
    var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    // Screen density
    var density = mainDisplayInfo.Density;
    

    We will find Generic is 1 and Pixel is larger than this. It means the former device needs more space to display the TextCell control as its built-in height.
    I suggest using a normal label like:

    <DataTemplate>
        <ViewCell>
            <StackLayout>
                <Label Text="{Binding Text1}"/>
                <Label Text="{Binding Text2}"/>
            </StackLayout>
        </ViewCell>
    </DataTemplate>
    

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Different devices have different resolutions.
    We could check it using:

    // Get Metrics
    var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    // Screen density
    var density = mainDisplayInfo.Density;
    

    We will find Generic is 1 and Pixel is larger than this. It means the former device needs more space to display the TextCell control as its built-in height.
    I suggest using a normal label like:

    <DataTemplate>
        <ViewCell>
            <StackLayout>
                <Label Text="{Binding Text1}"/>
                <Label Text="{Binding Text2}"/>
            </StackLayout>
        </ViewCell>
    </DataTemplate>
    
  • OsoBOsoB Member ✭✭✭

    Thanks for the reply @LandLu, it's useful. I see what you mean about using Label within a ViewCell. On the generic emulator there was still a significant space between the two label data lines within each displayed row, when using your suggestion, but I found that I could set the margin of the second label to a negative value and close-up the gap - see below.

                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="{Binding Text1}" Margin="10,0,0,0"/>
                                <Label Text="{Binding Text2}" Margin="10,-10,0,0"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
    

    The only complication with this, which to be fair relates more to our particular application than to Xamarin, is that we found TextCell to work very well with multi-line LF-delimited data and automatic truncation. Our ListView data contains multiple lines separated by LF and we found that TextCell automatically joins the multi-line values and furthermore truncates the trailing-end, whereas adding the truncation to the Label still doesn't achieve such good results, despite trying every single truncation option. But anyway, we can handle this within our data and just do it differently. Thanks again.

Sign In or Register to comment.