android.support.v4.view.MenuItemCompat Java Examples

The following examples show how to use android.support.v4.view.MenuItemCompat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DetailActivity.java    From android-weather with MIT License 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.detail, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.menu_item_share);
    // Fetch and store ShareActionProvider
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(getDefaultShareIntent());
    }
    else {
        Log.d(LOG_TAG, "ShareActionProvider is null");
    }
    return true;
}
 
Example #2
Source File: RecentActivity.java    From document-viewer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
 */
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.recentmenu, menu);

    MenuItem searchItem = menu.findItem(R.id.recentmenu_searchBook);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            ActionEx a = getController().getOrCreateAction(R.id.actions_searchBook);
            a.addParameter(new Constant("input", new SpannableStringBuilder(query)));
            a.run();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    return true;
}
 
Example #3
Source File: MainActivity.java    From ListViewVariants with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menuItem_search));
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(final String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(final String newText) {
            performSearch(newText);
            return true;
        }
    });

    return super.onCreateOptionsMenu(menu);
}
 
Example #4
Source File: RideDetailActivity.java    From android-ponewheel with MIT License 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.ride_menu, menu);

    // store action provider
    shareMenuItem = menu.findItem(R.id.menu_item_share);
    shareMenuItem.setVisible(false);
    shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);

    // Create share intent
    shareFileIntent = new Intent();
    shareFileIntent.setAction(Intent.ACTION_SEND);
    shareFileIntent.setType("text/csv");
    shareFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    setShareIntent(shareFileIntent);

    return true;
}
 
Example #5
Source File: LogActivity.java    From COCOFramework with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem item = menu.add(0, ACTION_FILTER, 0, "Filter");
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_NEVER);

    item = menu.add(0, ACTION_LEVEL, 0, "Level");
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_NEVER);

    item = menu.add(0, ACTION_CLEAR, 0, "Clear");
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_NEVER);

    item = menu.add(0, ACTION_SHARE, 0, "Share");
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_NEVER);

    return true;
}
 
Example #6
Source File: MainActivity.java    From openwebnet-android with MIT License 6 votes vote down vote up
private void addEnvironmentMenu(Menu menuGroup, List<EnvironmentModel> environments) {
    log.debug("reloadMenu: {}", environments);
    final AtomicInteger menuOrder = new AtomicInteger(MENU_ENVIRONMENT_RANGE_MIN);

    Stream.of(environments)
        .forEach(environment -> {
            menuGroup.add(R.id.nav_group_environment, environment.getId(),
                menuOrder.getAndIncrement(), environment.getName());

            // edit menu
            MenuItem menuItem = MenuItemCompat.setActionView(menuGroup.findItem(environment.getId()),
                R.layout.drawer_menu_environment);
            menuItem.getActionView().findViewById(R.id.imageViewDrawerMenuEnvironmentEdit)
                .setOnClickListener(new NavigationViewClickListener(this, environment.getId()));
        });
}
 
Example #7
Source File: BottomToolbar.java    From android_maplibui with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void inflateMenu(@MenuRes int resId) {
    super.inflateMenu(resId);
    Menu menu = getMenu();
    MenuItem item = menu.getItem(0);
    int size = item.getIcon().getIntrinsicWidth() + ControlHelper.dpToPx(30, getResources());
    int width = getWidth();

    for (int i = 0; i < menu.size(); i++) {
        item = menu.getItem(i);
        if (size * (i + 2) < width)
            MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        else
            break;
    }
}
 
Example #8
Source File: MusicContainerListActivity.java    From PlayMusicExporter with MIT License 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.music_track_list, menu);

    // Finds the search item and create the search view
    MenuItem itemSearch = menu.findItem(R.id.action_search);
    mSearchView = (SearchView)MenuItemCompat.getActionView(itemSearch);

    if (mSearchView != null) {
        // Sets the search listener
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setIconifiedByDefault(true);

        // Hack to change the default placeholder
        EditText searchEditText = (EditText) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchEditText.setHint(R.string.search);
    }

    return true;
}
 
Example #9
Source File: ZhuanTiActivity.java    From MaterialWpp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (null != manager) {
                manager.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            }
            Intent intent = new Intent(ZhuanTiActivity.this, SearchActivity.class);
            intent.putExtra("keyword",query);
            startActivity(intent);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    return true;
}
 
