Show and hide menu icon in toolbar with Fragment in Android Studio - android

I am trying to use this code to show and hide menu icon in toolbar with Fragment in Android Studio:
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem IconBTON = menu.findItem(R.id.myIcon);
if (statusBlueTooth == true){
IconBTON.setVisible(true);
}
if (statusBlueTooth == false){
IconBTON.setVisible(false);
}
}
but the icon is not shown even though the value of the flag statusBlueTooth = true, but just when I touch the toolbar the icon is shown, what do I need to do so that this is shown according to the value of flag statusBlueTooth

Try adding this after in your onCreate of your Fragment:
setHasOptionsMenu(true);
and check IconBTON != null before setting the visibility.

Related

How to modify WearableActionDrawer on runtime?

I have created an ActionDrawer for my Wear OS App and want to change the icon and text on runtime. This code is working and my app continues to run without errors, but I am not able to slide up the ActionDrawer anymore. Also, when the ActionDrawerMenu is already opened and I press the button there, it disappears and my UI of the app seems frozen.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Top navigation drawer
WearableNavigationDrawerView wearableNavigationDrawer = (WearableNavigationDrawerView) findViewById(R.id.top_navigation_drawer);
wearableNavigationDrawer.setAdapter(new NavigationDrawerAdapter());
wearableNavigationDrawer.getController().peekDrawer();
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = (WearableActionDrawerView) findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
final int itemId = menuItem.getItemId();
switch(itemId) {
case R.id.menuItem_pause:
if(intent == null) {
menuItem.setIcon(getDrawable(R.drawable.ic_pause));
menuItem.setTitle(R.string.actionDrawerMenuPause);
} else {
menuItem.setIcon(getDrawable(R.drawable.ic_play));
menuItem.setTitle(R.string.actionDrawerMenuStart);
}
invalidateOptionsMenu();
return true;
}
return false;
}
I have tried some solutions with invalidateOptionsMenu(), onPrepareOptionsMenu() and onCreateOptionsMenu() but nothing worked for me. I think this just works with standard Android mobile apps but not with Wear OS. So how can I change the text and icon of my WearableActionDrawer-MenuItems when pressing a MenuItem?
I found the solution by accident. It's just this one line of code that programmatically implements the menu.
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
getMenuInflater().inflate(R.menu.action_drawer_menu, wearableActionDrawer.getMenu());

Hide options menu in xamarin android

I'm having a single activity in my android application which I'm developing with Xamarin.
There is an option menu in my application which is appearing in all the pages. I want the option menu to be hidden in my first page and then be visible in rest of the pages.
My code is like this:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
if (!UserPrefExist()) {
SetContentView (Resource.Layout.FirstPage);
HandleSpinnerMethods ();
} else {
isFirstPage = false;
DisplayMainPage ();
}
}
And then I'm having this method:
public override bool OnPrepareOptionsMenu(IMenu menu)
{
IMenuItem menitm = menu.FindItem (Resource.Id.MyOption);
if (isFirstPage) {
menitm.SetEnabled (false);
return false;
} else {
menitm.SetEnabled (true);
return true;
}
}
The options menu is getting hidden all right, but its getting hidden for all the pages, which is not desirable as I only want to hide the option menu in the first page.
Need help from you guys.
Thanks & Regards,
Anirban
you should not directly enable/disable this on main page.as it is access locally obviously not affects other pages.You must set one flag in if (isFirstPage) { code instead of enabling/disabling and access that flag in rest of the pages to show menu or not.here you need to explicitly hide menu in these rest of the pages.

how to hide/show OptionsMenu with animation

I have a NavigationDrawer and hide/show the OptionsMenu (which is located on the bottom due to a split ActionBar) whenever the drawer gets opened/closed:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!isDrawerOpened) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
return false;
}
I would like to hide the Menu with an animation, (for reference: like the animation used by the system when you call ActionBar.hide() ), however I found no way to access the Layout used for the Menu (so I could get a View object to apply the animation to). It seems there's no public API available to perform this, I wouldn't mind some reflection approach if I knew what to look for. Any suggestions?
UPDATE
looking at this source I finally figured out how to access the View of a split ActionBar using a non-public identifier:
private View getSplitActionBarView() {
Window window = getWindow();
View v = window.getDecorView();
return v.findViewById(getResources().getIdentifier("split_action_bar",
"id", "android"));
}
Now I can create an Animation, set an AnimationListener and call invalidateOptionsMenu() as soon as the Animation ends. There's one problem left: doing this will let an ugly white rectangle at the place where the bottom part of the ActionBar was, this white rectangle will stay there until the Animation is complete. To get rid of it I would need to use <item name="android:windowActionBarOverlay">true</item> in my style XML (or doing the same by code) to display the ActionBar in overlay mode. However this is not what I need.
The question is now: is there any way to apply the overlay mode only to the split ActionBar (= the bottom part of the ActionBar)?
You need to call invalidateOptionsMenu() when the drawer is open/close, like this
public void onDrawerOpened(View view) {
invalidateOptionsMenu();
}
Now, onPrepareOptionsMenu() will be called,where you can hide/show the option menu like this..
#Override
public boolean onPrepareOptionsMenu(Menu menu){
// check if drawer is open
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
for(int index = 0 ; index < menu.size() ; index ++){
MenuItem menuItem = menu.getItem(index);
if(menuItem != null) {
// hide the menu items if the drawer is open
menuItem.setVisible(!drawerOpen);
}
}
return super.onPrepareOptionsMenu(menu);
}
You can hide all the menu or any of the required menu item.

