How to declare MenuItem objects instead of calling it everytime - android

I don't know how to declare MenuItem object and i call on every method the MenuItem so my code look like:
private Menu menu;
private void updateMenuIconForWifi(){
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuB.setVisible(false);
menuItemW.setIcon(R.drawable.ic_network_wifi_black_24dp);
}
private void changeIconToDefaultWifi(){
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuB.setVisible(true);
menuItemW.setIcon(R.drawable.ic_wifi_white_24dp);
}
private void updateMenuIconForBluetooth() {
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuItemW.setVisible(false);
menuB.setIcon(R.drawable.ic_bluetooth_connected_black_24dp);
}
private void changeIconToDefaultForBluetooth() {
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuB.setIcon(R.drawable.ic_bluetooth_white_24dp);
menuItemW.setVisible(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
this.menu = menu;
menuInflater.inflate(R.menu.menu_settings, menu);
for (int j = 0; j < menu.size(); j++) {
MenuItem item = menu.getItem(j);
item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return true;
}
but this look so ugly.
How to declare the MenuItems once? when i try adding some value like
private MenuItem menuB, menuItemW;
and then call this like in onCreate
menuB = menu.findItem(R.id.action_bluetooth);
menuItemW = menu.findItem(R.id.action_wifi);
i got a error

First read about onPrepareOptionsMenu(Menu menu)
Each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu method is called.
Basically, the onPrepareOptionsMenu method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
So do this (Don't use onCreateOptionsMenu(Menu menu) )
//Dynamically create context Menu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getMenuInflater();
if(condition_true)
inflater.inflate(R.menu.menu_one, menu);
else
inflater.inflate(R.menu.menu_two, menu);
return super.onPrepareOptionsMenu(menu);
}

you can save your menuItem once during onCreateOptionsMenu, and access it subsequently.
private MenuItem menuB, menuItemW;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_settings, menu);
menuB = menu.findItem(R.id.action_bluetooth);
menuItemW = menu.findItem(R.id.action_wifi);
//the rest of your codes
return true;
}

Related

How to set visibility for an Actionbar menu item?

I tried to hide item and group with below code it is not working, can someone please help me with this code. I tried for item and group both not working.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.navigation_menu, menu);
MenuItem shareItem = menu.findItem(R.id.login);
// show the button when some condition is true
if (true) {
shareItem.setVisible(false);
}
menu.setGroupVisible(R.id.common_menu, false);
return true;
}
Try this following code:
call this function whereever you want hideItem()
private void hideItem()
{
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_adduser).setVisible(false);
}

Android SearchView not working when calling invalidateOptionsMenu()

I want to hide some menu items when the search view expands so this is the code I used:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_refresh).setVisible(!isSearchOpen)
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search)
SearchView searchView = searchMenuItem.getMenu();
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
isSearchOpen = true;
invalidateOptionsMenu();
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
isSearchOpen = false;
invalidateOptionsMenu();
return true;
}
});
}
Now this code works as expected except for one issue: when I click the search button, the SearchView does not appear. I cannot figure out what is the problem. I searched for similar issues on stack overflow but I didn't found any answer the fixed the issue, so any help will be welcomed
You can try this way to hide and show other options menu:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
final MenuItem delMenu = menu.findItem(R.id.action_delete);
final MenuItem editMenu = menu.findItem(R.id.action_edit);
MenuItem searchMenu = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenu.getActionView();
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
delMenu.setVisible(false);
editMenu.setVisible(false);
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
delMenu.setVisible(true);
editMenu.setVisible(true);
return false;
}
});
return true;
}

MenuItem setVisible(false) do not work in Android

I put my MenuItem in onPrepareOptionsMenu(Menu menu) and set visible false but there is no change.
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mi = menu.findItem(R.id.example);
mi.setVisible(false);
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflater(R.menu.main_menu, menu);
// ...
}
There are no exceptions or syntax errors.
Control visibility of menu item in onCreateOptionsMenu methon, in onPrepareOptionsMenu only call invalidateOptionsMenu

What is the best (most efficient or conventional) way to control "MenuItem"s in Android's "ActionBar"?

When controlling a MenuItem, I have been doing this:
Menu menu;
(...)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
return true;
}
(...)
public void handleSearch(View view) {
Button button = (Button) view;
if(menu.findItem(R.id.action_search).isVisible()) {
button.setText(R.string.button_search_show);
menu.findItem(R.id.action_search).setVisible(false);
} else {
button.setText(R.string.button_search_hide);
menu.findItem(R.id.action_search).setVisible(true);
}
}
"this" being referencing the menu created in onCreateOptionsMenu with a Menu that any method in the class can use. The handleSearch method controls MenuItems by using findItem twice. This doesn't feel very conventional or efficient (a very scientific observation, I might add). Is there a more conventional or efficient way to do this?
You can save MenuItem in a variable instead of using findItem twice.
MenuItem myMenuitem = menu.findItem(R.id.action_search);
There is a method that handles actionBar's menu itself.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return (true);
case R.id.menu_home:
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
return (true);
}
return (super.onOptionsItemSelected(item));
}

Rename menu item in Android

I would like to rename menu on click of an item. I tried the following but it didn't change.
I tried item.setTitle("LandScape"); or item.setTitle("Portrait");
Declare the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Do something on click on the menu item:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean result = true;
switch(item.getItemId())
{
case R.id.oscillation_mode:
{
if(getScreenOrientation() == 1)
{
Log.e("Orientation","ABC"+getScreenOrientation());
item.setTitle("LandScape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
item.setTitle("Portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
The text is the same. The default it is Portrait.
when you call setRequestedOrientation() your Activity is destroyed and recreated, so any changes you make to in memory objects will be dropped. You should make these changes when the menu is shown instead
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem oscillation = menu.findItem(R.id.oscillation_mode);
if (getScreenOrientation() == 1){
item.setTitle("LandScape");
}else{
item.setTitle("Portrait");
}
return true;
}

Categories

Resources