Accessing the InputMethodManager from RecyclerView.Adapter

Nathan Sokalski 4,111 Reputation points
2021-03-27T20:07:52.947+00:00

I have a RecyclerView.Adapter that contains methods in which I need to use the InputMethodManager. In places such as the MainActivity I could access it by using

((InputMethodManager)GetSystemService(InputMethodService))

However, I am unable to do this in a RecyclerView.Adapter (at least I don't know exactly how). How can I access the InputMethodManager from a RecyclerView.Adapter? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-03-29T08:53:52.28+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The normal solution is to create a global static variable in MainActivity , assign the value in OnCreate , and access it from RecyclerView.Adapter .

    MainActivity

       public static MainActivity Instance;  
        protected override void OnCreate(Bundle savedInstanceState)  
        {  
             Instance = this;  
         
             global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
             LoadApplication(new App());  
        }  
    

    Adapter

       //somewhere   
         
       ((InputMethodManager)MainActivity.Instance.GetSystemService(InputMethodService));  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.