title: Changing view on Menu Item in Action Bar
body: <p>I'm attempting to implement a refresh button in my app to allow a user to manually re-sync to a web server.  The code works, but I'm having trouble figuring out the action views (at least, I think that's what I'm supposed to be using).</p>
  
  <p>My menu item is here:</p>
  
  <pre><code>&lt;item
      android:id="@+id/main_menu_refresh"
      android:enabled="true"
      android:icon="@drawable/refresh"
      android:orderInCategory="1"
      android:showAsAction="ifRoom"
      android:title="@string/refresh" 
      android:actionViewClass="android.widget.ProgressBar"&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p>The problem is, it always shows the ProgressBar.  I wondered if it worked like the search widget (the only example I really see online) and added the <code>collapseActionView</code> tag to the <code>showAsAction</code> and that prevented it from showing up immediately.  However, when I click the refresh button, the icon disappears (good), but so does the Title in the action bar, and the ProgressBar appears on the left side of the window where the title used to be.  Also not what I wanted.</p>
  
  <p>As something of a last ditch effort, I attempted to add this to my code, and remove the <code>actionViewClass</code> from the XML:</p>
  
  <pre><code>MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
  Log.w("MyApp", "Have Menu");
  ProgressBar pb = new ProgressBar(ReadingList.this);
  refresh.setActionView(pb);
  </code></pre>
  
  <p>That didn't work either, giving me a null pointer error on <code>setActionView</code>.</p>
  
  <p>I need a solution that I can call from any function (there is an auto-sync period at the beginning I would like the ProgressBar to display during as well), and be able to return it to it's static icon after.</p>
  
  <p>I tried reading through <a href="https://stackoverflow.com/questions/8198253/progressbar-in-an-actionbar-like-gmail-app-with-refresh">this question</a>, but I am having trouble understanding what the answer means.  I feel like I was trying to do just what it says, but I guess not.  Any assistance is much appreciated.</p>
  
  <p>Edit: For sastraxi's suggestion.</p>
  
  <pre><code>public class IconSwitcher extends LinearLayout{
  
  
  
  public IconSwitcher(Context context) {
      super(context);
  
      ProgressBar pb = new ProgressBar(context);
      ImageView iv = new ImageView(context);
  
      addView(iv);
      addView(pb);
  }
  }
  </code></pre>
  
  <p>This is my class thus far.  However, when I try and reference it with:</p>
  
  <pre><code>MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
  IconSwitcher ic = (IconSwitcher) refresh.getActionView();
  </code></pre>
  
  <p>I get a null pointer error.  on creating the IconSwitcher.  The button XML is as follows:</p>
  
  <pre><code>&lt;item
      android:id="@+id/main_menu_refresh"
      android:enabled="true"
      android:icon="@drawable/refresh"
      android:orderInCategory="1"
      android:showAsAction="ifRoom"
      android:title="@string/refresh" 
      android:actionViewClass="IconSwitcher"&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p>I'm just having a difficult time on referencing that IconSwitcher View.</p>
  
  <p>Edit 2: I'm appearantly having trouble referencing the Menu Item at all.</p>
  
  <pre><code>MenuItem refresh = (MenuItem)findViewById(R.id.main_menu_refresh);
  refresh.setVisible(false);
  </code></pre>
  
  <p>Also gives me a null pointer when I try and set the visibility.  What is wrong with my references?</p>