Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:24:31+00:00 2026-05-31T17:24:31+00:00

I’m absolute beginner when it comes to mono for android . I’ve used following

  • 0

I’m absolute beginner when it comes to mono for android.

I’ve used following code snippet to create 50 buttons programmatically:

for(int i=0;i<50;i++)
            {
            //code to calculate x and y position
                btn=new Button(this);
                //btn.SetBackgroundColor(Android.Resource.Color.);
                btn.SetTextSize(Android.Util.ComplexUnitType.Sp,8);
                btn.Text="Scrip "+i+"\n"+"CMP "+i+"\n"+"%Chg "+i;
                lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams((width+30)/5, (height-10)/10));
                btn.LayoutParameters=lp;
                lp.SetMargins(leftMargin,topMargin, 0, 0);
                main.AddView(btn);
            }
            
            String str="";
            btn.Click += (sender, e) => 
            {
                str=btn.Text;
                Toast.MakeText(this, "Selected="+str,ToastLength.Short).Show();
                Console.WriteLine("Selected="+str);
            };

But one big problem with this code is at the end of the loop, btn object has reference of the last button only.

So if any button other than last button is pressed, button click event is not fired.
How to resolve it?
Ideally, it should return the text of the clicked button.

Also, referring to below screenshot, the default button style doesn’t look good here. So I want to change it to exact rectangle and not rounded-rectangel(the default one).
Any idea on this?

As I’m very new to this, any help will be appreciated !!

EDIT

Asa result of your help, I’m able to create and refer all buttons appropriately.

But how to set their style as exact rectangle??

screen

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:24:33+00:00Added an answer on May 31, 2026 at 5:24 pm

    Move btn.Click += (sender, e) subscription inside for loop.

    Even better – create one named method instead of creating many anonymous. E.g. Button_Click and subscribe to it:

    btn = new Button(this);
    btn.Click = Button_Click;
    

    Inside that method you can cast sender object to Button and know which button was clicked.

    UPDATE: here is complete code

    const int rowsCount = 10;
    const int columnsCount = 5;
    int buttonsCount = rowsCount * columnsCount;
    
    for (int i = 0; i < buttonsCount; i++)            
        AddButton();
    

    I prefer not to use magic numbers in code 🙂

    private void AddButton()
    {
        Button button = new Button(this);
        button.Click += Button_Click;
        // add text and other properties
        main.AddView(button);
    }
    
    private void Button_Click(object sender, EventArgs e)
    {
        Button button = (sender as Button);
        // use clicked button e.g. Console.WriteLine("Selected = {0}", button.Text);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.