Inflate menu in CollapsingToolbarLayout

I am using the CollapsingToolbarLayout design support library. My problem: I cannot inflate the menu action icon on the toolbar. I am using the code below to create a CollapsingToolbarLayout.

<android.support.design.widget.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="240dp"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginBottom="32dp" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/profile_image" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="centerCrop" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> 

In fragment I use

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_emp_details, menu); super.onCreateOptionsMenu(menu,inflater); } 

and setHasOptionsMenu(true);

+4
source share
2 answers

There are two ways to do this: First you need to tell Activity that the Toolbar your use of the Action Bar with: setSupportActionBar(toolbar)

Else (recommend):

 Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); toolbar.inflateMenu(R.menu.your_menu_items); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { TODO: Write your logic here } }); 
+8
source

I believe that when you say β€œAction icon on the toolbar”, you mean the UP action on the left side of the toolbar.

If you mean the menu on the right side with a 3-dot icon, @Janhavi's answer is correct. If you mean the UP icon on the left side, read below:

You just need to configure it on the toolbar after inflating the layout, for example, the following code:

 @Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // inflate your view normally View root = inflater.inflate(R.layout.my_layout, container, false); // configure your views Toolbar toolbar = (Toolbar)root.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.icon); toolbar.setNavigationOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ // TODO: code your UP navigation here // probably: getFragmentManager().popBackStack ? } }); // return the view return root; } 

icon you can download here: https://www.google.com/design/icons/

+4
source

All Articles