title: Hide soft keyboard on app resume
body: <p>I have an Android app, written in c# using Xamarin.  I have reduced the app to a LinearLayout containing a TextView and a Google admod AdView for a banner ad.</p>
  
  <p>I don't want the soft keyboard to appear in the app - it's not required.  If I start the app, the TextView and the banner add appear.  If I then switch to another app, then switch back to the first app, the soft keyboard pops up as soon as the first app is resumed.  This only happens if the AdView is present.  If I remove the AdView from the app, the soft keyboard does not pop up.</p>
  
  <p>The keyboard does not pup up when the app is first started and the AdView is present - only if the app is paused and resumed.</p>
  
  <p>I have the following OnResume override:</p>
  
  <pre><code>protected override void OnResume()
  {
      base.OnResume();
      InputMethodManager im = (InputMethodManager)GetSystemService(Context.InputMethodService);
      im.HideSoftInputFromWindow(Window.DecorView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 
  }                                  
  </code></pre>
  
  <p>but the keyboard still pops up on resume if I include the AdView.</p>
  
  <p>I tried hiding the keyboard in OnResume for the AdView specifically:</p>
  
  <pre><code>im.HideSoftInputFromWindow(adView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
  </code></pre>
  
  <p>but that didn't help either.</p>
  
  <p>I have tried every suggestion I could find in many, many other stackoverflow questions.  I am able to prevent the keyboard popping up in every circumstance except when the app is resumed when the AdView is present.</p>
  
  <p>I'm stumped.  Any ideas?</p>
  
  <p>More details....</p>
  
  <p>First, a correction.  I have an EditText on the LinearLayout, not a TextView.  I don't want the soft keyboard, but I do want the EditText to be editable (in the full version of the app I provide buttons to add/delete/change text in the EditText).</p>
  
  <p>I have reduced the app to a single LinearLayour containing an EditText and an AdView.  If I load an ad to the AdView and run the app, switch to another app, then switch back, the soft keyboard pops up.  I don't want that.  If, however, I don't load an ad to the AdView (but still have the AdView in the axml file), then run and switch etc., no soft keyboard pops up.</p>
  
  <p>Source is as follows:</p>
  
  <p>AndroidMainifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App1.App1" android:versionCode="1" android:versionName="1.0"&gt;
      &lt;uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;application android:label="App1"&gt;
          &lt;meta-data android:name="com.google.android.gms.version" 
                 android:value="@integer/google_play_services_version" /&gt;
          &lt;activity android:name="com.google.android.gms.ads.AdActivity" 
                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
                android:theme="@android:style/Theme.Translucent" 
                android:windowSoftInputMode="stateAlwaysHidden" /&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Main.axml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/layout"&gt;
  
    &lt;EditText xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText" /&gt;
  
    &lt;com.google.android.gms.ads.AdView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/adView"
      ads:adSize="SMART_BANNER"
      ads:adUnitId="ca-app-pub-3940256099942544/6300978111"&gt;
    &lt;/com.google.android.gms.ads.AdView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>MainActivity.cs:</p>
  
  <pre><code>using Android.App;
  using Android.Content;
  using Android.Views;
  using Android.Views.InputMethods;
  using Android.OS;
  using Android.Widget;
  using Android.Gms.Ads;
  
  namespace App1
  {
      [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
      public class MainActivity : Activity
      {
          protected override void OnCreate(Bundle bundle)
          {
              base.OnCreate(bundle);
  
              SetContentView(Resource.Layout.Main);
  
              AdView adView = FindViewById&lt;AdView&gt;(Resource.Id.adView);
              var requestbuilder = new AdRequest.Builder();
              adView.LoadAd(requestbuilder.Build());
          }
  
  
          protected override void OnResume()
          {
              base.OnResume();
  
              InputMethodManager im = (InputMethodManager)GetSystemService(Context.InputMethodService);
  
              im.HideSoftInputFromWindow(Window.DecorView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
  
              LinearLayout layout = FindViewById&lt;LinearLayout&gt;(Resource.Id.layout);
              im.HideSoftInputFromWindow(layout.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
  
              EditText editText = FindViewById&lt;EditText&gt;(Resource.Id.editText);
              im.HideSoftInputFromWindow(editText.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
  
              AdView adView = FindViewById&lt;AdView&gt;(Resource.Id.adView);
              im.HideSoftInputFromWindow(adView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
  
          }
      }
  }
  </code></pre>
  
  <p>The code as shown above demonstrates my problem.  Deploy the app to a phone, run the app, switch to another app, then switch back again, and the soft keyboard pops up on resuming the first app.  But... comment out the following line of code in MainActivity.cs:</p>
  
  <pre><code>adView.LoadAd(requestbuilder.Build());
  </code></pre>
  
  <p>and repeat the deploy etc., and the soft keyboard doesn't pop up.</p>
  
  <p>As I said, I'm stumped.  I tried adding a handler for focuschange to the EditText to pop the keyboard down, but as far as I can tell that's only called when the EditText loses focus.</p>