Example usage for android.view MenuInflater inflate

List of usage examples for android.view MenuInflater inflate

Introduction

In this page you can find the example usage for android.view MenuInflater inflate.

Prototype

public void inflate(@MenuRes int menuRes, Menu menu) 

Source Link

Document

Inflate a menu hierarchy from the specified XML resource.

Usage

From source file:com.andrewshu.android.reddit.submit.SubmitLinkActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.submit_link, menu);

    return true;//from   w  w w . ja va2s  .  c  o m
}

From source file:br.com.thinkti.android.filechooserfrag.fragFileChooserQuizlet.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    if (info.position > 0) {
        MenuInflater inflater = _main.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
    }/*from   w  w  w  . ja  va 2  s. c  o m*/
}

From source file:com.android.contacts.activities.ContactSelectionActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);

    final MenuItem searchItem = menu.findItem(R.id.menu_search);
    searchItem.setVisible(!mIsSearchMode && mIsSearchSupported);

    final Drawable searchIcon = searchItem.getIcon();
    if (searchIcon != null) {
        searchIcon.mutate().setColorFilter(ContextCompat.getColor(this, R.color.actionbar_icon_color),
                PorterDuff.Mode.SRC_ATOP);
    }//  w w w.  ja va 2  s.c  o m
    return true;
}

From source file:com.anjalimacwan.fragment.NoteListFragment.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void listNotes() {
    // Get number of files
    int numOfFiles = getNumOfNotes(getActivity().getFilesDir());
    int numOfNotes = numOfFiles;

    // Get array of file names
    String[] listOfFiles = getListOfNotes(getActivity().getFilesDir());
    ArrayList<String> listOfNotes = new ArrayList<>();

    // Remove any files from the list that aren't notes
    for (int i = 0; i < numOfFiles; i++) {
        if (NumberUtils.isNumber(listOfFiles[i]))
            listOfNotes.add(listOfFiles[i]);
        else//  w w w  .  j a v a  2  s. c  o  m
            numOfNotes--;
    }

    // Declare ListView
    final ListView listView = (ListView) getActivity().findViewById(R.id.listView1);

    // Create arrays of note lists
    String[] listOfNotesByDate = new String[numOfNotes];
    String[] listOfNotesByName = new String[numOfNotes];

    NoteListItem[] listOfTitlesByDate = new NoteListItem[numOfNotes];
    NoteListItem[] listOfTitlesByName = new NoteListItem[numOfNotes];

    ArrayList<NoteListItem> list = new ArrayList<>(numOfNotes);

    for (int i = 0; i < numOfNotes; i++) {
        listOfNotesByDate[i] = listOfNotes.get(i);
    }

    // If sort-by is "by date", sort in reverse order
    if (sortBy.equals("date"))
        Arrays.sort(listOfNotesByDate, Collections.reverseOrder());

    // Get array of first lines of each note
    for (int i = 0; i < numOfNotes; i++) {
        try {
            String title = listener.loadNoteTitle(listOfNotesByDate[i]);
            String date = listener.loadNoteDate(listOfNotesByDate[i]);
            listOfTitlesByDate[i] = new NoteListItem(title, date);
        } catch (IOException e) {
            showToast(R.string.error_loading_list);
        }
    }

    // If sort-by is "by name", sort alphabetically
    if (sortBy.equals("name")) {
        // Copy titles array
        System.arraycopy(listOfTitlesByDate, 0, listOfTitlesByName, 0, numOfNotes);

        // Sort titles
        Arrays.sort(listOfTitlesByName, NoteListItem.NoteComparatorTitle);

        // Initialize notes array
        for (int i = 0; i < numOfNotes; i++)
            listOfNotesByName[i] = "new";

        // Copy filenames array with new sort order of titles and nullify date arrays
        for (int i = 0; i < numOfNotes; i++) {
            for (int j = 0; j < numOfNotes; j++) {
                if (listOfTitlesByName[i].getNote().equals(listOfTitlesByDate[j].getNote())
                        && listOfNotesByName[i].equals("new")) {
                    listOfNotesByName[i] = listOfNotesByDate[j];
                    listOfNotesByDate[j] = "";
                    listOfTitlesByDate[j] = new NoteListItem("", "");
                }
            }
        }

        // Populate ArrayList with notes, showing name as first line of the notes
        list.addAll(Arrays.asList(listOfTitlesByName));
    } else if (sortBy.equals("date"))
        list.addAll(Arrays.asList(listOfTitlesByDate));

    // Create the custom adapters to bind the array to the ListView
    final NoteListDateAdapter dateAdapter = new NoteListDateAdapter(getActivity(), list);
    final NoteListAdapter adapter = new NoteListAdapter(getActivity(), list);

    // Display the ListView
    if (showDate)
        listView.setAdapter(dateAdapter);
    else
        listView.setAdapter(adapter);

    // Finalize arrays to prepare for handling clicked items
    final String[] finalListByDate = listOfNotesByDate;
    final String[] finalListByName = listOfNotesByName;

    // Make ListView handle clicked items
    listView.setClickable(true);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            if (sortBy.equals("date")) {
                if (directEdit)
                    listener.editNote(finalListByDate[position]);
                else
                    listener.viewNote(finalListByDate[position]);
            } else if (sortBy.equals("name")) {
                if (directEdit)
                    listener.editNote(finalListByName[position]);
                else
                    listener.viewNote(finalListByName[position]);
            }
        }
    });

    // Make ListView handle contextual action bar
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        final ArrayList<String> cab = new ArrayList<>(numOfNotes);

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // Respond to clicks on the actions in the CAB
                switch (item.getItemId()) {
                case R.id.action_export:
                    mode.finish(); // Action picked, so close the CAB
                    listener.exportNote(cab.toArray());
                    return true;
                case R.id.action_delete:
                    mode.finish(); // Action picked, so close the CAB
                    listener.deleteNote(cab.toArray());
                    return true;
                default:
                    return false;
                }
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                listener.hideFab();

                // Inflate the menu for the CAB
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context_menu, menu);

                // Clear any old values from cab array
                cab.clear();

                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                listener.showFab();
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                // Add/remove filenames to cab array as they are checked/unchecked
                if (checked) {
                    if (sortBy.equals("date"))
                        cab.add(finalListByDate[position]);
                    if (sortBy.equals("name"))
                        cab.add(finalListByName[position]);
                } else {
                    if (sortBy.equals("date"))
                        cab.remove(finalListByDate[position]);
                    if (sortBy.equals("name"))
                        cab.remove(finalListByName[position]);
                }

                // Update the title in CAB
                if (cab.size() == 0)
                    mode.setTitle("");
                else
                    mode.setTitle(cab.size() + " " + listener.getCabString(cab.size()));
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }
        });
    }

    // If there are no saved notes, then display the empty view
    if (numOfNotes == 0) {
        TextView empty = (TextView) getActivity().findViewById(R.id.empty);
        listView.setEmptyView(empty);
    }
}

