Create settings menu with custom action bar / toolbar

I have already implemented a custom view for my action bar. Along with this I also need OptionMenu. Can I use the OptionMenu parameter by default? I do not want to create all the menus and its functionality on my own.

BTW, my activity extends android.support.v7.app.AppCompatActivity


Code for implementing custom view for my action bar

custom_action_bar.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentInsetEnd="0dp" app:contentInsetStart="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/custom_B" android:layout_width="60dp" android:layout_height="30dp" /> </LinearLayout> </android.support.v7.widget.Toolbar> 

I have included it in my main XML:

 <include layout="@layout/custom_action_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> 

For OptionsMenu, I put some magazines that are never printed

 @Override public boolean onCreateOptionsMenu(Menu menu) { Log.i(Constants.APP_NAME, "hello1"); } @Override public boolean onOptionsItemSelected(MenuItem item) { Log.i(Constants.APP_NAME, "hello2"); } 
+5
source share
1 answer

Remember to call setSupportActionBar() in your activity.

Without this call, the related menu methods are never called, because Activity considers that there is no toolbar.

+3
source

Source: https://habr.com/ru/post/1259264/


All Articles