Android add icon in toolbar inside a fragment -
i implement toolbar , replace actionbar according requirments. want add icon in toolbar inside fragment. tried oncreateoptionmenu() , pass menu xml it. wont work. tried other things , googling nothing works far. have idea this. here fragment code
public class fragment_favouritelocations extends fragment { public fragment_favouritelocations() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_main_fav_location_row_item, container, false); } @override public void oncreateoptionsmenu( menu menu, menuinflater inflater) { inflater.inflate(r.menu.fav_location_menu, menu); super.oncreateoptionsmenu(menu, inflater); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.edit_button) { toast.maketext(getactivity(), "", toast.length_long).show(); return true; } // // if(id == r.id.action_search){ // toast.maketext(getapplicationcontext(), "search action selected!", toast.length_short).show(); // return true; // } return super.onoptionsitemselected(item); } } here menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mayapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/edit_button" mayapp:icon="@android:drawable/ic_menu_edit" android:title="edit" mayapp:showasaction="always" /> </menu> thanks in advance
in fragment, in oncreateview() method, have add:
sethasoptionsmenu(true); otherwise oncreateoptionsmenu() never called.
Comments
Post a Comment