I'm trying to add a button to my menu bar in my ListFragment class. When I click the button, nothing happens though. The onCreateOptionsMenu method is never being called in the class. I've tried everything to get it working but it still won't work. Has anyone got any solutions?
I've attached my code below:
public class FragmentZero extends ListFragment {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// use your custom layout
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_zero, container, false);
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu items for use in the action bar
inflater.inflate(R.menu.main_fragment_zero, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemsSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_refresh:
// check for updates action
RefreshButton();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Add setHasOptionsMenu(true) to your onCreateView() method and remove it in OnCreate().
So this was a pretty stupid error that I was having and I solved it thanks to everyone's help here.
I was spelling onOptionItemSelected wrong. I was adding an extra 's' after item.
As soon as I corrected this, the method started to work.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
RefreshButton();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void RefreshButton() {
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
}
Related
I'm having a ToolBar made as ActionBar in a Fragment. I'm able to add ActionBar menu items but I'm not able to receive click response when I click on any ActionBar menu item.
I have read a lot of similar questions, I tried all but I'm still facing issue, so asked a question here.
The Fragment
public class DetailFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
return inflater.inflate(R.layout.project_detail, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mToolBar = (Toolbar)view.findViewById(R.id.tb_toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(mToolBar);
mToolBar.setTitle(R.string.project_details);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.project_detail_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// do something
break;
}
return true;
}
In my case in code above, onOptionsItemSelected is not even getting called.
Any pointers why ?
The Activity where I'm inflating this Fragment -
public class TestJust extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_just);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new ProjectDetailFragment());
ft.commit();
}
}
project_detail_menu.xml -
<?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/options"
android:title="options"
android:icon="#android:drawable/ic_menu_more"
app:showAsAction="always"/>
</menu>
Most probably your problem is that you're always returning true from the activity's onOptionsItemSelected method.
When you return true, you consume the click event inside the method in the activity, that's why the click event never reaches the onOptionsItemSelected method inside your fragment.
Try this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear()
inflater.inflate(R.menu.project_detail_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
Solution:
You are calling setHasOptionsMenu(true); in onCreateView()
Please call that method in onCreate()
Like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Calling this method in onCreate() ensures that the menu must participate in Fragment.
Then,
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu_items, menu);
}
Finally,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// do something
break;
}
return super.onOptionsItemSelected(item);
It should work now.
initialize option in onCreateOptionMenu then check it. you only inflate menu, use without initialize option, first initialize then use it.
like: MenuItem item = menu.findItem(R.id.option);
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
MenuItem item = menu.findItem(R.id.option);
}
switch (item.getItemId()) {
case R.id.options:
// do something
return true;//<- Here you need to return true because breaking compile from this line returns default false;
default:
return super.onOptionsItemSelected(item);
}
I am working on a navigation drawer in which i am using multiple fragment .In one of the fragment i have to use menu item for some action .Now i can inflate menu item on action bar on that fragment but action is not performing .
public class Location extends Fragment implements View.OnClickListener {
GoogleMap googleMap;
Fragment fragment;
Button arrived_mbtn;
TextView current_mtv,request_mtv;
LinearLayout btn_mlayout,journey_mlayout;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_location, container, false);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("you are in oncreate", "dfsdfsd");
setHasOptionsMenu(true);
}
#Override
public void onDestroyView() {
// TODO Auto-generated method stub
try {
if (fragment != null) {
fragment = getFragmentManager().findFragmentById(R.id.map);
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
Thread.interrupted();
super.onPause();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_payment_card_detail,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_settings:
Toast.makeText(getActivity(),"hello",Toast.LENGTH_SHORT);
return true;
}
return super.onOptionsItemSelected(item);
}
}
by this code an item is inflate on action bar but on click it did,t show any thing .
Change like this. You have not called show() on toast.
case R.id.action_settings:
Toast.makeText(getActivity(),"hello",Toast.LENGTH_SHORT).show();
Change your onCreateOptionsMenu method as below.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_payment_card_detail,menu);
}
And whatever id you have applied to your menu item, use that in the condition to check whether that menu item is clicked or not.
in your code:
Move the setHasOptionsMenu(true) in the onCreateView() method;
Use inflater.inflate(R.menu.menu_payment_card_detail,menu); in the
onCreateOptionsMenu method.
Something like this:
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_payment_card_detail,menu);
super.onCreateOptionsMenu(menu, inflater);
}
Add the show() method to your Toast Toast.makeText(getActivity(),"hello",Toast.LENGTH_SHORT).show();
In fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set setHasOptionsMenu
setHasOptionsMenu(true);
}
Check the parent activity doesn't eat the item selected, i.e. make sure if the parent activity implements onOptionsItemSelected() that it returns false for your fragment's items.
I am trying to create an Action Bar with one option in a fragment but it doesn't work.
And I have followed all the steps which are indicated in the internet tutorials:
Create the function "onCreate", it doesn't appear by default, with the "sethasoptionmenu(true)".
Write the function "onCreateOptionsMenu" with the "inflater.inflate(R.menu.menu_fotos, menu);"
Create the function "onOptionsItemSelected"
With this, what I get is a normal options menu. But what I need is an Action Bar menu!
Can you help me?
I copy here the code of the fragment:
package com.carlesqf.laguerra;
import *.*;
public class FragmentContingutCapitols extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.activity_contingutcapitols, null);
// Getting the bundle object passed from "PantallaContingutCapitols"
Bundle b = getArguments();
String nomcap=LlistaCapitols.name[b.getInt("position")];
if (nomcap.contains("1700-1701 Les causes del conflicte:"))
v = inflater.inflate(R.layout.capitol1700, null);
else if ((nomcap.contains("1702 – Primers combats. Itàlia i front del Rin:")))
v = inflater.inflate(R.layout.capitol1702, null);
...
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do something that differs the Activity's menu here
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_fotos, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Actually the actionbar menu is a normal options menu. Is NavigationDrawer what you're looking for?
I know that this question has already been asked, but none of the proposed solutions work for me.
I want this method to be called from the Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
View view = inflater.inflate(R.layout.my_clock_layout, container, false);
listView = (ListView) view.findViewById(R.id.clock_list);
listView.setAdapter(new ClockAdapter(this.getActivity()));
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.add_alarm:
Fragment newContent = new AddAlarmFragment();
if (getActivity() instanceof MenuMainActivity) {
MenuMainActivity mma = (MenuMainActivity) getActivity();
mma.switchContent(newContent);
}
}
return super.onOptionsItemSelected(item);
}
}
And then in the MenuMainActivity this method gets called:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
toggle();
return true;
case R.id.add_alarm:
return false;
}
return super.onOptionsItemSelected(item);
}
This unfortunately, is not working and the Fragment method doesn't get called.
What might the problem be?
Thanks in advance!
In your fragments onCreate make sure you're calling setHasOptionsMenu(true);
EDIT Please make sure your public boolean onOptionsItemSelected(MenuItem item) is actually public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item). Or update the import to this MenuItem
So I found my mistake. My Activity was extending a SherlockFragmentActivity, but the Fragment was extending normal Fragment instead of SherlockFragment. With the Fragment extending SherlockFragment it works now.
Thanks for help everyone!
I have a fragment that contains a ListView, when I try to show a DialogFragment on the top of it the selected list items are deselected. Is it possible to keep the items selected when the DialogFragment appears/disappears?
My Fragment's onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (DEBUG) {
Log.d(TAG, "BrowserFragment.onCreateView()");
}
View v = inflater.inflate(R.layout.fragment_filebrowser, container,
false);
listView = (ListView) v.findViewById(android.R.id.list);
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(this);
listView.setEmptyView(v.findViewById(android.R.id.empty));
// FOR CONTEXT ACTION MENU
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contexual, menu);
mode.setTitle("Choose Files");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
Log.d(TAG, "onDestroyActionMode!");
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
SimpleDialogFragment
.createBuilder(getActivity(),
getActivity().getSupportFragmentManager())
.setTitle(R.string.delete_files)
.setMessage(R.string.confirm_delete)
.setPositiveButtonText(R.string.yes)
.setNegativeButtonText(R.string.no).show();
mode.finish();
//The rest of the program..
Screenshots:
As you can see in the second screenshot, the listview's selected items have been deselected. How can I prevent that?
UPDATE: I'm using StyledDialogs library
Found the solution, the problem was that i was calling mode.finish() right after the dialogfragment.show(). I stored the ActionMode variable and used it inside my DialogFragments positive button callback to call the .finish() instead and everything is working correctly.