-1

How do I hide a search bar item or disable a search on fragment?

Here is what I have..

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Create the search view
        SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setQueryHint("What is your vehicle plate no.?");
        searchView.setOnQueryTextListener(this);
        searchView.setOnSuggestionListener(this);

        if (mSuggestionsAdapter == null) {
            MatrixCursor cursor = new MatrixCursor(COLUMNS);

            AutoCompleteTextView actv = new AutoCompleteTextView(this);
            actv.setThreshold(1);

/*            VehicleHolder vh = new VehicleHolder("");
            for (int i = 0; i < vh.getVehicle().length(); i++) {

                String is = String.valueOf(i);
//                cursor.addRow(new String[]{is, list_vehicle.get(0).get("plate_num").toString()});
                cursor.addRow(new String[]{is, list_vehicle.get(i).getVehicle().toString()});
            }*/

            mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar().getThemedContext(), cursor);
        }
        searchView.setSuggestionsAdapter(mSuggestionsAdapter);

        menu.add("Search")
                .setIcon(true ? R.drawable.ic_action_action_search : R.drawable.ic_action_action_search)
                .setActionView(searchView)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        return true;
    }

However, once the Fragment was called(Where I want it hide) it will automatically disable this function/search. Like a search icon hide.

user8256287
  • 177
  • 15

1 Answers1

1

you can hide it with searchView.setVisibility(View.GONE); for the menu: searchItem.setVisible(false);

YueMuc
  • 86
  • 1
  • 6
  • I don't want search view gone cause my main drawer is located on it. I just want to hide the particalur search icon. Could that be possible? – user8256287 Aug 10 '17 at 07:40
  • View.INVISIBLE keeps the space of the view. - searchItem.setVisible(false) hides the icon in the menu. – YueMuc Aug 10 '17 at 07:42
  • How can I achieve that? On fragment class I have only onCreate to put a code – user8256287 Aug 10 '17 at 07:44
  • Override method (rightclick android studio -> generate -> override), See fragment lifecircle here: https://i.stack.imgur.com/fRxIQ.png – YueMuc Aug 10 '17 at 07:46
  • you can override one of the methods that are called after "oncreate" like "onactivitycreated" and put your code in there. Just click generate-> override method. Then your code will be invoked at that point. – YueMuc Aug 10 '17 at 07:50
  • I cant find `searchItem` could it be `menu.setGroupVisible(View.GONE, true);` ?? – user8256287 Aug 10 '17 at 07:55
  • Also, what should I be overriding from the lists? https://postimg.org/image/4arw72lq7/ – user8256287 Aug 10 '17 at 07:57
  • give your menu item an ID at "menu.add()", see methods here: https://developer.android.com/reference/android/view/Menu.html and here: https://stackoverflow.com/questions/15191550/android-create-a-simple-menu-programmatically. Override onActivityCreated(), then get menu item with MenuItem menuItem = menu.get(id) , menuItem.setVisible(false) – YueMuc Aug 10 '17 at 08:05
  • How can I possibly get the id on the mainactivity on fragment activity? – user8256287 Aug 10 '17 at 08:15
  • https://stackoverflow.com/questions/24526783/android-how-to-access-actionbars-menu-items-in-fragment-class – YueMuc Aug 10 '17 at 08:19
  • or you notify the activity from the fragment that it's created https://stackoverflow.com/questions/14999698/android-how-to-notify-activity-when-fragments-views-are-ready – YueMuc Aug 10 '17 at 08:20
  • Okay, I'll set it as answered. Thanks – user8256287 Aug 10 '17 at 08:22