Example usage for android.view MenuItem getTitle

List of usage examples for android.view MenuItem getTitle

Introduction

In this page you can find the example usage for android.view MenuItem getTitle.

Prototype

public CharSequence getTitle();

Source Link

Document

Retrieve the current title of the item.

Usage

From source file:com.afzaln.myweatherapp.custom.action.NavigationViewActions.java

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id./*from www .ja  v a  2  s  . c  o  m*/
 *
 * <p>
 * View constraints:
 * <ul>
 * <li>View must be a child of a {@link DrawerLayout}
 * <li>View must be of type {@link NavigationView}
 * <li>View must be visible on screen
 * <li>View must be displayed on screen
 * <ul>
 *
 * @param menuItemId the resource id of the menu item
 * @return a {@link ViewAction} that navigates on a menu item
 */
public static ViewAction navigateTo(final int menuItemId) {

    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NavigationView navigationView = (NavigationView) view;
            Menu menu = navigationView.getMenu();
            if (null == menu.findItem(menuItemId)) {
                throw new PerformException.Builder().withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new RuntimeException(getErrorMessage(menu, view))).build();
            }
            menu.performIdentifierAction(menuItemId, 0);
            uiController.loopMainThreadUntilIdle();
        }

        private String getErrorMessage(Menu menu, View view) {
            String NEW_LINE = System.getProperty("line.separator");
            StringBuilder errorMessage = new StringBuilder(
                    "Menu item was not found, " + "available menu items:").append(NEW_LINE);
            for (int position = 0; position < menu.size(); position++) {
                errorMessage.append("[MenuItem] position=").append(position);
                MenuItem menuItem = menu.getItem(position);
                if (menuItem != null) {
                    CharSequence itemTitle = menuItem.getTitle();
                    if (itemTitle != null) {
                        errorMessage.append(", title=").append(itemTitle);
                    }
                    if (view.getResources() != null) {
                        int itemId = menuItem.getItemId();
                        try {
                            errorMessage.append(", id=");
                            String menuItemResourceName = view.getResources().getResourceName(itemId);
                            errorMessage.append(menuItemResourceName);
                        } catch (NotFoundException nfe) {
                            errorMessage.append("not found");
                        }
                    }
                    errorMessage.append(NEW_LINE);
                }
            }
            return errorMessage.toString();
        }

        @Override
        public String getDescription() {
            return "click on menu item with id";
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(NavigationView.class),
                    withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDisplayingAtLeast(90));
        }
    };

}

From source file:com.mifos.mifosxdroid.tests.action.NavigationViewActions.java

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id.//  ww w . j a v a 2  s. co m
 * <p/>
 * <p/>
 * View constraints:
 * <ul>
 * <li>View must be a child of a {@link DrawerLayout}
 * <li>View must be of type {@link NavigationView}
 * <li>View must be visible on screen
 * <li>View must be displayed on screen
 * <ul>
 *
 * @param menuItemId the resource id of the menu item
 * @return a {@link ViewAction} that navigates on a menu item
 */
public static ViewAction navigateTo(final int menuItemId) {

    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NavigationView navigationView = (NavigationView) view;
            Menu menu = navigationView.getMenu();
            if (null == menu.findItem(menuItemId)) {
                throw new PerformException.Builder().withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new RuntimeException(getErrorMessage(menu, view))).build();
            }
            menu.performIdentifierAction(menuItemId, 0);
            uiController.loopMainThreadUntilIdle();
        }

        private String getErrorMessage(Menu menu, View view) {
            String NEW_LINE = System.getProperty("line.separator");
            StringBuilder errorMessage = new StringBuilder(20)
                    .append("Menu item was not found, available menu items:").append(NEW_LINE);
            for (int position = 0; position < menu.size(); position++) {
                errorMessage.append("[MenuItem] position=").append(position);
                MenuItem menuItem = menu.getItem(position);
                if (menuItem != null) {
                    CharSequence itemTitle = menuItem.getTitle();
                    if (itemTitle != null) {
                        errorMessage.append(", title=").append(itemTitle);
                    }
                    if (view.getResources() != null) {
                        int itemId = menuItem.getItemId();
                        try {
                            errorMessage.append(", id=");
                            String menuItemResourceName = view.getResources().getResourceName(itemId);
                            errorMessage.append(menuItemResourceName);
                        } catch (NotFoundException nfe) {
                            errorMessage.append("not found");
                        }
                    }
                    errorMessage.append(NEW_LINE);
                }
            }
            return errorMessage.toString();
        }

        @Override
        public String getDescription() {
            return "click on menu item with id";
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(NavigationView.class),
                    withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDisplayingAtLeast(90));
        }
    };

}

