Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalidateOptionsMenu doesn't work in fragment

Tags:

I want to show or hide item in actionbar according to either their is text in the edit text or not

so I did the following

            public class NounSearch extends  android.app.Fragment  {  EditText seachEditText;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,             Bundle savedInstanceState)     {             super.onCreate(savedInstanceState);             View rootView = inflater.inflate(R.layout.nounsearchactivity, container, false);                     //Intiate EditText             seachEditText =(EditText) rootView.findViewById(R.id.nounSearch);             seachEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {                 @Override                 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                     if (actionId == EditorInfo.IME_ACTION_SEARCH) {                         searchResult.Entities = new ArrayList<NounModel>();                         _currentPage = 0;                         categoryId = -1;                         new GetNouns().execute();                         return true;                     }                       return false;                 }             });             seachEditText.addTextChangedListener(new TextWatcher() {                 public void afterTextChanged(Editable s) {                }                 public void beforeTextChanged(CharSequence s, int start,                  int count, int after) {                }                 public void onTextChanged(CharSequence s, int start,                  int before, int count) {                  getActivity().invalidateOptionsMenu();                }               });      }     @Override     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {         super.onCreateOptionsMenu(menu, inflater);              if(seachEditText.getText().toString().length() > 0)             {                 menu.findItem(R.id.action_search).setVisible(true);             }             else             {                 menu.findItem(R.id.action_search).setVisible(false);             }     }      } 

but the actionitem never appear

like image 812
AMH Avatar asked Aug 09 '14 06:08

AMH


People also ask

What is invalidateOptionsMenu?

Edit: Here is a better answer to the question. A good use for invalidateOptionsMenu() is when we have a ListView and Delete All MenuItem so when the ListView is empty we should use invalidateOptionsMenu() to remove the Delete All MenuItem .

What is onPrepareOptionsMenu Android?

Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.


2 Answers

For updating the onCreateOptionsMenu inside the fragment you need to call the setHasOptionsMenu(true); inside the onCreate method of the fragment. Otherwise you won't be able to update it when you call getActivity().invalidateOptionsMenu();

sample:

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setHasOptionsMenu(true); } 

EDIT:

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {         if(seachEditText.getText().toString().length() > 0)         {             menu.findItem(R.id.action_search).setVisible(true);         }         else         {             menu.findItem(R.id.action_search).setVisible(false);         }     super.onCreateOptionsMenu(menu, inflater);   } 
like image 80
Rod_Algonquin Avatar answered Sep 18 '22 13:09

Rod_Algonquin


Try this:

@Override public boolean onPrepareOptionsMenu(Menu menu, MenuInflater inflater) {     super.onPrepareOptionsMenu(menu, inflater);     if (seachEditText.getText().toString().length() > 0) {         menu.findItem(R.id.action_search).setVisible(true);     } else {         menu.findItem(R.id.action_search).setVisible(false);     } } 
like image 32
Akash Moradiya Avatar answered Sep 19 '22 13:09

Akash Moradiya