I have followed some steps on hiding and showing menu items. But unfortunately my app crashes. What could I be doing wrong?
This is my Activity:
private MenuItem menuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol_type_page);
invalidateOptionsMenu();
menuItem.setVisible(false);
}
//TOOLBAR SETTINGS
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_icon, menu);
menuItem = menu.findItem(R.id.view_orders);
return super.onCreateOptionsMenu(menu);
}
Thanks in advance for any help! :D
Update
private MenuItem menuItem;
private boolean menuItemShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol_type_page);
}
//TOOLBAR SETTINGS
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_icon, menu);
menuItem = menu.findItem(R.id.view_orders);
menuItem.setVisible(menuItemShow);
return super.onCreateOptionsMenu(menu);
}
onCreateOptionsMenu is invoked after onCreate,you shouldn't set menu item visibility in onCreate method.
Related
I am trying to hide/show menuItem but getting NPE. It works in onPrepareOptionsMenu but not in onCreate. By default it should be invisible.
e.g. in my fragment's onCreate i would like to do something like:
if (condition) menuItem.setvisible(true);
but it gets me NPE on that menuItem.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.inventory_list_toolbar, menu);
return true;
}
#Override
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_inventory);
mToolbar = findViewById(R.id.inventory_list_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
menuItem = menu.findItem(R.id.inventory_list_menu_add);
menuItem.setVisible(false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You can't hide menu from onCreate method since it's not inflated yet.
You should make your logic in onPrepareOptionsMenu method:
#Override
public void onPrepareOptionsMenu(Menu menu) {
if (condition) {
// do what you want
MenuItem menuItem = menu.findItem(R.id.menu_item_id);
menuItem.setvisible(true);
}
return super.onPrepareOptionsMenu(menu);
}
Otherwise, put your logic in onCreateOptionsMenu method and call invalidateOptionsMenu() when you need to refresh something.
private Menu menu;
Then initialize it as
#Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
When u want to hide/show the menu use
if(menu != null){
MenuItem item_up = menu.findItem(R.id.action_up);
item_up.setVisible(true);
}
public class Profile extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
}
#Override //my code for action bar but it makes the action bar missing
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Have you tried explicitly returning true?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
I'm inflating a menu with menu items in Activity's toolbar and overwriting the options in fragment.But the new menu items are not showing up.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.encounter_tabacivity_menu, menu);
return true;
}
Fragment
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.clear(); //remove all items
getActivity().getMenuInflater().inflate(R.menu.encounter_frag_problemlist_menu, menu);
}
#Override
public void onResume() {
super.onResume();
getActivity().invalidateOptionsMenu();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
view = inflater.inflate(R.layout.fragment_problem_list, container, false);
init();
return view;
}
You might not have set your toolbar as your actionbar. To view your menu in your actionbar declare your toolbar as actionbar as follows
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You need setHasOptionMenu(true) in your fragment.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You must override this in fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
Try this code in your Fragment :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(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_record ) {
// startActivity(new Intent(getActivity(),RecordingActivity.class));
startActivity(new Intent(getActivity(),RecordingTestActivity.class));
getActivity().finish();
return true;
}
if (id == R.id.action_search ) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.recording, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
} super.onCreateOptionsMenu(menu, inflater);
}
I add ActionBarCompat to my project based on this tutorial. But I get confused on how to give event for user input when search button is clicked.
public class MainActivity extends ActionBarActivity {
ActionBar ab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ab = getSupportActionBar();
ab.setTitle("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
}
And I found out that to get the input from user is with implements SearchView.OnQueryTextListener, as I found from here. Don't forget to add this listener to the SearchView at onCreateOptionsMenu(Menu menu) method, likt this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
return true;
}
Is that possible to have different menus for each tab of TabHost?
Yes, you can in onCreateOptionsMenu depending on the tab inflate a different menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
int tab = getTabHost().getCurrentTab()
if (tab==1)
inflater.inflate(R.menu.main_menu, menu);
else
inflater.inflate(R.menu.other_menu, menu);
return true;
}
You need to provide different versions of menu.xml files within res/menu for this.
If you're using fragments you can put setHasOptionsMenu(true); inside onCreate method of your fragment and override onCreateOptionsMenu.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_xxx, menu);
}