0

There is a class inheritor from the handler through which I control the graphic button. To search for this button, I pass in the designer a link to the activation. But I want to pass the view there. How can I get a view from activity?

public class FlActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_fl);

        toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);

        tabLayout = FindViewById<TabLayout>(Resource.Id.tabLayout);
        viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
        fpAdapter = new FpAdapter(SupportFragmentManager, null);

        uiHandler = new UiHandler(this, MainLooper);

    }
}
public class UiHandler : Handler
{
    private Activity activity { get; }
    public UiHandler(Activity a, Looper loader) : base(loader) 
    {
        activity = a;
    }
    public override void HandleMessage(Message msg)
    {
         activity.FindViewById<ImageButton>(Resource.Id.imageButton1).SetImageResource(msg.Data.GetInt("ID", Resource.Drawable.bt_off));
    }
}

If I change private Activity activity { get; } to private View view { get; }, how can I transfer the view from the main activity when creating a handler instance. What to replace this in creating a nadler object?

MaxNTF
  • 33
  • 1
  • 7

1 Answers1

2

There are quite several ways to do this :

this.Window.DecorView.RootView;

or

this.Window.DecorView.FindViewById(Android.Resource.Id.Content);

or

this.FindViewById(Android.Resource.Id.Content);

or

this.FindViewById(Android.Resource.Id.Content).RootView;

or

((ViewGroup) this.FindViewById(Android.Resource.Id.Content)).GetChildAt(0);

Although, since it's probably a layout, not a single view, I recommend using ViewGroup instead of View (these methods returns View so you have to cast them to ViewGroup if you want)

=================

Credit to this answer

XamarinDev
  • 66
  • 7
  • Thanks it helped. I need a view, because I want to change in the handler elements of other views from this class, for example, a dialog box. – MaxNTF Jan 25 '20 at 09:24