Title: Drawer menu icon disappears after applying a custom view to ActionBar
Tags: android;android-actionbar;navigation-drawer
Question: I have a drawer menu icon before adding a custom action bar view, in this custom view, it's a just a imageView:
<?xml version="1.0" encoding="utf-8"?&gt;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"&gt;
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bell_icon"
android:layout_gravity="right"
android:layout_marginRight="@dimen/double_spacer"
android:id="@+id/ic_notif"/&gt;
</LinearLayout&gt;
and in the fragment that will have this custom view applied, I have the following lines:
@Override
public void onStart() {
super.onStart();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater mInflater = LayoutInflater.from(getActivity());
View customView = mInflater.inflate(R.layout.action_bar_custom_layout, null);
setHasOptionsMenu(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(R.string.plan_member_services);
actionBar.setCustomView(customView);
actionBar.setDisplayShowCustomEnabled(true);
ImageView img_notification = customView.findViewById(R.id.ic_notification);
img_notif.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("*** icon clicked ***");
}
});
if(isShowIcon){
img_notifi.setVisibility(View.VISIBLE);
}else{
img_notifi.setVisibility(View.GONE);
}
With above code, I am able to see the icon of the action bar defined in "AndroidMenifest.xml":
<application
android:name=".MyApp"
android:allowBackup="true"
android:allowClearUserData="false"
android:fullBackupContent="@xml/backup"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyActionBarTheme"&gt;
and also I'm able to see the title, but the menu icon for the drawer menu is gone! What have I missed?


Here is another answer: I found when I put those codes in the activity instead of the fragment, the drawer menu icon appears when the fragment loaded.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:03/18/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-actionbar><navigation-drawer>', 'Title': 'Drawer menu icon disappears after applying a custom view to ActionBar', 'ViewCount': 31, 'id': 55229637, 'user_AboutMe': None, 'user_AccountId': 6410519.0, 'user_DisplayName': 'shawn', 'user_Reputation': 45.0, 'user_Views': 9.0, 'user_id': 4970440.0}}