This is my code
#Override
public void onCreate(Bundle SavedInstanceState){
//Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.forecastfragment, menu);
}
public boolean onOptionItemSelected(MenuItem item){
//Handle action bar item clicks here. The action bar will
//automatically handle clicks on the Home/Up button, so long
//as you specify a parent activity in AndroidManifest.xml
int id=item.getItemId();
if (id==R.id.action_refresh){
FetchWeatherTask weatherTask=new FetchWeatherTask();
weatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
When I execute this I'm getting an " app.SuperNotCalledException" I saw answers for similar questions but I don't get it Pls all helps appreciated. Thank you.
You have to call super method from your parent class before overriding it.
#Override
public void onCreate(Bundle SavedInstanceState){
super.onCreate(savedInstanceState);
//Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
Hope this will be useful for you.
Related
I am displaying overflow menu list when we click the three dots. When I press home button and again when I launch the app, overflow menu list is still displaying. How to dismiss overflow menu list window?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v("RAMKUMARV ONCREATEOPTION", "RAMKUMARV");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_settings);
item.setVisible(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can have an activity field that stores the options/overflow menu whenever the onCreateOptionsMenu() gets triggered, and then use close() method to dismiss the menu when the home button is clicked i.e. in onUserLeaveHint().
public class MainActivity extends AppCompatActivity {
// Field to store the overflow menu
private Menu mOverflowMenu;
// omitted rest of your code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mOverflowMenu = menu;
// omitted rest of your code
return true;
}
// Dismissing the overflow menu on home button click
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
mOverflowMenu.close();
}
}
Declare the menu item like at MainActivity level, like this:
public class MainActivity extends AppCompatActivity {
private MenuItem overflowItem;
...
}
Now you should instanciate the object inside the onCreateOptionsMenu method, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
overflowItem = menu.findItem(R.id.action_settings);
overflowItem.setVisible(true);
return true;
}
Then you can use the onPause method to set the overflowItem not visible anymore:
#Override
public void onPause() {
super.onPause();
overflowItem.setVisible(false);
}
Finally remember to replace your MenuItem item object with overflowItem where it is used.
To dismiss the overflow menu you can simply call closeOptionsMenu() inside Activity.
closeOptionsMenu()
Progammatically closes the options menu. If the options menu is
already closed, this method does nothing.
You can call it in onPause() method like below:
#Override
public void onPause() {
super.onPause();
closeOptionsMenu();
}
or in case you want to call it only when the user presses the Home key you can use onUserLeaveHint() like below:
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
closeOptionsMenu();
}
My Fragment class:
Toolbar toolbar;
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
toolbar = getView().findViewById(R.id.toolbar3);
toolbar.inflateMenu(R.menu.menufragmentmain);
setHasOptionsMenu(true); //i also tried putting this function in oncreate function
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Toast.makeText(getActivity(), "i never enter this function also" , Toast.LENGTH_LONG).show();
super.onCreateOptionsMenu(menu,inflater);
inflater.inflate(R.menu.menufragmentmain, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getActivity(), "i never enter this function" , Toast.LENGTH_LONG).show();
//some switch cases.....
return super.onOptionsItemSelected(item);
}
I've got a stupid error which I cant find unfourtantly. I've got a simple toolbar for my fragment. In onViewCreated I inflate my actionbar menu with my toolbar.
The issue is that the functions onCreateOptionsMenu and 'onOptionsItemSelected' are never called. I've got no clue why.
Things i checked in other similar questions with the same issue:
I checked if my main activity has a onCreateOptionsMenu or'onOptionsItemSelected'. It doesn*t
Checked if my style class has NOT: android:theme="#android:style/Theme.Black.NoTitleBar
None of the points unfourtantly work. What did I miss. Do I need to check something else?
As per the Fragment-owned app bar guide which explains how to use a Fragment-owned Toolbar, you do not use any of the setHasOptionsMenu(), onCreateOptionsMenu(), or onOptionsItemSelected() APIs - those are only used for activity owned app bars.
Instead, you would follow the guide for handling menu click events by using the Toolbar APIs:
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
toolbar = getView().findViewById(R.id.toolbar3);
toolbar.inflateMenu(R.menu.menufragmentmain);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getActivity(), "onMenuItemClick called" , Toast.LENGTH_LONG).show();
//some switch cases.....
return true;
}
});
}
Set up the toolbar in the host Activity. Then override the menu handling in each Fragment wherever necessary.
Here's some generic scaffolding (with comments):
public class MyFragment extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Report that this Fragment has an options menu
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
// Inflate then menu, and THEN call super()
// Note the order of the invocations
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
// Handle menu item selections
final int itemId = item.getItemId();
switch(itemId) {
case R.id.my_menu_option_x: ...; return true;
case R.id.my_menu_option_y: ...; return true;
case R.id.my_menu_option_z: ...; return true;
default: return super.onOptionsItemSelected(item);
}
}
}
This is my scenario: I have three fragments and I only want to show an action bar in one of those fragments. In the activity that creates the fragments, I currently have this code which correctly creates the menu bar:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_view_report, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_view_report) {
Intent intent = new Intent(getApplicationContext(), ViewReportActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Then in one of the fragments of which I am trying to hide the menu bar I have this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_view_report).setVisible(false);
super.onPrepareOptionsMenu(menu);
}
Does anyone have any idea of what I am doing wrong or a suitable solution?
Thanks in advance
I implement a toolbar and replace it with my actionbar according to my requirments. Now I want to add an icon in toolbar inside a fragment. I tried onCreateOptionMenu() and pass my menu xml to it. But it wont work. I also tried some other things and googling but nothing works so far. Does anybody have any idea about this. Here is my 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 the layout for this 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. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a 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 is selected!", Toast.LENGTH_SHORT).show();
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
Here is my 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 your Fragment, in the onCreateView() method, you have to add:
setHasOptionsMenu(true);
Otherwise the onCreateOptionsMenu() is never called.
you may need to use invalidateOptionsMenu() to tell the fragment to redraw the menu actions like this ...
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().invalidateOptionsMenu();
setHasOptionsMenu(true);
}
I am not getting how to show only selected action items on some activities. Below is my code:
public abstract class BaseActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 0:
Intent loginIntent = new Intent(BaseActivity.this,LoginForm.class);
startActivity(loginIntent);
return true;
case 1:
Intent saveIntent = new Intent(BaseActivity.this,SaveForm.class);
startActivity(saveIntent);
return true;
}
return false;
}
}
In the above code I am setting action items so that I can reuse this action items in some activities without repeating the code. Whenever I want these action items to be displayed on particular activity, I am extending this BaseActivity on the present activity as below.
public class FirstActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
....
....
}
So, whenever I need these action items on the particular activities, I am extending the baseactivity to the activities on which I need. Now my problem is how to set only selected action items on particular activity. Suppose I don't want to show up "login" action Item on one activity. How can I get this done? Code snippets would be appreciated.
Now it's a reasonable approach but I would only have items that were reused for all in the baseactivity. That said I think the correct way of handling this would be to override the methods with the implementation you want in these other activities.
You could have some variables that is set in onCreate(), if these are true/false handle it accordingly in onCreateOptionsMenu()
Edit
To override in your own activity you simply do the same thing as in the baseactivity but without calling initializing those buttons you don't need.
If you instead want to make toggle if buttons should be there or not simply add the variables to your baseactivity:
protected boolean mIsLoginButton = true;
Change the onCreateOptionsMenu() method in baseactivity to something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(mIsLoginButton) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Then in your firstActivity you simply do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIsLoginButton = false;
}