I try to change the icon of the action bar to a progress bar in onOptionsItemSelected(MenuItem)
method.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.progressitem2:
mProgress = item;
mProgressCreate = mProgress;
mProgress.setActionView(R.layout.progress);
mLayout.removeView(mTable);
// Execute code that change mTable again.
return true;
}
progress.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:focusable="true"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
style="@android:style/Widget.ProgressBar.Small"/>
</LinearLayout>
The action bar is created in this way:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar, menu);
mProgress = menu.getItem(0);
mProgressCreate = mProgress;
return true;
}
action_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/progressitem2"
android:icon="@drawable/ic_action_refresh"
android:title="Reload"
android:showAsAction="ifRoom|withText"
android:visible="true" />
</menu>
This works good. The progress icon is shown after I click the action bar icon.
I try to keep the reference of the original action bar symbol in mProgressCreate
and try to add the action view back at the end of the onCreate()
method:
mProgress.setActionView(mProgressCreate.getActionView());
But this do not work...
What`s wrong here?
Regards, Sandro