MenuItem setIcon - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for MenuItem setIcon method.

This code example shows how to use the following methods:setActionView, setIcon, setShowAsAction
    } 
 
    @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Place an action bar item for searching. 
        MenuItem item = menu.add("Search");
        item.setIcon(android.R.drawable.ic_menu_search);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
                | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        SearchView sv = new SearchView(getActivity());
        sv.setOnQueryTextListener(this);
        item.setActionView(sv);
    } 
 
    public boolean onQueryTextChange(String newText) {
        // Called when the action bar search text has changed.  Update 
        // the search filter, and restart the loader to do a new query 
        // with this filter. 
        mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
        getLoaderManager().restartLoader(0, null, this);
        return true; 
7
CodeRank
This code example shows how to use the following methods:setActionView, setIcon, setShowAsAction
        } 
 
        @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // Place an action bar item for searching. 
            MenuItem item = menu.add("Search");
            item.setIcon(android.R.drawable.ic_menu_search);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
            SearchView sv = new SearchView(getActivity());
            sv.setOnQueryTextListener(this);
            item.setActionView(sv);
        } 
 
        public boolean onQueryTextChange(String newText) {
            // Called when the action bar search text has changed.  Update 
            // the search filter, and restart the loader to do a new query 
            // with this filter. 
            String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
            // Don't do anything if the filter hasn't actually changed. 
            // Prevents restarting the loader when restoring state. 
7
CodeRank
This code example shows how to use the following methods:setActionView, setIcon, setShowAsAction
        } 
 
        @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // Place an action bar item for searching. 
            MenuItem item = menu.add("Search");
            item.setIcon(android.R.drawable.ic_menu_search);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
            SearchView sv = new SearchView(getActivity());
            sv.setOnQueryTextListener(this);
            item.setActionView(sv);
        } 
 
        public boolean onQueryTextChange(String newText) {
            // Called when the action bar search text has changed.  Update 
            // the search filter, and restart the loader to do a new query 
            // with this filter. 
            String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
            // Don't do anything if the filter hasn't actually changed. 
            // Prevents restarting the loader when restoring state. 
7
CodeRank
This code example shows how to use the following methods:getActionView, isEnabled, isVisible, setActionView, setEnabled
	@Override 
	public View getActionView() {
		if (Build.VERSION.SDK_INT < 11) {
			return compatActionView;
		} else { 
			return menuItem.getActionView();
		} 
	} 
 
	@Override 
	public char getAlphabeticShortcut() { 
		return menuItem.getAlphabeticShortcut();
	} 
 
	@Override 
	public int getGroupId() { 
		return menuItem.getGroupId();
	} 
 
	@Override 

7
CodeRank
This code example shows how to use the following methods:setIcon, setShowAsAction
        MenuItem actionItem = menu.add("Action Button");
 
        // Items that show as actions should favor the "if room" setting, which will 
        // prevent too many buttons from crowding the bar. Extra items will show in the 
        // overflow area. 
        actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
 
        // Items that show as actions are strongly encouraged to use an icon. 
        // These icons are shown without a text description, and therefore should 
        // be sufficiently descriptive on their own. 
        actionItem.setIcon(android.R.drawable.ic_menu_share);
 
        return true; 
    } 
 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
        Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
        return true; 
    } 
6
CodeRank
This code example shows how to use the following methods:setActionView, setIcon, setShowAsAction
        } 
 
        @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // Place an action bar item for searching. 
            MenuItem item = menu.add("Search");
            item.setIcon(android.R.drawable.ic_menu_search);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            SearchView sv = new SearchView(getActivity());
            sv.setOnQueryTextListener(this);
            item.setActionView(sv);
        } 
 
        @Override public boolean onQueryTextChange(String newText) {
            // Called when the action bar search text has changed.  Since this 
            // is a simple array adapter, we can just have it do the filtering. 
            mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
            mAdapter.getFilter().filter(mCurFilter);
            return true; 
        } 
 
6
CodeRank
This code example shows how to use the following methods:setIcon
 
	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem helpItem = menu.add(0, 0, 0, "Home");
		helpItem.setIcon(android.R.drawable.ic_menu_edit);
		return true; 
	} 
 
	@Override 
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0: 
			Intent i = new Intent(Success.this, Home.class);
			startActivity(i);
			return true; 
		} 
		return false; 
	} 
 
6
CodeRank
This code example shows how to use the following methods:setActionView, setIcon, setShowAsAction
    } 
     
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuItem searchItem = menu.add("Search");
    	searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    	searchItem.setIcon(android.R.drawable.ic_menu_search);
    	SearchView search = new SearchView(this);
    	searchItem.setActionView(search);
    	 
    	return true; 
    } 
     
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
    	return true; 
    } 
     
    private TabSpec newTab(String tag) {
    	 
6
CodeRank
This code example shows how to use the following methods:setIcon
	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem homeItem = menu.add(0, 0, 0, "Home");
		MenuItem aboutItem = menu.add(0, 1, 0, "About");
		homeItem.setIcon(android.R.drawable.ic_menu_edit);
		aboutItem.setIcon(android.R.drawable.ic_menu_info_details);
 
		return true; 
	} 
 
	@Override 
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0: 
			Intent i = new Intent(Help.this, Home.class);
			if (extras != null) {
				i.putExtras(extras);
			} 
			startActivity(i);
6
CodeRank
This code example shows how to use the following methods:setIcon
	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem homeItem = menu.add(0, 0, 0, "Home");
		MenuItem aboutItem = menu.add(0, 1, 0, "Help");
		aboutItem.setIcon(android.R.drawable.ic_menu_info_details);
		homeItem.setIcon(android.R.drawable.ic_menu_edit);
		return true; 
	} 
 
	@Override 
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0: 
			Intent i = new Intent(About.this, Home.class);
			if (extras != null) {
				i.putExtras(extras);
			} 
			startActivity(i);
			return true; 
6
CodeRank

Related MenuItem setIcon Questions & Answers:

setIcon Method Overview
Change the icon associated with this item.
See Code Examples for other Android MenuItem Methods: