I want to set OnClick behaviour on menu items of custom toolbar in an activity. I found many answers of same thing but on the actionbar which comes from theme, no answer was found for clicking items of menu of custom material toolbar.
Note: I don't want to add Image button on toolbar.
Any help will be appreciated.
It is the same as with standard ActionBar.
1) Replace ActionBar with your own material toolbar like so:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
2) Override OnCreateOptions menu as usual:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_layout,menu);
super.onCreateOptionsMenu(menu, inflater);
}
3) Handle clicks:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.register: {
...
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
Related
My app has minSDKVersio=11 and a targetSDKVersion=22. I am running the app on a GalaxyNexus emulator running on API14.
My activity has a Toolbar in the layout. The Toolbar is imported from the android.support.v7.widget.Toolbar library.
Here is the content of the menu resource for populating the Toolbar. The filename is homeview_menu_common.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_favorite"
android:title="XXX"
app:showAsAction="always"/>
<item
android:id="#+id/homeview_toolbar_overflow_logout"
android:title="#string/homeview_toolbar_overflow_logout"
app:showAsAction="always">
</item>
</menu>
My problem is: The overflow menu is not displayed at all. Since GalaxyNexus does not have a hardware options button, the overflow menu should be visible in the Toolber. I also find that the onCreatOptionsMenu(Menu menu) is never called.
Update: Posting the activity code
public class HomeView extends GenericActivity<HomeOps.View, HomeOps> implements HomeOps.View {
#Override
protected void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
setContentView(R.layout.homeview_layout);
super.onCreate(savedInstanceState, HomeOps.class, this);
Toolbar toolbar = (Toolbar) findViewById(R.id.homeview_toolbar);
Boolean show = toolbar.showOverflowMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.homeview_menu_common, menu);
return super.onCreateOptionsMenu(menu);
}
}
1) Replace app:showAsAction="always" by app:showAsAction="never"
2) Your basic activity implementation should look like like:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.homeview_menu_common, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorite:
// Do your stuff for favorite menu item
break;
case R.id.homeview_toolbar_overflow_logout:
// Do your stuff for homeview_toolbar_overflow_logout menu item
break;
}
return super.onOptionsItemSelected(item);
}
}
3) Use Theme.AppCompat.Light.DarkActionBar as your Activity theme. If you don'
t want to add the ActionBar from your theme, be sure to call setSupportActionBar(toolbar); in the onCreate() :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.homeview_toolbar);
setSupportActionBar(toolbar);
}
I finally solved the problem as follows:
Use Toolbar.inflateMenu() to inflate the menu items in the Toolbar.
Use Toolbar.setOnMenuItemClickListener() to set a click listener.
How can I access my Menu from my fragment and then change the icon of one of the menu items there?
What I am doing is querying my local DB to see if a certain entry exists when the fragment is shown. If it does display a solid icon, if not, display an outlined icon.
In your fragments onCreate() method you can use setHasOptionsMenu(true) to allow your fragment to handle different menu items than it's root Activity.
So you could do something like this in your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
And then, you can override any of the menu life-cycle methods in your fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_fragment, menu);
// You can look up you menu item here and store it in a global variable by
// 'mMenuItem = menu.findItem(R.id.my_menu_item);'
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_to_change_icon_for); // You can change the state of the menu item here if you call getActivity().supportInvalidateOptionsMenu(); somewhere in your code
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// Handle actions based on the id field.
}
I found a simpler way of doing it than in the provided answer:
Toolbar toolbar = requireActivity().findViewById(R.id.toolbar);
Menu menu = toolbar.getMenu();
MenuItem item = menu.findItem(R.id.item_settings);
item.setIcon(R.drawable.ic_settings);
Access the menu from the toolbar.
I have MainActivity and it's have fragment. I added toolbar from MainActivity;
MainActivity
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
When I was open any fragment from MainActivity then that fragment use own menu file
In fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
As you can see above I inflated custom menu in onCreateOptionsMenu. But It doesn't work.
after
toolbar.getMenu().clear();
add toolbar.inflateMenu(R.menu.menu_blank);
and I am not set toolbar as acionbar,Maybe you should user getActionbar to do something.
I'm not entirely sure this is what you're asking but I think you want to be able to change the options in the menu, dynamically. You can do the following
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (mHideSomething) {
MenuItem myItem = menu.findItem(R.id.action_something);
myItem.setVisible(false);
} //otherwise it will show as usual
return true;
}
Then when you want to change something in the menu...
mHideSomething = true;
supportInvalidateOptionsMenu();
EDIT
Now I understand you're just adding odd behaviour when overriding. You can still do the following (although it seems like these items just shouldn't be part of main menu if they're not relevant always)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem mainMenuItemToRemove = menu.findItem(R.id.action_something);
mainMenuItemToRemove .setVisible(false);
}
The only problem with the above is it makes assumptions about what is available in the main menu even though the fragment should be reusable. A better solution would be to pass in an interface to the fragment to call back to the activity and let the activity have control. Better still, update the activity logic and never inflate the main menu at all if not required.
So.. I'm facing a problem that has been driving me crazy for the past hours.
I have an App using the AppCompact v21 and toolbar. I also handle back navigation using:
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
In combination with the parent activity on the manifest. Which works perfect....
My problem is:
I have an activity with 3 tabs with a viewpager and I need one of the fragments to have it's own menu.
I can inflate the menu just fine but once the menu is inflated the back arrow in that fragment don't work anymore. In the other 2 fragments of the view pager the back navigation through the toolbar still works.
Inside my fragment:
// Inside onCreate...
this.setHasOptionsMenu(true);
// Later on somewhere else...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_submit, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
// my menu logic goes here.
return true;
}
Any suggestions?
When you always return true in onOptionsItemSelected(), that means you've handled every menu item possible (including the Up button). You should instead return super.onOptionsItemSelected(item) in cases where you do not handle one of your items:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Your menu logic such as
case R.id.your_menu_item:
// Do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Im currently messing arround with the new AppCompat library bringing material design to older devices.
Setting a toolbar as actionbar works fine for me, but the toolbar seems to not do anything on calling inflateMenu(int resId). From the docs, i thought this is to replace getMenuInflater().inflate(int resId) called from onCreateOptionsMenu.
If I do the latter, the menu items are correctly inflated and added to the toolbar, but inflateMenu seems to to nothing.
What am I missing?
Activity Code:
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.main); // this does nothing at all
setSupportActionBar(toolbar);
}
// this works
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Thanks in advance!
If you are calling setSupportActionBar() you don't need to use toolbar.inflateMenu() because the Toolbar is acting as your ActionBar. All menu related callbacks are via the default ones. The only time you need to call toolbar.inflateMenu() is when you are using the Toolbar as a standalone widget. In this case you will also have to handle menu item click events via
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle menu item click event
return true;
}
});