question

njsokalski avatar image
0 Votes"
njsokalski asked njsokalski commented

Accessing the InputMethodManager from RecyclerView.Adapter

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.

dotnet-android
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered njsokalski commented

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.



· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is your code for Xamarin.Android? I would be hesitant to use code that includes "Xamarin.Forms.Forms" (line 6) in a Xamarin.Android app. My question is for a Xamarin.Android app, not the Xamarin.Android component of a Xamarin.Forms app.

0 Votes 0 ·

The solution is identical in Xamarin.Android.

public static MainActivity Instance;
 protected override void OnCreate(Bundle savedInstanceState)
 {
      Instance = this;
 }
0 Votes 0 ·

I have the following in my Adapter:

 ((InputMethodManager)((MainActivity)Xamarin.Essentials.Platform.CurrentActivity).GetSystemService(InputMethodService)).RestartInput(txtTrackerName);

However, InputMethodService gives the following error:

"'InputMethodService' is a type, which is not valid in the given context"

Is my code using a different GetSystemService than what it should be? Is there something else I need to add?

0 Votes 0 ·
Show more comments