Android Action buttons remain from previous view - android

I have created a master detail view in Android using fragments. (For now I'm considering the phone layout and not the tablet layout).
In my master view, I have a list of items and I show a "add" button in the action bar.
In my detail view, I show the details for the selected item, a "delete" button and a "edit" button in the actionbar.
Now the way I have done it, the "add" button is still present in the action bar in my detail view.
Master class:
public class AvailableZonesFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_available_zones, menu);
}
}
Master menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:orderInCategory="1"
app:showAsAction="always"
android:title="Add">
</item>
</menu>
Detail class:
public class ZoneDetailsFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_zone_details, menu);
}
}
Detail menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:orderInCategory="1"
app:showAsAction="always"
android:title="Edit">
</item>
<item
android:id="#+id/remove"
android:icon="#android:drawable/ic_menu_delete"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:title="Delete">
</item>
</menu>

Did you try to call
menu.clear();
as first line in onCreateOptionsMenu in your details fragment, to remove menu items from previous fragment.

Related

NavigationView menu's .add not working except in main bodies of onCreate() and onResume()

This is the code of onNavigationItemSelcted
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Menu eventMenu = mNavigationView.getMenu();
eventMenu.add(R.id.events,Menu.NONE,1,"newEvent").setIcon(R.drawable.hangouts_white);
// extra code
}
However, I can perfectly use this add method in onCreate() and onResume(). But, again, if I use this method in onClick() in onCreate() or onResume(), it does not work. As long as it is in their main body, it will work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNavigationView = findViewById(R.id.navigation_view);
drawerAndToggle();//For drawerlayout setup
Menu eventMenu = mNavigationView.getMenu();
MenuItem eventItem = eventMenu.findItem(R.id.add_custom_event);
eventItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// This gets executed but does not do anything
eventMenu.add(R.id.events,Menu.NONE,1,"New item");
return true;
}
});
// This one works fine
eventMenu.add(R.id.events,Menu.NONE,1,"New item");
}
But I need to add an item if the user selects "Add Item" which is one of the menu items in the Navigation Drawer. What am I missing or what should I be adding?
Here is my nav_menu XML of my navigation view
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/events">
<item
android:orderInCategory="1"
android:id="#+id/item1"
android:title="Item 1" />
<item
android:orderInCategory="1"
android:id="#+id/item2"
android:title="Item 2" />
</group>
<group android:id="#+id/settings">
<item
android:orderInCategory="2"
android:id="#+id/add_item"
android:title="Add Item"/>
</group>
</menu>

How to set menu to toolbar in android that should display in fragment

I have created a toolbar which is used in whole android app. Now I have to add menu item to the common toolbar.And I am following MVVM architecture.
menu_sort.xml code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/sortToolBar"
android:title=""
app:showAsAction="ifRoom"
app:actionLayout="#layout/common_tool_bar"
android:icon="#drawable/ic_sort"/>
</menu>
And Even I added below code in fragment
And even I added
setHasOptionMenu(true)
fragment.xml
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_sort, menu);
super.onCreateOptionsMenu(menu,inflater);
}
You need to set setHasOptionsMenu flag as true on the fragment onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

Adding icon on Action Bar instead of three dots

I'm practicing follow this guide http://developer.android.com/training/basics/actionbar/adding-buttons.html
but it doesn't display icon "search" on action bar, it just have "Search" string after I clicked on "three dots". How to remove three dots and showing search icon on Action Bar?
My app like this but there is no search icon, but there is a "Search" string in "three dots"
add this to your menu item:
android:icon="#drawable/my_icon"
app:showAsAction="ifRoom"
What you have to do is setHasOptionsMenu method in true and override onCreateOptionsMenu and onOptionsItemSelected if you are using fragments.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.events_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearchView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the menu xml file that displays a search option on the ActionBar:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:showAsAction="always" />
You can remove the 3-dots menu by just adding below attribute android:showAsAction="never" to it:
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
And also use app:showAsAction="always" in all other menu items.
Hope this helps!

How To Creating Dynamic Menu to show on Fragment

I have a class BaseFragment i was create a private menu and set that menu from
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
this.menu = menu;
}
in main.xml i have :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"/>
<!-- Refresh -->
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
android:showAsAction="ifRoom" />
<!-- Download -->
<item android:id="#+id/action_download"
android:icon="#drawable/ic_action_download"
android:title="#string/action_download"
android:showAsAction="never"/>
<!-- Favorite -->
<item android:id="#+id/action_favorites"
android:icon="#drawable/ic_action_favorites"
android:title="#string/action_favorites"
android:showAsAction="never" />
</menu>
and i have another fragments FragmentA and FragmentB extends to that BaseFragment.
In FragmentA i want to show All menu and all is OK when running the app, but in FragmentB i want to show the download and and favorite whitout search and refresh.
I was try to create a method on base to manipulate the visibility on menuitem and my menu always return null.
protected void setSearchable(boolean isSearchable){
menu.findItem(R.id.action_search).setVisible(isSearchable);
}
protected void setHasRefreshFuction(boolean isHasRefreshFunction){
menu.findItem(R.id.action_refresh).setVisible(isHasRefreshFunction);
}
protected void setDownloadable(boolean isDownloadable){
menu.findItem(R.id.action_download).setVisible(isDownloadable);
}
protected void setFavorite(boolean isFavorite){
menu.findItem(R.id.action_favorites).setVisible(isFavorite);
}
i call that method on onCreate() in FragmentB like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDownloadable(true);
setFavorite(true);
setHasRefreshFuction(false);
setSearchable(false);
}
Can someone help me please.... i was read many tutorial and doesn't find an answer to make this.
note : I was setHasOptionsMenu(true); in oncreate in BaseFragment
The easiest way would be to override onCreateOptionsMenu in FragmentB again.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
menu.findItem(R.id.action_search).setVisible(false);
menu.findItem(R.id.action_refresh).setVisible(false);
this.menu = menu;
}
You can't access menu outside it's callbacks. For you can implement ActionProvider subclass to handle visibility of needed menu items.
Or you can call invalidateOptionsMenu() method of your activity to request Options Menu recreating, and change visibility of items in onCreateOptionsMenu()
In the menu.xml you should add all the menu items. Then you can hide items that you don't want to see in the initial loading.
<item
android:id="#+id/action_newItem"
android:icon="#drawable/action_newItem"
android:showAsAction="never"
android:visible="false"
android:title="#string/action_newItem"/>
Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You don't need to override onCreateOptionsMenu in your Fragment class again. Menu items can be changed (Add/remoev) by overriding onPrepareOptionsMenumethod available in Fragment.
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_newItem).setVisible(true);
super.onPrepareOptionsMenu(menu);
}

MultiSelect Menu in Action Bar Sherlock

I wan to have one action bar with one "menu" button. (Until here I know how to do) But Now I want to do that, when the users Clicks on any subitem of the menu, don't close this menu.
Said in other words, I want to enable the multiselect menu in action bar sherlock, but I dont know how to do it.
Can someone explain me some way to achive it?
Thats what I have
public class MainActivity extends SherlockActivity{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setTitle("Testing");
actionBar.setSubtitle("test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
And this is the XML Menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu"
android:orderInCategory="0"
android:title="Menu"
android:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/subitem1"
android:title="SubMenu1"/>
<item
android:id="#+id/subitem2"
android:title="SubMenu2"/>
<item
android:id="#+id/subitem3"
android:title="SubMenu3"/>
<item
android:id="#+id/subitem4"
android:title="SubMenu4"/>
</menu>
</item>
</menu>

Categories

Resources