From source file:com.justplay1.shoppist.utils.NavigationViewActions.java

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id./*from  w w w  . j a  v a  2 s .  c o  m*/
 * <p>
 * <p>
 * View constraints:
 * <ul>
 * <li>View must be a child of a {@link DrawerLayout}
 * <li>View must be of type {@link NavigationView}
 * <li>View must be visible on screen
 * <li>View must be displayed on screen
 * <ul>
 *
 * @param menuItemId the resource id of the menu item
 * @return a {@link ViewAction} that navigates on a menu item
 */
public static ViewAction navigateTo(final int menuItemId) {

    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NavigationView navigationView = (NavigationView) view;
            Menu menu = navigationView.getMenu();
            if (null == menu.findItem(menuItemId)) {
                throw new PerformException.Builder().withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new RuntimeException(getErrorMessage(menu, view))).build();
            }
            menu.performIdentifierAction(menuItemId, 0);
            uiController.loopMainThreadUntilIdle();
        }

        private String getErrorMessage(Menu menu, View view) {
            String NEW_LINE = System.getProperty("line.separator");
            StringBuilder errorMessage = new StringBuilder(
                    "Menu item was not found, " + "available menu items:").append(NEW_LINE);
            for (int position = 0; position < menu.size(); position++) {
                errorMessage.append("[MenuItem] position=").append(position);
                MenuItem menuItem = menu.getItem(position);
                if (menuItem != null) {
                    CharSequence itemTitle = menuItem.getTitle();
                    if (itemTitle != null) {
                        errorMessage.append(", title=").append(itemTitle);
                    }
                    if (view.getResources() != null) {
                        int itemId = menuItem.getItemId();
                        try {
                            errorMessage.append(", id=");
                            String menuItemResourceName = view.getResources().getResourceName(itemId);
                            errorMessage.append(menuItemResourceName);
                        } catch (Resources.NotFoundException nfe) {
                            errorMessage.append("not found");
                        }
                    }
                    errorMessage.append(NEW_LINE);
                }
            }
            return errorMessage.toString();
        }

        @Override
        public String getDescription() {
            return "click on menu item with id";
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(NavigationView.class),
                    withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDisplayingAtLeast(90));
        }
    };

}

From source file:com.example.ray.firstapp.bottombar.MiscUtils.java

/**
 * A hacky method for inflating menus from xml resources to an array
 * of BottomBarTabs./*from   ww w . j av a2 s  .  com*/
 *
 * @param activity the activity context for retrieving the MenuInflater.
 * @param menuRes  the xml menu resource to inflate
 * @return an Array of BottomBarTabs.
 */
protected static BottomBarTab[] inflateMenuFromResource(Activity activity, @MenuRes int menuRes) {
    // A bit hacky, but hey hey what can I do
    PopupMenu popupMenu = new PopupMenu(activity, null);
    Menu menu = popupMenu.getMenu();
    activity.getMenuInflater().inflate(menuRes, menu);

    int menuSize = menu.size();
    BottomBarTab[] tabs = new BottomBarTab[menuSize];

    for (int i = 0; i < menuSize; i++) {
        MenuItem item = menu.getItem(i);
        BottomBarTab tab = new BottomBarTab(item.getIcon(), String.valueOf(item.getTitle()));
        tab.id = item.getItemId();
        tabs[i] = tab;
    }

    return tabs;
}

