Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Problem with programmatically button creation

$
0
0

Hi! I am developing my first android app. I got problem, which I failed to resolve. The problem is in position offset. First row is always very strange. Actually first button which I add has wrong positon and wrong size. Please, help to fix it :((

Here is my source code:

// Here is list with names 
String[] names = { "Name1", "Name2", "Name3", "Name4", "Name5" };

// Size calculation
int button_size = WindowManager.DefaultDisplay.Width / 3;
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
LinearLayout.LayoutParams button_param = new LinearLayout.LayoutParams (button_size, button_size);

// I have LinearLayout already, it's basic layout
LinearLayout table = FindViewById<LinearLayout> (Resource.Id.myLayout);     
table.Orientation = Orientation.Vertical;

LinearLayout row = null;
int curCount = 0;
foreach (var n in names) 
{
    if (curCount == 0) 
    {
        row  = new LinearLayout (this);
    }
    Button b = new Button (this);
    b.Text = n;

    row .AddView (b, button_param);
    curCount++;

    // if row is completed, add it to table
    if (curCount == maxCount) 
    {
        table.AddView (row, param);
        curCount = 0;
    }
}
if (curCount > 0)
    table.AddView (l, param);

Viewing all articles
Browse latest Browse all 204402

Trending Articles