Share via


Programatically call a button's Click method

Question

Sunday, June 2, 2013 4:49 PM

So in one of my Activity's OnCreate method, I assign a Click delegate to a button. I immediately afterward wish for that method to execute, but calling the button's PerformClick() function doesn't seem to be working. Should I be calling the PerformClick function in a method other than OnCreate?

Any insight would be greatly appreciated, thank you all for your time.

-mwerdekker

All replies (11)

Sunday, June 2, 2013 5:31 PM

If you have the need for calling something that you atm handle with a lambda, change the lambda to a function and call that function.

If you add an event to click ( or any other event ) press [tab] twice after the click += and the IDE will crate the function for you.


Sunday, June 2, 2013 7:16 PM

You could do somethink like this?

button.Click += HandleClick;
DoSomething();

void HandleClick (object sender, EventArgs e)
{
    DoSomething ();
}

private void DoSomething()
{
    //Click code here.
}

Wednesday, October 28, 2015 11:35 AM

Hello @rmacias. I just wonder why my button click event doesnt come automatically Button btngotoact1 = FindViewById (Resource.Id.btngotoact1);

btngotoact1.Click+= [tab tab tab...] (juts button name and nothing more, no any void click)

what do I miss here? I use Xamarin Studio


Wednesday, October 28, 2015 12:41 PM

@SamirSamedov,

Do you have any other controls above button ?

I think you simply try by having a breakpoint on that button click event and debug your app !


Wednesday, October 28, 2015 12:52 PM

Hello @Yksh.Leo I always write it manual, my autocomplete doesnt work on click event dont know why

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Views.InputMethods;

namespace ActivityWorkLogic
{
    [Activity (Label = "ActivityOne")]          
    public class ActivityOne : Activity
    {
        Button btn_actone_back_main;
        Button btn_actOne_other;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.ActOne);

            btn_actone_back_main=FindViewById<Button> (Resource.Id.btn_actone_back_main);
            btn_actOne_other=FindViewById<Button> (Resource.Id.btn_actOne_other);
            Button btnTry=  FindViewById<Button> (Resource.Id.btnTry);

                             // btnTry.Click += after that "tab" doesnt bring me the "btnTry_Click"
                    btnTry.Click += btnTry_Click;


            btn_actone_back_main.Click += delegate {
                StartActivity(typeof(MainActivity));
            };

        }

    // created manual
        void btnTry_Click(object sender , EventArgs e)
        {
            Intent intent = new Intent (this,typeof(MainActivity));
            this.StartActivity (intent);
            this.Finish ();
        }



    }
}

Wednesday, October 28, 2015 1:46 PM

@SamirSamedov,

This code snippet work for me,

    using System;

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

    namespace ActivityWorkLogic
    {
        [Activity (Label = "ActivityWorkLogic", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {


            Button btnTry;
            Button btn_actone_back_main;
            protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);

                SetContentView (Resource.Layout.Main);

                btnTry = FindViewById<Button> (Resource.Id.btnTry);
                btn_actone_back_main = FindViewById<Button> (Resource.Id.btn_actone_back_main);

                btnTry.Click += btnTry_Click;

                btn_actone_back_main.Click += delegate 
                {
                    Toast.MakeText (this, "Click from btn_actone_back_main !", ToastLength.Long).Show();
                };
            }

            void btnTry_Click (object sender,EventArgs args)
            {
                Toast.MakeText (this, "Click from btnTry !", ToastLength.Long).Show ();
            }
        }
    }

Just make a try 😊


Wednesday, October 28, 2015 1:48 PM

dear @Yksh.Leo code is working, just need to be fast that is why I need autocomplete of button.Click


Wednesday, October 28, 2015 1:59 PM

@SamirSamedov,

Go through these links :


Wednesday, October 28, 2015 2:08 PM

Try this for Visual Studio

  • Launch Visual Studio 2012
  • Open a solution with .AXML file in it
  • Now XML main menu should be visible on top
  • Open Schemas menu
  • Sort by File Name column and see if android-layout-xml and schemas.android.com.apk.res.android are there. If found, skip to step 13
  • If not found, go to Program Files if 32-bit system or Program Files (x86) if 64-bit system.
  • Then go to \MSBuild\Novell or \MSBuild\Xamarin\Android
  • You should be able to see 2 files android-layout-xml.xsd and schemas.android.com.apk.res.android.xsd
  • Copy these 2 files to \Microsoft Visual Studio 11.0\Xml\Schemas
  • Again open the Schemasmenu as mentioned in steps 3&4
  • Now click Add button and add these 2 files which should be located as mentioned in step 7 & 8
  • Now restart visual studio and the XML editor for design layout should work fine with Intellisense
  • From step 5….
  • If above steps don’t work, then close the Visual Studio 2012.
  • Now open Developer command prompt for VS2012 under Windows programs menu.
  • Type Devenv /ResetSettings without quotes.
  • Launch Visual Studio 2012 and see if Intellisense works.

Wednesday, October 18, 2017 9:53 AM

@YkshLeo , hi. I would like to ask about this issue I am having. Say I have a button created in my xaml page. When clicked, it will trigger an event in the xaml.cs. I've got it working though, however, now I want this button being clicked automatically as the page loads. How can I possibly do this?


Thursday, October 19, 2017 6:32 AM

@"jaYCee.1750" Create a method containing logic to be executed on button click, You can use this method in button click event and also inside onCreate(Bundle savedInstanceState) to execute the logic on page create