Example #10
Source File: Main.java    From redalert-android with Apache License 2.0 6 votes vote down vote up
void initializeSettingsButton(Menu OptionsMenu) {
    // Add refresh in Action Bar
    MenuItem settingsItem = OptionsMenu.add(Menu.NONE, Menu.NONE, Menu.NONE, getString(R.string.settings));

    // Set up the view
    settingsItem.setIcon(R.drawable.ic_settings);

    // Specify the show flags
    MenuItemCompat.setShowAsAction(settingsItem, MenuItem.SHOW_AS_ACTION_ALWAYS);

    // On click, go to Settings
    settingsItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Start settings activity
            goToSettings(false);

            // Consume event
            return true;
        }
    });
}
 
Example #11
Source File: MainActivity.java    From MaterialWpp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (null != manager) {
                manager.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            }
            Intent intent = new Intent(MainActivity.this, SearchActivity.class);
            intent.putExtra("keyword",query);
            startActivity(intent);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    return true;
}
 
Example #12
Source File: MainActivity.java    From VCL-Android with Apache License 2.0 6 votes vote down vote up
/** Create menu from XML
 */
@TargetApi(Build.VERSION_CODES.FROYO)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mMenu = menu;
    /* Note: on Android 3.0+ with an action bar this method
     * is called while the view is created. This can happen
     * any time after onCreate.
     */
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.media_library, menu);

    if (AndroidUtil.isFroyoOrLater()) {
        SearchManager searchManager =
                (SearchManager) VLCApplication.getAppContext().getSystemService(Context.SEARCH_SERVICE);
        mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.ml_menu_search));
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        mSearchView.setQueryHint(getString(R.string.search_hint));
        SearchSuggestionsAdapter searchSuggestionsAdapter = new SearchSuggestionsAdapter(this, null);
        searchSuggestionsAdapter.setFilterQueryProvider(this);
        mSearchView.setSuggestionsAdapter(searchSuggestionsAdapter);
    } else
        menu.findItem(R.id.ml_menu_search).setVisible(false);
    return super.onCreateOptionsMenu(menu);
}
 
Example #13
Source File: SearchActivity.java    From Toutiao with Apache License 2.0 6 votes vote down vote up
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        searchView = (SearchView) MenuItemCompat.getActionView(item);
        // 关联检索配置与 SearchActivity
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(
                new ComponentName(getApplicationContext(), SearchActivity.class));
        searchView.setSearchableInfo(searchableInfo);
        searchView.onActionViewExpanded();
//        // 设置搜索文字样式
//        EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//        searchEditText.setTextColor(getResources().getColor(R.color.textColorPrimary));
//        searchEditText.setHintTextColor(getResources().getColor(R.color.textColorPrimary));
//        searchEditText.setBackgroundColor(Color.WHITE);
        setOnQuenyTextChangeListener();

        return super.onCreateOptionsMenu(menu);
    }
 
Example #14
Source File: ActivityMain.java    From Android-Firewall with GNU General Public License v3.0 6 votes vote down vote up
private void fillApplicationList() {
    // Get recycler view
    final RecyclerView rvApplication = (RecyclerView) findViewById(R.id.rvApplication);
    rvApplication.setHasFixedSize(true);
    rvApplication.setLayoutManager(new LinearLayoutManager(this));

    // Get/set application list
    new AsyncTask<Object, Object, List<Rule>>() {
        @Override
        protected List<Rule> doInBackground(Object... arg) {
            return Rule.getRules(ActivityMain.this);
        }

        @Override
        protected void onPostExecute(List<Rule> result) {
            if (running) {
                if (searchItem != null)
                    MenuItemCompat.collapseActionView(searchItem);
                adapter = new RuleAdapter(result, ActivityMain.this);
                rvApplication.setAdapter(adapter);
            }
        }
    }.execute();
}
 
Example #15
Source File: MainActivity.java    From MaterialHome with Apache License 2.0 6 votes vote down vote up
private void initNavView() {
    boolean night = SPUtils.getPrefBoolean(Constant.THEME_MODEL, false);
    if (night) {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
    MenuItem item = mNavigationView.getMenu().findItem(R.id.nav_theme);
    mNavigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
    mThemeSwitch = (SwitchCompat) MenuItemCompat.getActionView(item).findViewById(R.id.view_switch);
    mThemeSwitch.setChecked(night);
    mThemeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SPUtils.setPrefBoolean(Constant.THEME_MODEL, isChecked);
            mThemeSwitch.setChecked(isChecked);
            if (isChecked) {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }
    });
}
 
