Previous Button Clicked ID

Hi I'm trying to get the previous clicked ID as like follows :

    public string PrevBtnID = string.Empty;
    public string Prevclickedvalue = string.Empty;
    private void Button_01(object sender, EventArgs e)
    {          
        Button btn = (Button)sender;
        if(Prevclickedvalue==string.Empty)
        {
            Prevclickedvalue = btn.Text;

    **//Here trying to get the ID of the clicked Button. But we are getting a GUID instead of button ID**
            PrevBtnID =Convert.ToString(btn.Id);
        }
        else
        {
            if(Prevclickedvalue==btn.Text)
            {
                btn.IsVisible = false;
                Button btnhide = (Button)this.FindByName(PrevBtnID);
                btn.IsVisible = false;
            }
            else
            {

            }
        }
    }

Thank you.

Answers

  • AmitManchandaAmitManchanda USMember ✭✭

    @Sreekanth3 Yes, you will definitely get GUID as This value is generated at runtime and is not stable across runs of your app. So for this you can set class id and get that class id of the button. Else you cn use text also for the last clicked button.

  • Sreekanth3Sreekanth3 Member ✭✭

    Hi AmitManchanda ,
    If I get the text of the button, How can we hide the button, because we weill be having multiple buttons..
    Is there any way to loop all the buttons of our page and check the text ?

  • jezhjezh Member, Xamarin Team Xamurai

    @Sreekanth3

    You can try the following code:

             var root = Window.DecorView.FindViewById<ViewGroup>(Android.Resource.Id.Content);
            var childViews = GetViewsByType<Button>(root);
    
            foreach (Button button in childViews) {
                if (button.Text.Equals("3")) { // eg: the text of this button is 3
                    button.Text= "it is you...3..";
                    button.Visibility = ViewStates.Invisible;
                }
            }
    

    The method GetViewsByType:

        private IEnumerable<T> GetViewsByType<T>(ViewGroup root) where T : View
        {
            var children = root.ChildCount;
            var views = new List<T>();
            for (var i = 0; i < children; i++)
            {
                var child = root.GetChildAt(i);
                if (child is T myChild)
                    views.Add(myChild);
                else if (child is ViewGroup viewGroup)
                    views.AddRange(GetViewsByType<T>(viewGroup));
            }
            return views;
        }
    
  • Sreekanth3Sreekanth3 Member ✭✭

    @jezh , I tried your solution.
    I'm getting a compile exception as like "window.DecorView does not exist in current context."
    I tried to resolve by name space. But unfortunately I didnt found any namesapce related to that.
    Do you have any idea about that ?

    Thank you.

  • jezhjezh Member, Xamarin Team Xamurai

    @Sreekanth3
    You can define an ID for the root layout.
    For example, if your root layout is a LinearLayout, and the id of it is myroot, just like this:

    <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout 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:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/myroot"
        android:layout_height="match_parent">
    </LinearLayout>
    

    Then, you can use the following code:

       var root = FindViewById<LinearLayout>(Resource.Id.myroot);
       var childViews = GetViewsByType<Button>(root);
            //........
    
Sign In or Register to comment.