I am using ActionBarActivity from support library V7.
However, when I specify android:showAsAction="always" in the menu xml, it does not take effect. I have to do it in the Java code in the method onCreateOptionsMenu(), to find the item and set the property manually. Is there anything wrong with my code? Why does this happen?
Here is my activity code, most from the document sample:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar
.newTab()
.setText("ArtistFragment")
.setIcon(android.R.drawable.ic_input_add)
.setTabListener(
new TabListener<ArtistFragment>(this, "ArtistFragment", ArtistFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("AlbumFragment")
.setIcon(android.R.drawable.btn_radio)
.setTabListener(
new TabListener<AlbumFragment>(this, "AlbumFragment", AlbumFragment.class));
actionBar.addTab(tab);
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
mFragment.setHasOptionsMenu(true); //MUST manually set here
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
public static class ArtistFragment extends Fragment {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu1, menu);
// Manually set will take effect
menu.findItem(R.id.menu_item1)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public static class AlbumFragment extends Fragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu2, menu);
// XML set android:showAsAction="always" does not work
}
}
}
And here are the two menu xml files:
menu1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item1"
android:icon="@android:drawable/ic_menu_view"
android:title="menu1"
android:showAsAction="always"/>
</menu>
And menu2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item2"
android:icon="@android:drawable/ic_menu_view"
android:title="menu2"
android:showAsAction="always"/>
</menu>