Libor
Libor

Reputation: 1

BoxView matrix in Xamarin

I'm currently struggling with my project. The idea is to make few one row matrixes with variable length. In those matrixes I'll need change each boxView's color depends on the input. And apparently I got stuck in the begining of that project with creating matrixes. With following code I am able to make one or more matrixes and move it in the X direction, but no way can it move in Y. What am I doing wrong?

// BoxView dot dimensions.
double boxHeight = 1;
double boxWidth = 0.05;

public BoxView[,] rowPlaces(int count, double x, double y)
{
    BoxView[,] digitBoxViews = new BoxView[count, 1];

    // Create and assemble the BoxViews.
    double xIncrement = 0.035 ;
    double yIncrement = 0.03;

    for (int index = 0; index < count; index++)
    {
        for (int col = 0; col < 1; col++)
        {
            for (int row = 0; row < count; row++)
            {
                // Create the index BoxView and add to layout.
                BoxView boxView = new BoxView();
                digitBoxViews[row, col] = boxView;
                absoluteLayout.Children.Add(boxView,
                                            new Rectangle(x, y, boxWidth, boxHeight),
                                            AbsoluteLayoutFlags.All);
                digitBoxViews[row, col].Color = free;
                y += yIncrement;
            }

            x += xIncrement;
        }
        x += xIncrement;
    }
    return digitBoxViews;
}

In MainPage() I'm just calling this method, as I mentioned there should be like 16 of these matrixes [ whatever x 1] and also 2 columns [1 x whatever].

I'm new in Android and bit confused, I think the mistake is some basic? Maybe wrong layout(currently using absoluteLayout)

To make things more clear here is a picture of approximately layout I want to reach, each boxView should be reachable by name of matrix(vector) and it's index to change color.

Upvotes: 0

Views: 311

Answers (1)

Robbit
Robbit

Reputation: 4358

Here are many layouts you can choose, you can use any layout to achieve your goal, include AbsoluteLayout.

I have do something based on your codes:

Layout:

<AbsoluteLayout 
    x:Name="absoluteLayout"
    BackgroundColor="Yellow">

</AbsoluteLayout>

MainPage:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        rowPlaces(5, 0.2, 0.2);
    }

    public BoxView[,] rowPlaces(int count, double x, double y)
    {
        BoxView[,] digitBoxViews = new BoxView[count, 2];
        double sourcey = y;
        // Create and assemble the BoxViews.
        double xIncrement = 0.25 / count;
        double yIncrement = 0.5 / count;

        for (int index = 0; index < count; index++)
        {
            for (int col = 0; col < 2; col++)
            {
                for (int row = 0; row < count; row++)
                {
                    // Create the index BoxView and add to layout.
                    BoxView boxView = new BoxView();
                    digitBoxViews[row, col] = boxView;
                    absoluteLayout.Children.Add(boxView,
                        new Rectangle(x, y, 10, 10),
                                                AbsoluteLayoutFlags.PositionProportional);
                    digitBoxViews[row, col].Color = Color.Blue;
                    y += yIncrement;
                }

                x += xIncrement;
                y = sourcey;
            }
            x += xIncrement;
        }
        return digitBoxViews;
    }

}

Result :

enter image description here

Upvotes: 1

Related Questions