Example #16
Source File: ImageViewerActivity.java    From android-imageviewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    // Locate MenuItem with ShareActionProvider
    MenuItem itemShare = menu.findItem(R.id.action_share);
    // Get the provider and hold onto it to set/change the share intent.
    shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(itemShare);
    // Set history different from the default before getting the action
    // view since a call to MenuItemCompat.getActionView() calls
    // onCreateActionView() which uses the backing file name. Omit this
    // line if using the default share history file is desired.
    shareActionProvider.setShareHistoryFileName("snowdream_android_imageviewer_share_history.xml");
    Intent shareIntent = createShareIntent();
    if (shareIntent != null) {
        doShare(shareIntent);
    }
    return true;
}
 
Example #17
Source File: MainActivity.java    From FaceT with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onBackPressed() {
    super.onBackPressed();
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else if (drawerLayout.isDrawerOpen(GravityCompat.END)) {  /*Closes the Appropriate Drawer*/
        drawerLayout.closeDrawer(GravityCompat.END);
    } else {
        this.finish();
    }

    if (searchView != null && !searchView.isIconified()) {
        MenuItemCompat.collapseActionView(mMenuItem);
        return;
    }
}
 
Example #18
Source File: DetailFragment.java    From Krishi-Seva with MIT License 6 votes vote down vote up
@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.detailfragment, menu);

    // Retrieve the share menu item
    MenuItem menuItem = menu.findItem(R.id.action_share);

    // Get the provider and hold onto it to set/change the share intent.
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

    // If onLoadFinished happens before this, we can go ahead and set the share intent now.
    if (mForecast != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
    }
}
 
Example #19
Source File: MainActivity.java    From android-MediaRouter with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Be sure to call the super class.
    super.onCreateOptionsMenu(menu);

    // Inflate the menu and configure the media router action provider.
    getMenuInflater().inflate(R.menu.sample_media_router_menu, menu);

    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider =
            (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
    mediaRouteActionProvider.setRouteSelector(mSelector);

    // Return true to show the menu.
    return true;
}
 
Example #20
Source File: SearchActivity.java    From aMuleRemote with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    
    if (mApp != null && DEBUG) Log.d(TAG, "SearchActivity.onPrepareOptionsMenu: Setting items visibility");
    
    if (refreshItem != null)  {
        
        if (lastSearch != null && (lastSearch.mSearchStatus == ECSearchStatus.STARTING || lastSearch.mSearchStatus == ECSearchStatus.RUNNING)) {
            if (mIsProgressShown) {
                MenuItemCompat.setActionView(refreshItem, R.layout.part_refresh_progress);
                //refreshItem.setActionView(R.layout.part_refresh_progress);
            } else {
                MenuItemCompat.setActionView(refreshItem, null);
                //refreshItem.setActionView(null);
            }
            refreshItem.setVisible(true);
        } else {
            refreshItem.setVisible(false);
        }
    }
    
    if (mApp != null && DEBUG) Log.d(TAG, "SearchActivity.onPrepareOptionsMenu: calling super");
    boolean superRet = super.onPrepareOptionsMenu(menu);
    if (mApp != null && DEBUG) Log.d(TAG, "SearchActivity.onPrepareOptionsMenu: super returned " + superRet + " - end");
    return superRet;
}
 
Example #21
Source File: ReceiptViewActivity.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.share_menu, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.action_share);

    // Fetch ShareActionProvider
    ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
    if (shareActionProvider == null)
    {
        Log.w(TAG, "Failed to find share action provider");
        return false;
    }

    if(receiptFilename == null)
    {
        Log.w(TAG, "No receipt to share");
        return false;
    }

    Intent shareIntent = new Intent(Intent.ACTION_SEND);

    // Determine mimetype of image
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(receiptFilename, opt);
    shareIntent.setType(opt.outMimeType);

    Uri outputUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, new File(receiptFilename));
    shareIntent.putExtra(Intent.EXTRA_STREAM, outputUri);

    // set flag to give temporary permission to external app to use the FileProvider
    shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    shareActionProvider.setShareIntent(shareIntent);

    return super.onCreateOptionsMenu(menu);
}
 
Example #22
Source File: PagedRecyclerViewFragment.java    From COCOFramework with Apache License 2.0 5 votes vote down vote up
@Override
public void onLoadFinished(final Loader<List<T>> loader, final List<T> items) {
    final Exception exception = getException(loader);
    onStopLoading();
    if (refresh != null) {
        MenuItemCompat.setActionView(refresh,null);
    }
    if (exception != null) {
        ended.set(true);
        showError(exception);
        showList();
        return;
    }
    if (items != null && mAdapter != null && !items.isEmpty()) {
        if (time == 0)
            getAdapter().refresh();
        getAdapter().add(items);
    }
    showList();
    updateAdapter();
    onLoaderDone(items);

    showLoading();
    if (items == null || items.size() < pagedSize(time)) {
        ended.set(true);
        onAllDataLoaded();
        hideLoading();
    }

    time++;
}
 