From source file:it.mb.whatshare.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    return true;//from  w ww  .j a va2  s .  c om
}

From source file:it.mb.whatshare.MainActivity.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu_devices, menu);
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    deviceSelectedContextMenu = inboundDevices.get(info.position);
}

From source file:net.sf.diningout.app.ui.FriendsFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (!mListener.onFriendsOptionsMenu()) {
        return;/*from  w ww . j a  v a2  s. co  m*/
    }
    inflater.inflate(R.menu.friends, menu);
    if (mInit) {
        menu.removeItem(R.id.search);
    } else {
        MenuItem item = menu.findItem(R.id.search);
        mSearch = (SearchView) item.getActionView();
        mSearch.setSearchableInfo(Managers.search(a).getSearchableInfo(a.getComponentName()));
        SearchViews.setBackground(mSearch, R.drawable.textfield_searchview);
        mSearch.setOnSearchClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                event("friends", "search");
            }
        });
        mSearch.setOnQueryTextListener(new SearchTextListener());
        item.setOnActionExpandListener(new SearchExpandListener());
    }
    if (!Intents.hasActivity(a, sAddIntent)) {
        menu.removeItem(R.id.add);
    }
}

From source file:cn.tycoon.lighttrans.fileManager.AbstractFilePickerFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.picker_actions, menu);

    MenuItem item = menu.findItem(R.id.action_createdir);
    item.setVisible(allowCreateDir);//from   w ww. j a  v  a 2  s.  co m
}

From source file:com.dattasmoon.pebble.plugin.IgnorePreference.java

