Using onOptionsItemSelected(MenuItem item) to change view to a different Tab - android

I have an app that has 4 (0,1,2,3) Tabs and Fragments. My Actionbar has a menu item called "settings". When settings is clicked I want to navigate from the current Tab/Fragment to Fragment(3). In each of my java files associated with a fragment I am using the code below to handle menu clicks. I can launch an Intent successfully with this code but how can I navigate to another tab/fragment on menu item click? Thanks!
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
My actual code.
public class event extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.event, container, false);
setHasOptionsMenu(true);
return view;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.notify:
Log.i("4", "Notify pressed");
return true;
case R.id.action_settings:
Log.i("4", "Settings pressed");
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
}
}

If you're using a ViewPager then it's as simple as setCurrentItem(int currentItem)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
// ...
case R.id.settings:
((ViewPager)findViewById(R.id.of_view_pager)).setCurrentItem(3);
return true;
// ...
}
}
Edit in response to:
When I add that code some elements show up in red indicating I am doing something wrong. "findViewById" and "of_view_pager" are in red.
Sounds like you are in a Fragment and not an Activity in which case you probably want to find the ViewPager in your onCreateView method and store it in a field for later use in your onOptionsItemSelected method.
Something like this:
private ViewPager m_viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_changelog, container, false);
// Other stuff already in your `onCreateView`
m_viewPager = (ViewPager) view.findViewById(R.id.of_view_pager);
return view;
}
The of_view_pager is in red because that id doesn't exist. You need to replace it with the correct id of your ViewPager that you specified in the layout.

Ok, I know where I went wrong. I had a main activity and a fragment for each tab in my application. I had two action bar menus one included in the main activity and one custom menu for fragments. The code I was trying to run for switching tabs by clicking a menu button did not work if the onOptionsItemSelected code was in the fragment file. When I put the code in the main activity it worked fine.

Related

When press back redrawing the actionbar menu

I added a refresh button to action-bar. When fragment loading that refresh button is creating in the action-bar. I have more than one fragment in my application, problem is when move between fragments and go back using back button using back button, action bar menu refresh button drawing several times. like this
following code I'm using
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_seebo_tv, container, false);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.seebo_tv, menu);
}
in main activity
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.action_refresh:
fragment=new ProgressFragment();
FragmentManager fragmentManagerProgress = getFragmentManager();
fragmentManagerProgress.beginTransaction().replace(R.id.container, fragment).addToBackStack(null).commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
how can I fix this?
Try moving the call to setHasOptionsMenu(true); from onCreateView to onCreate instead.
Here's an example of a fragment adding a menu item: https://stackoverflow.com/a/31935887/798464

onOptionsItemsSelected not being used in Fragment

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

Action Bar menu in Fragment doesn't appear

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?

How do you set a Menu item listener (onMenuItemSelected) in a FragmentPagerAdapter?

I'm trying to have a Fragment inside a FragmentPagerAdapter have its own onclicklistener for a MenuItem.
In other words, I'm trying to use an Activity's onOptionsItemSelected method inside a Fragment
How do I do this?
I've tried using onContextItemSelected(MenuItem theItem) and onOptionsItemSelected(MenuItem theItem) to no avail
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
…
setHasOptionsMenu(true);
…
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.your_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.your_key_id:
saveRoute();
break;
default:
break;
}
return false;
}
Use this if you need one menu for all the pages.
Edit:
If you need a specific menu on a specific page, see this: Android: Showing Action Bar menu items depending on ViewPager

Android: onOptionsItemSelected doesn't get called from the Fragment

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!

Categories

Resources