0

I'm new to Xamarin worked with android before and now I'm trying to make a board class but can't seem to create buttons dynamically.

I'm trying to generate buttons and for some reason right now I meet this problem while trying to dynamically make buttons... hopefully you could help me :)

My current problem

in activity:

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;

namespace App2
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource

            Board board = new Board(this);
            SetContentView(Resource.Layout.activity_main);

        }
    }
    class Board
    {
        RelativeLayout layout;
        Context context;
        const int length = 8;
        Button[,] buttons;


        public Board(Context context)
        {
            layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
            this.context = context;
            Init();
        }
        private void Init()
        {
            //Initializing the board
            buttons = new Button[length, length];
            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    buttons[i, j] = new Button(context);
                    buttons[i, j].Text = (i + j).ToString();
                    buttons[i, j].SetX(50 * i);
                    buttons[i, j].SetY(20 * j);
                    layout.AddView(buttons[i, j]);

                }
            }

        }
    }

}

in axml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
1
  • does it work now ? Commented Mar 22, 2019 at 9:16

1 Answer 1

1

My current problem

in activity:

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;

namespace App2
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource

            Board board = new Board(this);
            SetContentView(Resource.Layout.activity_main);

        }
    }
    class Board
    {
        RelativeLayout layout;
        Context context;
        const int length = 8;
        Button[,] buttons;


        public Board(Context context)
        {
            layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
            this.context = context;
            Init();
        }
        private void Init()
        {
            //Initializing the board
            buttons = new Button[length, length];
            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    buttons[i, j] = new Button(context);
                    buttons[i, j].Text = (i + j).ToString();
                    buttons[i, j].SetX(50 * i);
                    buttons[i, j].SetY(20 * j);
                    layout.AddView(buttons[i, j]);

                }
            }

        }
    }

}

in axml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>

ps:i run it,it works,but buttons are overlapping and there may be problems in calculating the position of Buttons in your code.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank yo very much sir! have a nice day
Sure! I've just got in another problem.. when I run it , it tells me when: layout.AddView(button[i,j]); that the button is not an instance but I declared as a new button a few lines back
@EyalAbramovitch what's the problem ,have some error message ? I don't quite understand what you're describing, and layout.AddView(buttons[i,j]); spelling mistakes
could you update your code above and let me have a look
did you miss "this.context = context" in construct method
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.