I am using ActionBarSherlock.
I want to change which ActionBar MenuItems are visible depending on the state of my Activity. (For instance depending on which tab is selected.)
How can I do this?
You can get the ActionMenu within the onCreateOptionsMenu method and store it in your activity and change any item visibility at any time...
You can add all items at the same menu and set it visible or not visible just using:
menu.findItem(R.id.your_item).setVisible(true/false);
in onPrepareOptionsMenu
Related
I have a navigation drawer in my app. Every Entry in the list of the NavigationDrawer represents one navigation graph. I want to mark the element in which the user currently is.
I managed to get the item to be marked after it is selected and until the visible fragment changes (even if the new fragment is still in the same navGraph). I did that by setting a NavigationItemSelectedListener using NavigationView.setNavigationItemSelectedListener(NavigationView.OnNavigationItemSelectedListener listener). Inside this listeners onNavigationItemSelected(item: MenuItem): Boolean function, I do: item.isChecked = true.
The problem with this is that the item is unchecked after the app starts and the item looses it's checked state as soon as the fragment changes (even if the navGraph doesn't change). How do I do this correctly?
I have also tried to set android:checked="true" in the xml declaration of the menu item I want to be checked when the app starts. It doesn't seem to do anything.
I have also tried to set navView.setCheckedItem(R.id.main_nav_option_one) in the Activity's onCreate function. Again, it doesn't seem to do anything.
Can you try
navView.getMenu().getItem(index).setChecked(true)
in the fragment's onResume?
I managed to solve my problem.
The most important change I had to make is mentioned here. I had to give my menu items the same ID as the ID of the navGraphs. When I did this, I removed my NavigationItemSelectedListener because it was interfering with the automatically generated navigation. Without a NavigationItemSelectedListener I could no longer handle a navigation item being clicked, that doesn't have a corresponding element in the navGraph. However I was able to fix it, by adding the NavigationItemSelectedListener back in, and making the correct calls to NavController.navigate() for every element in the list.
TL;DR:
I had to set the menu item IDs to be the same as my navGraph IDs and make the navigation calls myself anyway in the NavigationItemSelectedListener.
I have an appcompat activity with no action bar theme. I am running a fragment inside the activity to display a list of items. The items are displayed in two modes: list and delete. When the screen opens, it is always in list mode. The action bars are different in the two display modes. In list mode, in the action bar I have two menu items on the right side which are displayed when the fragment opens for the time. When I come back from the delete mode to list mode, the menu items are not inflated then. How do I make the menu items inflate while coming from delete mode to list mode.
Upon entering the screen, the user goes to the list mode. I have setHasOptionMenu(true); in my fragment. So onCreateOptionMenu and onPrepareOtionsMenu both are called and the two menu items are inflated properly.
When I switch from the list mode to delete mode (which happens when you click on the delete menu item displayed in the action bar on the top-left), I inflate a new custom view and remove all the views from the toolbar and then add this custom view to the toolbar to change the action bar as per the delete mode:
View customView = LayoutInflater.from(getActivity()).inflate(
R.layout.delete_mode_toolbar_layout, null);
Toolbar toolbar = mActionBarHelper.getToolbar();
toolbar.removeAllViews();
toolbar.addView(customView);
And it works totally fine with the delete mode.
But now when I come back to the list mode, I again do the same.
View customView = LayoutInflater.from(getActivity()).inflate(R.layout.list_mode_toolbar_layout,null);
Toolbar toolbar = mActionBarHelper.getToolbar();
toolbar.removeAllViews();
toolbar.addView(customView);
Objects.requireNonNull((AppCompatActivity)getActivity()).supportInvalidateOptionsMenu();
But this time in the list mode, the action bar is coming correctly as per the list mode's custom toolbar, and the oncreateoptionsmenu and onprepareoptionsmenu both are being called. But the menu items are not inflated.
Sorry, but due to some restrictions, this is all the code I can share. Please comment your doubts, I can try to clear them in comments.
So please can someone help me understand why the menu items are not inflating after coming back from the delete mode to list mode.
Thanks in advance.
I got a work around which is a solution to my situation but not the solution to my question.
Now when going from list to delete mode, first hide the list mode custom toolbar layout already present in the toolbar (by keeping a reference to the list mode custom toolbar layout), then inflate the delete mode custom toolbar layout and add it to the toolbar using toolbar.addView(view). Now while coming back to the list mode again from the delete mode, just remove the custom delete toolbar layout from the toolbar using toolbar.removeView(view) and then make the list mode toolbar layout, already present in the toolbar, visible.
I am trying to create a selected and unselected Item on a the ActionBar in my app. For example, when the user is in a fragment if they click the Item in the ActionBar, the ImageView will switch from a grey to a red square and if they switch to a different fragment then come back it will still be selected. So each fragment can either be selected or unselected based upon the color of the square and if the user clicked it or not. I am unsure how to implement this, anything helps?
Based on the location of the item in the ActionBar, this can be presented as a fragment option. Each fragment that gets inflated has the chance to inflate its own menu items into the action bar. This can be done by calling setHasOptionsMenu(true) in fragment's onCreate or maybe in the constructor. This will cause the onCreateOptionsMenu / onPrepareOptionsMenu / onOptionsItemSelected methods to be called and this gives you the chance to add your menu item which you can control based on your fragment's state.
First off, for saving state in a fragment, you can use onSaveInstanceState() to remember your selected state. You would need a property on your fragment:
private boolean mSelected;
In your onSaveInstanceState() override you save this value:
outState.putBoolean("selected", mSelected);
Then in onCreate() you can recover the value:
if (savedInstanceState != null) {
mSelected = savedInstanceState.getBoolean("selected");
}
And in onCreateView() you use the selected property to set up your ImageView:
imageView.setBackground(mSelected ? R.color.gray : R.color.red); // for example
So now when your user switches away from the fragment then comes back, the ImageView will have the same selection state as before.
Next thing is the action bar. Fragments can make their own changes to the menu. Start by calling setHasMenuOptions(true) in your fragment's onCreateView(). Then you override onCreateOptionsMenu() and onOptionsItemSelected() just like you would for an activity.
In my app, I need an ActionBar with a dropdown on it (a spinner / setNavigationMode NAVIGATION_MODE_LIST). The ActionBar needs to be visible in all the views/screens of the app, but the content underneath it changes - it's a list of items that when clicked, a detailed view of the item is seen.
The ActionBar state should be persistent throught the different views (e.g. the selected item in the drop down).
What would be the correct way to do it? I can think of:
A base activity that initializes the ActionBar by calling actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST), attaching an adapter if needed etc. (How to persist the dropdown state?)
Using fragments
Another way?
Which of these ways is preferred?
Thanks
I have a checkbox for one of the items in my navigation drawer. On checking it I am setting a preference value to true. In one of my fragments I have a checkbox which is set based on this preference value. Everything works fine except for one case.
That is, when I am on this particular fragment. I open the navigation drawer and check/uncheck this checkbox. Close the drawer. The setting of checkbox is not affecting in my fragment. This is because fragment is not refreshing.
How can I do this?Anyone please help me out. Thanks in advance.
put the code inside onResume section of activity and call onresume() wherever you want.