java - Menu Item onClickListener for fragments -
i have following menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".mainactivity">  <item     android:id="@+id/action_settings"     android:orderincategory="100"     app:showasaction="never"     android:title="settings" />  <item android:id="@+id/menu_share"     android:icon="@drawable/ic_heart_outline_white_24dp"     app:showasaction="always"     android:title="favorite" /> </menu>   how set onclick listener this? bearing in mind class extends fragment:
public class my_profile extends fragment implements ....{}   ...and not know if makes difference have inflated layout in java file too.
@override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {  myview = inflater.inflate(r.layout.testfragment, container, false); ... }   update 1
this tried after suggestions nothing seems happen can please tell me im doing wrong:
    @override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         inflater.inflate(r.menu.menu_main, menu);         super.oncreateoptionsmenu(menu, inflater);     }    @override     public boolean onoptionsitemselected(menuitem item) {         int id = item.getitemid();          if (id == r.id.menu_share) {             log.d("buttonclick", "button click worked");         }          return super.onoptionsitemselected(item);     }   and in oncreate added this:
sethasoptionsmenu(true);   update 2
dont know if makes difference, i'm working example @ moment , menu inflated this. dont know if affects other 2 methods:
 mtoolbar.inflatemenu(r.menu.menu_main);   update 3
this have tried yet, still button click doesn't log anything:
 public view oncreateview(layoutinflater inflater, @nullable viewgroup container,                              @nullable  bundle savedinstancestate) {          sethasoptionsmenu(true);          myview = inflater.inflate(r.layout.testfragment, container, false);          ...          return myview;         }   and testing redirected 1 of other activities, yet nothing still happens:
 @override     public boolean onoptionsitemselected(menuitem item) {         int id = item.getitemid();          if (id == r.id.menu_share) {             intent myintent = new intent(getactivity(),loginactivity.class);             startactivity(myintent);         }else{             intent myintent = new intent(getactivity(),signupactivity.class);             startactivity(myintent);         }          return super.onoptionsitemselected(item);     }       @override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         super.oncreateoptionsmenu(menu, inflater);         inflater.inflate(r.menu.menu_main, menu);     }      
your fragment should have these 2 methods handle menu: oncreateoptionsmenu , onoptionsitemselected onclick stuff in latter.
for example:
@override public boolean onoptionsitemselected(menuitem item) {     int id = item.getitemid();      if (id == r.id.action_settings) {         //todo     } else if (id == r.id.menu_share) {         //todo     }      return super.onoptionsitemselected(item); }   edit
don't forget sethasoptionsmenu(true) fragment (thanks)
Comments
Post a Comment