Forum General

How Do I change fontsize of a listview item in a .cs file?

HakiJDHakiJD Member ✭✭

This is my code in my Edit.cs:

        BackgroundImageSource = "blue_gradient1";
            this.Title = "Edit Text";

            var db = new SQLiteConnection(_dbPath);

            StackLayout stackLayout = new StackLayout();

            _listView = new ListView();
            _listView.ItemsSource = db.Table<SpeechRecTable>().OrderBy(x => x.Text).ToList();
            _listView.ItemSelected += _listView_ItemSelected;
            //_listView.SeparatorColor = Color.WhiteSmoke;
            stackLayout.Children.Add(_listView);

            _button = new Button();
            _button.Text = "UPDATE";
            _button.BackgroundColor = Color.Coral;
            _button.TextColor = Color.WhiteSmoke;
            _button.Clicked += _button_Clicked;
            stackLayout.Children.Add(_button);

            Content = stackLayout;

I am a beginner in xamarin and I am trying to create a CRUD app. I am following a youtube tutorial that only uses .cs instead of .xaml.cs as its UI.
I cant seem to customize how my listview looks, please help.

Tagged:

Answers

  • RichyRichy Member ✭✭

    Please go with XAML, it make designing easier than ever.

    For a setting the font size in the label of listview you need to assign a template to listview in your .cs file

    listView.ItemTemplate = new DataTemplate(typeof(CustomCell));

    then create the 'CustomCell'

    `public class CustomCell : ViewCell
    {
    public CustomCell()
    {
    var label1 = new Label
    {
    Text = "Label 1",
    FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
    FontAttributes = FontAttributes.Bold
    };

            label1.SetBinding(Label.TextProperty, new Binding("."));
    
            View = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.StartAndExpand,
                Padding = new Thickness(15, 5, 5, 15),
                Children = {label1}
            };
        }
    }`
    

    Please refer https://github.com/xamarin/xamarin-forms-samples/tree/master/WorkingWithListview/WorkingWithListview/Custom for more details

Sign In or Register to comment.