title: Setting PopupWindow to have a maximum height
body: <p>I inflate a ListView inside a PopupWindow and I want the popup to behave like this:</p>
  
  <ul>
  <li>wrap the listview when its height is &lt; x</li>
  <li>set the popup's height = x when the listiew's height > x (the listview being scrollable)</li>
  </ul>
  
  <p>the popup is showed by a TextWatcher attached to an EditText, as it is meant to show search suggestions.
  The adapter underlying the ListView is managed by a custom Loader, started whenever the user has typed something in the EditText.
  I tried to override <code>onMeasure()</code> on the listview and pass the measured height to a listener which calls <code>PopupWindow.update()</code>, but this creates a loop since the latter would end up calling the first.
  With the following code, the popup wraps the contained ListView as wanted, but the height is not restricted to any value. I need a solution to restrict the height to a maximum value, let's say 300dp.</p>
  
  <pre><code>etFiltro.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
              /...
          }
          @Override
          public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
              Log.i("loader","text changed");
              filtro = String.valueOf(charSequence);
              getSupportLoaderManager().restartLoader(0,null,loaderCallbacks);
              if (popupWindow.isShowing()) popupWindow.dismiss();
          }
          @Override
          public void afterTextChanged(Editable editable) {               
              popupWindow.showAsDropDown(etFiltro);
          }
      });
  
  private LoaderManager.LoaderCallbacks&lt;Cursor&gt; loaderCallbacks = new LoaderManager.LoaderCallbacks&lt;Cursor&gt;() {
  
      MyListView suggestionList;
      SimpleCursorAdapter adapter;
      int mHeight;
  
      @Override
      public Loader&lt;Cursor&gt; onCreateLoader(int i, Bundle bundle) {
          SuggestionLoader loader = new SuggestionLoader(MainActivity.this, databaseConnector, filtro);
          loader.setUpdateThrottle(500);
          View view = ((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_window,null);
          suggestionList = (MyListView) view.findViewById(R.id.suggestionList);
          adapter = new SimpleCursorAdapter(MainActivity.this,android.R.layout.simple_list_item_1,null,
                  new String[]{"note"},new int[]{android.R.id.text1},0);
          suggestionList.setAdapter(adapter);
          suggestionList.setEmptyView(view.findViewById(R.id.tvNoSuggestions));
          //ensure previous popup is dismissed
          if (popupWindow!=null) popupWindow.dismiss();
          popupWindow = new PopupWindow(view,etFiltro.getWidth(),0);
          popupWindow.setWindowLayoutMode(0,WindowManager.LayoutParams.WRAP_CONTENT);
          popupWindow.setAnimationStyle(0);//0 = no animation; -1 = default animation            
          Log.i("loader","onCreateLoader");
          return loader;
      }
  
      @Override
      public void onLoadFinished(Loader&lt;Cursor&gt; cursorLoader, Cursor cursor) {
          adapter.changeCursor(cursor);           
          Log.i("loader","onLoadFinished");
  
      }
  
      @Override
      public void onLoaderReset(Loader&lt;Cursor&gt; cursorLoader) {
          Log.i("loader", "onLoaderReset");
          adapter.changeCursor(null);
      }
  };
  </code></pre>