#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
inflater.inflate(R.menu.menuitem, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Below is my onCreateView method, where i am calling
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.layout1, container, false);
}
I don't get the log statements or the menu getting called in my action-bar.
Update: I tried calling this from onCreate method of fragment, yet the menu is not shown.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/section" android:title="#string/section"
android:icon="#drawable/ic_section"
android:showAsAction="always" />
<item android:id="#+id/refresh" android:title="#string/refresh"
android:icon="#drawable/ic_refresh"
android:showAsAction="always" />
<item android:id="#+id/edit_patient" android:title="#string/edit_patient"
android:icon="#drawable/ic_editpatient"
android:showAsAction="always" />
<item android:id="#+id/about" android:title="#string/about"
android:showAsAction="never" />
<item android:id="#+id/help" android:title="#string/help"
android:showAsAction="never" />
<item android:id="#+id/signout" android:title="#string/signout"
android:showAsAction="never" />
</menu>
You'll need to make a call of setHasOptionsMenu(true); from within one of the starting lifecycle methods of the Fragment. Preferably from within onCreate(...).
In a minimalistic case the onCreate method of your Fragment looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Also, calling super.onCreateOptionsMenu(menu, inflater); after you have inflated your custom menu will reset the menu you just have inflated to an empty menu.
So either call:
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menuitem, menu);
}
or:
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
//no super call
inflater.inflate(R.menu.menuitem, menu);
}
Also, if you're testing on a Gingerbread device, the menu might not be displayed if the hosting Activity does not contain a menu item of it's own.
Related
I want to load an activity and have the search bar visible right away with a magnifying glass available to click/search based on the term entered. Here is what I want to see when I load the activity:
Code:
public class FlickRActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flick_r);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getApplicationContext(),newText,Toast.LENGTH_SHORT).show();
return false;
}
});
return true;
}
}
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/search"
android:title="#string/hint_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Just call searchItem.expandActionView();
I followed this tutorial to build up a Sliding Tab application :
http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
Now my Question:
How can I change my Menu icons in my actionbar when I change to another Tab ?For example in Tab1 you can see a Search Icon and when you change to Tab2 this gets invisible but there comes a Add icon.
My code till now :
#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_main, menu);
menu.add("menu_search").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("menu_contact").setIcon(R.drawable.ic_add_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
You can define menu in Fragment instead of Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#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) {
switch (item.getItemId()) {
case R.id.menu_item_1:
// handling menu item click
break;
}
return super.onOptionsItemSelected(item);
}
Of course each Fragment will have its own menu with corresponding icons.
This is an example (menu_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_1"
android:showAsAction="ifRoom"
android:title="#string/ic_menu_label"
android:icon="#drawable/ic_menu_icon"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
I am trying to implement a SearchView in OptionsMenu of a Fragment. The onQueryTextChange and onQueryTextSubmit is not working. I am not sure where am I going wrong.
The menu file:
<?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/profile"
android:orderInCategory="10"
android:title="Profile"
app:showAsAction="never">
</item>
<item
android:id="#+id/settings"
android:orderInCategory="11"
android:title="Settings"
app:showAsAction="never">
</item>
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
The part in fragment where I am trying to access the text from SearchView:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.browse_story_fragment, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
SearchView.OnQueryTextListener listener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String query) {
query = query.toLowerCase();
Log.e("queryText",query);
return false;
}
public boolean onQueryTextSubmit(String query) {
Log.e("queryTextSubmit", query);
return false;
}
};
super.onCreateOptionsMenu(menu, inflater);
}
I do not see anything in the Logs. Where am I going wrong?
My big mistake! What I was thinking!
Here is the code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.browse_story_fragment, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// collapse the view ?
//menu.findItem(R.id.menu_search).collapseActionView();
Log.e("queryText",query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// search goes here !!
// listAdapter.getFilter().filter(query);
Log.e("queryText",newText);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
i have a problem.
I'm using support library for my app.
I add some Action items on my ActionBar by XML-file.
From docs:
https://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView" />
...
</menu>
It works. But the item stays on all fragments.
I need use different action items in ActionBar.
I created a few menu-files and tried to use them on different frŠ°gments, but it doesn't work.
I see only the first Action item from class extends ActionBarActivity.
From fragment:
public class Foods extends Fragment implements View.OnClickListener, SearchView.OnQueryTextListener
...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.showinfo_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
searchView.setQueryHint(getResources().getString(R.string.search));
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
I have just found my mistake.
If you want ActionBar items of fragmets to work, you should add it:
http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
...
The android native menu is not opening when pressing menu button while using PhoneGap. When I am pressing menu button, onCreateOptionsMenu() method is getting called but the menu is not getting created.
public class PhoneGapActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.setBooleanProperty("showTitle", true);
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
Log.d("Menu","Menu option called");
getMenuInflater().inflate(R.menu.phone_gap, menu);
return true;
}
}
The log is getting printed, that means the function is getting called.
XML file for menu: When the android:showAsAction is set to always its showing in the action bar, otherwise its not working.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_menu_settings_holo_light"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item
android:id="#+id/more"
android:icon="#drawable/ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="never"
android:title="#string/more"/>
</menu>
What could be the problem?
Solved it. The bug was in PhoneGap 2.6. I downloaded PhoneGap version 2.5 and it worked normally.