@Override
protected void onBindDialogView(View view) {

    btnAdd = (Button) view.findViewById(R.id.btnAdd);
    etMatch = (EditText) view.findViewById(R.id.etMatch);
    chkRawRegex = (CheckBox) view.findViewById(R.id.chkRawRegex);
    chkCaseInsensitive = (CheckBox) view.findViewById(R.id.chkCaseInsensitive);
    actvApplications = (AutoCompleteTextView) view.findViewById(R.id.actvApplications);

    spnApplications = (Spinner) view.findViewById(R.id.spnApplications);
    spnMode = (Spinner) view.findViewById(R.id.spnMode);
    lvIgnore = (ListView) view.findViewById(R.id.lvIgnore);
    lvIgnore.setAdapter(arrayAdapter);/*from   www . j  a v  a  2  s .co m*/
    lvIgnore.setEmptyView(view.findViewById(android.R.id.empty));
    new LoadAppsTask().execute();

    lvIgnore.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo contextInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
            int position = contextInfo.position;
            long id = contextInfo.id;
            // the child view who's info we're viewing (should be equal to v)
            final View v = contextInfo.targetView;
            MenuInflater inflater = new MenuInflater(getContext());
            inflater.inflate(R.menu.preference_ignore_context, menu);

            //we have to do this mess because DialogPreference doesn't allow for onMenuItemSelected or onOptionsItemSelected. Bleh
            menu.findItem(R.id.btnEdit).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    final int arrayPosition = (Integer) v.getTag();
                    final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
                    JSONArray temp = new JSONArray();
                    for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                        try {
                            JSONObject ignore = arrayAdapter.getJSONArray().getJSONObject(i);
                            if (i == arrayPosition) {
                                etMatch.setText(ignore.getString("match"));
                                chkRawRegex.setChecked(ignore.getBoolean("raw"));
                                chkCaseInsensitive.setChecked(ignore.optBoolean("insensitive", true));
                                String app = ignore.getString("app");
                                if (app == "-1") {
                                    actvApplications.setText(getContext().getString(R.string.ignore_any));
                                } else {
                                    actvApplications.setText(app);
                                }
                                boolean exclude = ignore.optBoolean("exclude", true);
                                if (exclude) {
                                    spnMode.setSelection(Constants.IgnoreMode.EXCLUDE.ordinal());
                                } else {
                                    spnMode.setSelection(Constants.IgnoreMode.INCLUDE.ordinal());
                                }
                                continue;
                            }

                            temp.put(ignore);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    arrayAdapter.setJSONArray(temp);

                    arrayAdapter.notifyDataSetChanged();
                    return true;
                }
            });
            menu.findItem(R.id.btnDelete).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

                    final int arrayPosition = (Integer) v.getTag();
                    final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
                    builder.setMessage(getContext().getResources().getString(R.string.confirm_delete) + " '"
                            + text + "' ?")
                            .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    JSONArray temp = new JSONArray();
                                    for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                                        if (i == arrayPosition) {
                                            continue;
                                        }
                                        try {
                                            temp.put(arrayAdapter.getJSONArray().getJSONObject(i));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    arrayAdapter.setJSONArray(temp);

                                    arrayAdapter.notifyDataSetChanged();
                                }
                            }).setNegativeButton(R.string.decline, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // User cancelled the dialog
                                }
                            });
                    builder.create().show();
                    return true;
                }
            });
        }
    });
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject item = new JSONObject();
            try {
                item.put("match", etMatch.getText().toString());
                item.put("raw", chkRawRegex.isChecked());
                item.put("insensitive", chkCaseInsensitive.isChecked());
                if (actvApplications.getText().toString()
                        .equalsIgnoreCase(getContext().getString(R.string.ignore_any))) {
                    item.put("app", "-1");
                } else {
                    item.put("app", actvApplications.getText().toString());
                }
                if (spnMode.getSelectedItemPosition() == Constants.IgnoreMode.INCLUDE.ordinal()) {
                    item.put("exclude", false);
                } else {
                    item.put("exclude", true);
                }
                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG, "Item is: " + item.toString());
                }
                arrayAdapter.getJSONArray().put(item);
                etMatch.setText("");
                arrayAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
    actvApplications.setText(getContext().getString(R.string.ignore_any));
    actvApplications.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actvApplications.showDropDown();
        }
    });
    actvApplications.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ApplicationInfo pkg = (ApplicationInfo) parent.getItemAtPosition(position);
            if (pkg == null) {
                actvApplications.setText(getContext().getString(R.string.ignore_any));
            } else {
                actvApplications.setText(pkg.packageName);
            }
        }
    });
    actvApplications.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                actvApplications.showDropDown();
            } else {
                if (actvApplications.getText().length() == 0) {
                    actvApplications.setText(getContext().getString(R.string.ignore_any));
                }
            }
        }
    });
    super.onBindDialogView(view);

}

From source file:org.ros.android.app_chooser.ExchangeActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.app_chooser_menu, menu);
    return true;//from  w ww .  j  a  va2 s.  c  o  m
}