How To Creating Dynamic Menu to show on Fragment - android

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);
}

Related

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);
}

Android Action buttons remain from previous view

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.

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!

extend actionbar menu items

I create base activity with actionbar menu functional and extended another activities from it.
public class BaseActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
return true;
}
}
Now I want to add some buttons to the actionbar in some activities. How can i add elements to actionbar and use element from base activity?
You can do that as simple as in BaseActivity just don't forget to call super.onCreateOptionsMenu().
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help" />
</menu>
home.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:title="#string/action_new" />
</menu>
In BaseActivity you are using base menu config main.xml.
public class BaseActivity extends SherlockFragmentActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In child activity you are using another config from home.xml only with additional menu items, no duplicates. But don't forget to call super.onCreateOptionsMenu(menu) with the same menu instance to add elements from BaseActivity (parent menu items would be added after child menu items if you call super's method after inflate, and before otherwise).
public class HomeActivity extends BaseActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
}
You can specify your actions for the actionbar in the main.xml
example :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
And call from your subclass
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Make different xml files containing the buttons you need specific to those Activity. Say for Activity1 you have two buttons and for Activity2 you have one, then you will create 2 xml files like below.
action_activity1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
action_activity2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
</menu>
Then in the onCreateOptionsMenu(Menu menu) method inflate the desired xml file. Like,
Activity1:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity1, menu);
return true;
}
Activity2:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity2, menu);
return true;
}
Please be care full with the syntax (as I used appcompat actionbar). :)
This is what I use. Hope you find it helpful. And, I will be happy to see an easier way than this. :)

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