Dynamically Adding and Removing Spinner from Toolbar in a Fragments - android

I have referenced this post: Change spinner style in toolbar to enable a spinner in the new toolbar. My question is if I want to add or remove this spinner based on when different fragments are displayed, how can I remove this if it is being inflated in the toolbar xml?
With the actionbar, I could add or remove menu items, can I do the same with the id of the spinner from the xml? Right now, the spinner comes up blank when it is in a fragment without me populating it with data.

I'm using View.setVisibility(int) to hide a Spinner in my Toolbar based on the current Fragment.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_menu, menu);
switch (getCurrentFragment()) {
case FRAGMENT_WITH_SPINNER:
mSpinner.setVisibility(View.VISIBLE);
break;
case FRAGMENT_WITHOUT_SPINNER:
mSpinner.setVisibility(View.GONE);
break;
}
return true;
}
I do this in onCreateOptionsMenu() so it is refreshed when some component call
mActivity.invalidateOptionsMenu();

Related

How to change text content in bottomNavigationView dynamically

I've got bottomNavigationView which is already created and attach to activity. Currently I'm trying to find out how to change text in one of navigation items which are inflated from res-menu dynamically.
I've tried to look into existing threads and none of that help me (I've already tried to pick menu from onCreateOptionsMenu and change it with setTitle option later but that wouldn't change text in the activity)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
this.menu = menu;
return true;
}
//later update on button click (this action is performed)
menu.findItem(R.id.secondItem).setTitle("New Text");
I've expected to change text to "New Text" but it's still same as before
Okay after a while of trying I've finally figured out how to do that. Instead of taking menu during onCreateOptionsMenu I've pick menu directly from bottomNavigationView when I click on the button
//your code to invoke this action
Menu menu = bottomNavigationView.getMenu();
menu.getItem(2).setTitle("New Text");
try this method inside your activity/fragment:
public void setNavigationTitle(String title) {
MenuItem menuItem = bottomNavigationView.getMenu().findItem(R.id.action_navigation_next);
menuItem.setTitle(title);
}

How to animate toolbar overflow menu icon

Is there a way to animate the default 3-vertical-dotted menu icon on toolbar?
I use toolbar as actionbar with the standard code:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
and I also use the onCreateOptionsMenu method inside activity where I inflate my menu.xml file
but I don't know how to gain more control over the overflow icon which is created automatically. What I'm most interested in is how to reference the menu icon So I can animate it. I don't care about the animation type. It can be a simple rotation animation
Well, you play with the View specifically ActionMenuView so try this, copy the codes into your Activity
//we declare our objects globally
Toolbar tool; ActionMenuView amv;
then override onPrepareOptionsMenu, what you decide to return is your choice
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//to be safe you can check if children are greater than 1
amv = (ActionMenuView) tool.getChildAt(1);//hope you've met amv
return true;
}
now this is the crucial part- whenever you want to animate the "3 verticall dots" -(your overflow) you have to check visible children-(i.e if you want to) actually forget that
amv.getChildAt(amv.getChildCount()-1).startAnimation(AnimationUtils.loadAnimation(
MainActivity.this,R.anim.abc_fade_in));
that gives you a basic fade-in animation- you can pimp your ride now.
EDIT 1:
The above code made assumptions that you have nothing added to your Toolbar aside from just inflating the menu in onCreateOptionsMenu.
Suppose you have a complex ToolBar use this rather for your initialisation
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
for(int i =0; i < tool.getChildCount(); ++i){
if(tool.getChildAt(i).getClass().getSimpleName().equals("ActionMenuView")){
amv = (ActionMenuView) tool.getChildAt(i);
break;
}
}
return true;
}
Also where you call your initialisation of amv View can be in either onCreateOptionsMenu or onPrepareOptionsMenu, i chose onPrepareOptionsMenu because i wanted readability
Hope it helps

Android - toolbar and status bar as shared objects with content changes