Example #23
Source File: GenericStatusLayoutDemo.java    From MaskEverywhere with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuItemCompat.setShowAsAction(menu.add(0,0,0,"loading"),MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    MenuItemCompat.setShowAsAction(menu.add(0,1,0,"empty"),MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    MenuItemCompat.setShowAsAction(menu.add(0,2,0,"error"),MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}
 
Example #24
Source File: ArticleDetailActivity.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_thread_detail, menu);

    MenuItem author = menu.findItem(R.id.action_author);
    author.setVisible(false);

    MenuItem item = menu.findItem(R.id.action_more);
    MoreActionProvider provider = (MoreActionProvider) MenuItemCompat.getActionProvider(item);
    provider.setCallback(this, this);
    return true;
}
 
Example #25
Source File: GlideDualImageFragment.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	super.onCreateOptionsMenu(menu, inflater);
	MenuItem clearImage1 =
			menu.add(0, 9, 0, "Clear image 1").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
	MenuItem clearImage2 =
			menu.add(0, 10, 0, "Clear image 2").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
	MenuItemCompat.setShowAsAction(clearImage1, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
	MenuItemCompat.setShowAsAction(clearImage2, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
 
Example #26
Source File: OCFileListFragment.java    From Cirrus_depricated with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    searchItem = menu.findItem(R.id.action_search);
    layoutView = menu.findItem(R.id.action_changeView);

    // create search bar and search listener
    if (searchItem != null) {
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(this);
    }
}
 
Example #27
Source File: SwapWorkflowActivity.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
private void setUpSearchView(Menu menu) {
    SearchView searchView = new SearchView(this);

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    MenuItemCompat.setActionView(searchMenuItem, searchView);
    MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String newText) {
            String currentFilterString = currentView.getCurrentFilterString();
            String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
            if (currentFilterString == null && newFilter == null) {
                return true;
            }
            if (currentFilterString != null && currentFilterString.equals(newFilter)) {
                return true;
            }
            currentView.setCurrentFilterString(newFilter);
            if (currentView instanceof SelectAppsView) {
                getSupportLoaderManager().restartLoader(currentView.getLayoutResId(), null,
                        (SelectAppsView) currentView);
            } else if (currentView instanceof SwapSuccessView) {
                getSupportLoaderManager().restartLoader(currentView.getLayoutResId(), null,
                        (SwapSuccessView) currentView);
            } else {
                throw new IllegalStateException(currentView.getClass() + " does not have Loader!");
            }
            return true;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return true;
        }
    });
}
 
Example #28
Source File: AlertView.java    From redalert-android with Apache License 2.0 5 votes vote down vote up
void initializeShareButton(Menu OptionsMenu) {
    // Add refresh in Action Bar
    MenuItem shareItem = OptionsMenu.add(Menu.NONE, Menu.NONE, Menu.NONE, getString(R.string.shareAlert));

    // Set up the view
    shareItem.setIcon(R.drawable.ic_share);

    // Specify the show flags
    MenuItemCompat.setShowAsAction(shareItem, MenuItem.SHOW_AS_ACTION_ALWAYS);

    // On click, open share
    shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Prepare share intent
            Intent shareIntent = new Intent(Intent.ACTION_SEND);

            // Set as text/plain
            shareIntent.setType("text/plain");

            // Add text
            shareIntent.putExtra(Intent.EXTRA_TEXT, getShareMessage());

            // Show chooser
            startActivity(Intent.createChooser(shareIntent, getString(R.string.shareAlert)));

            // Consume event
            return true;
        }
    });
}
 
Example #29
Source File: BaseFragment.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setHasSearchView(IOnSearchViewChangeListener listener,
                             Menu menu, int menu_id) {
    mOnSearchViewChangeListener = listener;
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu
            .findItem(menu_id));
    if (mSearchView != null) {
        mSearchView.setOnCloseListener(closeListener);
        mSearchView.setOnQueryTextListener(searchViewQueryListener);
        mSearchView.setIconifiedByDefault(true);
    }
}
 
Example #30
Source File: BuildpropFragment.java    From kernel_adiutor with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onBackPressed() {
    if (searchItem != null && MenuItemCompat.isActionViewExpanded(searchItem)) {
        MenuItemCompat.collapseActionView(searchItem);
        return true;
    }
    return false;
}