I want to implement menu in fragment class. The fragment works fine and there is not error but there is no menu shown in the fragment. I am implementing the class in the following way
public class FilesFragment extends Fragment {
public FilesFragment() {
// Required empty public constructor
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_files, container, false);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.files_menu,menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_setting){
Toast.makeText(getActivity(),"setting clicked",Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
and this is the xml file of files_menu in menu folder.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_setting"
android:title="settings"
app:showAsAction="never"/>
<item
android:id="@+id/aobut"
android:title="About"
app:showAsAction="never"
/>
</menu>
The above code is not showing the menu at all. How do menu is implemented in the fragment?
You can populate the menu in any view without any complexity. Use the following code and enjoy.
any View can be a button, imageView or any other else in which you want to inflate this menu. No need to add menu in xml. Just do in java
PopupMenu popup = new PopupMenu(context, any View);
popup.inflate(R.menu.files_menu );
popup.setOnMenuItemClickListener(item -> {
int id = item.getItemId();
if (id == R.id.action_setting) {
// do stuff
} else if (id == R.id.aobut) {
//
}
return true;
});
popup.show();
I want to implement menu in fragment class. The fragment works fine and there is not error but there is no menu shown in the fragment. I am implementing the class in the following way
public class FilesFragment extends Fragment {
public FilesFragment() {
// Required empty public constructor
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_files, container, false);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.files_menu,menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_setting){
Toast.makeText(getActivity(),"setting clicked",Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
and this is the xml file of files_menu in menu folder.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_setting"
android:title="settings"
app:showAsAction="never"/>
<item
android:id="@+id/aobut"
android:title="About"
app:showAsAction="never"
/>
</menu>
The above code is not showing the menu at all. How do menu is implemented in the fragment?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.