How do we dismiss actionbar SearchView?

I want to dismiss my actionbar searchview after the user submits a search. This is what I have:
SearchView.OnQueryTextListener listener = new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
mSearchView.setIconified(true);
return false;
}
};
but it appears to have no effect. I've also tried keeping a reference to its menu item and doing:
mSearchMenuItem.collapseActionView();
no effect. What am I doing wrong?
Thanks
You could set its visibility in order to make it invisible :
mSearchView.setVisible(false);
Then turn it back to true when you come back on the activity
EDIT
In my case, but mSearchMenuItem.collapseActionView(); worked, but I had to add mSearchView.setOnQueryTextListener(null); just before it. Maybe you can try that too.

Changing options menu/actionbar menu programatically for 3.0+

In short, here's my question:
Can option menus (shown in the actionbar) be modified programatically on android 3.0+?
I have a wizard-style activity in which I use a ViewFlipper to switch between views, or steps.
The steps are: 1 -> 2 -> 3. Only the second screen (2) has a menu item, while the others don't. I have tried hanging on to the Menu reference (source) and either removing/adding items or just hiding/showing them.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
mMenu.getItem(0).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Switch to the second screen ->
public void showNext(View v) {
if (mVFlipper.getDisplayedChild() < (mVFlipper.getChildCount() - 1)) {
mVFlipper.showNext();
if (mVFlipper.getDisplayedChild() == 1) {
setTitle("Second screen");
mMenu.getItem(0).setVisible(true);
}
}
}
This works fine on 2.2, but fails miserably on 4.1. Starting off with a visible MenuItem and hiding it later works. Starting off with an invisible menu item and showing it later -
There is a bug in Android's MenuItem setVisible that causes problems when turning items back to visible.
In your onCreateOptionsMenu(), add a check to see if the displayed page needs the Menu, if it does, add the MenuItem. Then, call invalidateOptionsMenu() whenever the page changes. That will rebuild the Menu.
I was also struggling with this issue, then I applied a small hack:
menu1.setEnabled(false);
menu1.setTitle("");
Then where you want to visible it again:
menu1.setEnabled(true);
menu1.setTitle("Okay"); //or you can set text according to your given updated values.
Problem fixed by having the MenuItem be visible after onCreateOptionsMenu finishes and then hiding it from a callback called after onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
boolean dummyVal = super.onCreateOptionsMenu(menu);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
mMenu.getItem(0).setVisible(true);
} else {
mMenu.getItem(0).setVisible(false);
}
return dummyVal;
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (mMenu != null) {
mMenu.getItem(0).setVisible(false);
}
}
}
If anyone has this problem, I recommend trying toadzky's suggestion first: calling "invalidateOptionsMenu()".

Categories

Resources