How can I change Menu item size in android? - java

I created a simple menu item with the help of ActionBarSherlock:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, ITEMD, 0, "item").setIcon(R.drawable.topbar_btn_inbox).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.breadcrumb_bar_menu, menu);
return true;
}
But I can't change its width and height. How can I do this in XML or programatically?

As #Tanis.7x stated and according to the Action Bar Icons Documentation size and densities for different devices are already predefined for consistency, so you cant change any. However you can define your custom Action Bar view and then behave like a view and change sizes, etc:
actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
actionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.custom_view);

Related

Update ActionBar menu depending on navigation destination

I would like to change the menu items displayed in my ActionBar depending on the current destination of my NavController. By "change" I mean inflate a specified menu-xml for each destination.
The behavior could be compared to the integrated way the new navigation system changes the ActionBar title, depending on the given android:label for the destination fragment in the navigation-xml.
So far I setup a basic activity with ActionBar and DrawerLayout using the new Android navigation. Also I created all necessary XML files and Fragments to navigate between.
...
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
this._drawerLayout = this.findViewById(R.id.drawer_layout);
Toolbar toolbar = this.findViewById(R.id.action_bar);
this.setSupportActionBar(toolbar);
ActionBar actionbar = Objects.requireNonNull( this.getSupportActionBar() );
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
NavigationView navigationView = this.findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(menuItem -> {
menuItem.setChecked(true);
this._drawerLayout.closeDrawers();
return true;
});
NavController navController = Navigation.findNavController(this, R.id.navigation_host);
NavigationUI.setupWithNavController(toolbar, navController, this._drawerLayout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Here I inflate the ActionBar menu that is kept between all destinations.
// Instead of keeping the same menu between all destinations I want to display different menus depending on the destination fragment.
this.getMenuInflater().inflate(R.menu.actionbar_items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
this._drawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.appbar_search:
return true;
}
return super.onOptionsItemSelected(item);
}
I thought about using an individual toolbar in each destination fragment
but I discarded the idea, because I would lose the transition animation between hamburger icon and back-arrow icon.
Is there any way to achieve this with the new navigation system or in any other way?
I just tested this. With the current version (2.2.2) of the navigation library, menu items inflated by each fragment will be added and removed as you navigate around.
But onCreateOptionsMenu is not called by default! To make things work, you have to sprinkle the magic dust and call setHasOptionsMenu(true) during initialisation.
Beware, any menu items inflated by the containing activity will always appear alongside the ones from the current fragment, so if you don't want any common menu items, it's probably easiest to not inflate a menu in the Activity.
Side note: in the Fragment class, onCreateOptionsMenu(menu, inflater) is an empty method, so the call to super.onCreateOptionsMenu(menu, inflater) in your code is a no-op.
I found a way to achieve the desired behavior by calling menu.clear(); before inflating the updated layout. I am still wondering if there is a built-in way to achieve this with the new navigation system.
In the destination Fragment I am using this now:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.toolbar_items_idle, menu);
}

Android - inflater menu disable / enable

I had an inflater menu created in a fragment.
This is the code.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
It works, but I want that if I click on a button the inflater is disable. It means that also if I click on the inflater it doesn't show the menu. When I click on another button the inflater return to show its options. How can I do? Thanks!

Whenever I try to add a button to my actionbar, the items are added under the overflow menu

I am trying to add a simple button to my action bar, but when I try to use the following code, the items are added as items under the overflow menu, not on the actionbar itself. Any help would be appreciated,
Jacob
Here is my Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_review, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/seda"
android:icon="#drawable/redpinreal"
android:title="Hello Worldh"
android:showAsAction="withText|always"/>
</menu>
I also have this enabled:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Part of your code (android:showAsAction, instead of yourapp:showAsAction) is attempting to use the native action bar. Part of your code (getSupportActionBar(), instead of getActionBar()) is attempting to use the appcompat-v7 action bar backport. Choose one and stick with it.

Android set menu items dynamically at startup

I need to change the title of a menu item in my action bar at startup based on a few variables which get created at the startup.
But for some reason I cant simply do that since the menu items take time to inflate maybe?
how do I get around this issue.
below is my attempt but it throws java.lang.IndexOutOfBoundsException
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.map_fragment_action_menu, menu);
mOptionsMenu = menu;
mOptionsMenu.getItem(R.id.map_fragment_action_layers_0).setTitle("my title");
}
P.S. I am using a fragment, I also tried to set the title in onCreateView() method but still doesn't work.
You need to use the method Menu#findItem() instead.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.map_fragment_action_menu, menu);
mOptionsMenu = menu;
mOptionsMenu.findItem(R.id.map_fragment_action_layers_0).setTitle("my title");
}
Menu#getItem() expects an index and not the menu item's id. For e.g, if this menu item is the first item in the menu, you would use
mOptionsMenu.getItem(0).setTitle("my title");

Adding separators between groups of actionbar menu items

I'm creating an android action bar menu for an app. What I'm looking for is a way to show the logical groupings of menu items. So what I have currently is:
Whereas what I'd like is:
I'm creating the menu as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu){
//Note, on 2.3.x - 3.0 this is called when user opens the menu for the first timed
//on 3.0+ it is called when the activity starts
MenuInflater inflater = getMenuInflater();
//adds the (currently empty) menu, after login it will be filled with options
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.add("example");
menu.add("example");
return true;
}
Menu XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto"
>
</menu>
I had hoped menu groups would help, but I understand that they are just for modifying all the menu items properties together. Equally, submenu actually creates a hidden menu that is opened free-floating when clicked.
Is there any way to create this logical grouping of menu items that I'm looking for?

Categories