In my app I set the toolbar and status bar as shared objects as suggested in option #2 in this post
The general behavior and outline of the toolbar and tabs are excellent - the only issue is that when I move to activity B, some of the UI elements of the Toolbar are not participating in the content transition, particularly the Toolbar title and the menu icons.
I tried adding a SharedElementCallback and in it to loop over the children of the toolbar and tabs and add them all to a Fade transition - but it didn't affect the behaviour of the toolbar and tab content.
Any idea how to proceed from here? The desired effect is to have the elements of the Toolbar (title, up button, menu icons) participate in the content transition.
Added screenshots after comment:
Activity A
Activity B
Each activity has your own menu, so you have to create the menu for each one, even if they are the same.
However, if you prefer, you can create just one menu and create a custom class for manipulate the menu; Then you call this custom class on onCreateOptionsMenu and onOptionsItemSelected from whatever activity.
The following code is an example.
Custom class:
public class MenuActionBar {
public static void createOptionsMenu(final Activity activity, Menu menu) {
activity.getMenuInflater().inflate(R.menu.yourmenu, menu);
// Do whatever you wanna do
}
public static boolean optionsItemSelected(Activity activity, MenuItem item) {
// Do whatever you wanna do
}
}
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuActionBar.createOptionsMenu(this, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuActionBar.optionsItemSelected(this, item)
? true : super.onOptionsItemSelected(item);
}

Hiding actionbar button from expandable list view adapter of a fragment

I have an activity which consists of fragment view pager. Each fragment consists of an expandable listview. I want to show or hide the actionbar button from the expandable listview adapter. I tried it by passing the menu item to the adapter class but its not working. Cant able to show or hide the button from adapter class. Can anyone help me to obtain this.
What you can do is create a hide/show function in your Activity class that has access to your MenuItem(I am assuming you are inflating it there).
MenuItem buttonToShowHide;
....................//rest of your activity code
#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_mainmenu, menu);
//Initialize your menuitem here:
buttonToShowHide=menu.findItem(R.id.your_menu_id);
return true;
}
//Now you can make a function that will show/hide your menu item
public void setButtonVisibility(boolean visible){
//show item
buttonToShowHide.setVisibility(visible);
invalidateOptionsMenu(); //invalidate the menu
}
Now you can get your activity instance inside your adapter class easily using context.You can then use the above function to show/hide your menu item.
MyActivity myActivity=(MyActivity) context;
//now you can access the above function as:
myActivity.setButtonVisibility(true);
I hope this works for you.

Access Fragment's Menu within a FragmentStatePagerAdapter

I use a FragmentStatePagerAdapter to display fragments inside a ViewPager.
These fragments display 2 additional icons on top of the ones coming from the parent Activity.
These 2 icons are showed/hidden according to fragment displayed, so I tried to keep the Menu inflated in memory in order to modify it dynamically.
On my ViewPager, 3 fragments are always loaded, only the middle one is displayed.
The problem is the following: it seems that onCreateOptionsMenu and onPrepareOptionsMenu are called only for the first Fragment created, because of this, I get null pointers when I try to get the menu on the other fragments. Please see the code below:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Get the 2 icons for the rates
inflater.inflate(R.menu.rate_fragment, menu);
//Get activity's icons
super.onCreateOptionsMenu(menu, inflater);
mMenu = menu;
}
Here are the functions I use to hide/show the menu items:
private void hideOption(int id){
MenuItem item = mMenu.findItem(id);
if(item != null){
item.setVisible(false);
}
}
private void showOption(int id){
MenuItem item = mMenu.findItem(id);
if(item != null){
item.setVisible(true);
}
}
But these functions fail as soon as the system tries to load the second Fragment.
My guess is that the menu is only created once for the FragmentStatePagerAdapter, but then how can I modify dynamically the menu? Do I have to modify it using the Activity?
Thanks!

Categories

Resources