From source file:com.wms.opensource.images3android.manager.MenuManager.java

public static void displayTemplatesMenu(final Context context, final Image image) {
    IconContextMenu cm = new IconContextMenu(context, R.menu.templates_menu);
    if (templateCollection == null) {
        String templateCollectionString = "";
        templateCollectionString = FileUtil.getStringFromFileInCache(StorageUtil.getTempDirectory(context),
                PersistFileNameUtil.getTemplateCollectionFileName(image.getImagePlantId()),
                context.getString(R.string.charSetName));
        try {//from   w ww . ja  v a2 s .c om
            templateCollection = (TemplateCollection) JsonUtil.deserialize(templateCollectionString, "",
                    TemplateCollection.class);
        } catch (JsonParseException e) {

        } catch (JsonMappingException e) {

        } catch (IOException e) {

        }
    }
    for (int i = 0; i < templateCollection.getResults().size(); i++) {
        TemplateResult result = templateCollection.getResults().get(i);
        cm.getMenu().add(0, i + 1, i, result.getId().getTemplateName());
    }

    cm.setOnIconContextItemSelectedListener(new IconContextItemSelectedListener() {
        @Override
        public void onIconContextItemSelected(MenuItem item, Object info) {
            if (item.getItemId() == R.id.action_hint) {
                // Position 0 is the indication for selecting a template, so we do nothing
            } else {
                String templateName = item.getTitle().toString();
                ReviewImageManager reviewImageManager = new ReviewImageManager(context);
                reviewImageManager.showImagePopupWindow(image, templateName);
            }
        }
    });

    cm.show();
}

From source file:org.brandroid.openmanager.fragments.DialogHandler.java

private static void fillShortcutsTable(TableLayout table) {
    final Context context = table.getContext();
    for (int sc : MenuUtils.getMenuShortcuts(context)) {
        TableRow tr = new TableRow(context);
        TextView tv1 = new TextView(context);
        tv1.setGravity(Gravity.RIGHT);/*  ww  w  .  j av a  2  s .c om*/
        tv1.setPadding(20, 0, 20, 0);
        char scc = (char) (sc + ('a' - KeyEvent.KEYCODE_A));
        tv1.setText("" + scc);
        TextView tv2 = new TextView(context);
        MenuItem item = MenuUtils.getMenuShortcut(sc);
        tv2.setText(item.getTitle());
        tr.addView(tv1);
        tr.addView(tv2);
        table.addView(tr);
    }
}

From source file:com.example.android.supportv7.app.ActionBarMechanics.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
    return true;//from   ww  w.  ja  v a2 s  .com
}

From source file:pl.allegro.foggerexample.ui.ComponentsFragment.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    Toast.makeText(getActivity(), "You picked: " + item.getTitle(), Toast.LENGTH_SHORT).show();
    return true;/*from  ww  w .  j a v a2 s  . co m*/
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void resetCheatSheet(ActionMenuView menuView) {
    final OnLongClickListener listener = new OnLongClickListener() {
        @Override/*from w  w w.j  av a  2  s  . co  m*/
        public boolean onLongClick(View v) {
            if (((ActionMenuItemView) v).hasText())
                return false;
            final MenuItem menuItem = ((ActionMenuItemView) v).getItemData();
            Utils.showMenuItemToast(v, menuItem.getTitle(), true);
            return true;
        }
    };
    for (int i = 0, j = menuView.getChildCount(); i < j; i++) {
        final View child = menuView.getChildAt(i);
        if (!(child instanceof ActionMenuItemView))
            continue;
        final ActionMenuItemView itemView = (ActionMenuItemView) child;
        final MenuItem menuItem = itemView.getItemData();
        if (menuItem.hasSubMenu())
            continue;
        itemView.setOnLongClickListener(listener);
    }
}

From source file:com.blackmoon.english.MainActivity.java

private void setupDrawerLayout() {
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override// w ww. ja v a2s  .c o m
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            Snackbar.make(content, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            return true;
        }
    });
}