Example usage for android.view MenuItem setActionView

List of usage examples for android.view MenuItem setActionView

Introduction

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

Prototype

public MenuItem setActionView(@LayoutRes int resId);

Source Link

Document

Set an action view for this menu item.

Usage

From source file:com.teitsmch.hearthmaker.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.navigation_drawer, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    //item.setActionView(R.layout.iv_rotate);
    //ImageView refresh = (ImageView) item.getActionView();
    final ImageView image = new ImageView(this);
    ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(
            ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
    image.setLayoutParams(layoutParams);
    image.setImageResource(R.drawable.ic_filter_list_24dp);
    int padding_in_dp = 16;
    final float scale = getResources().getDisplayMetrics().density;
    int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
    image.setPadding(padding_in_px, 0, padding_in_px, 0);

    image.setOnClickListener(new View.OnClickListener() {
        @Override//from ww  w. j  av a  2s  .  c o  m
        public void onClick(View v) {
            if (mSpinnerView.getVisibility() == View.VISIBLE)
                image.setImageResource(R.drawable.rev_ic_filter_list_24dp);
            else
                image.setImageResource(R.drawable.ic_filter_list_24dp);

            onOptionsItemSelected(item);
        }
    });
    item.setActionView(image);

    if (mFragment.getClass() == CardViewFragment.class) {
        menu.findItem(MENU_ITEM_RENAME).setVisible(false);
        menu.findItem(MENU_ITEM_CLEAR).setVisible(false);
        menu.findItem(MENU_ITEM_DELETE).setVisible(false);
        menu.findItem(R.id.class_icon).setVisible(false);
    } else {
        menu.findItem(MENU_ITEM_RENAME).setVisible(true);
        menu.findItem(MENU_ITEM_CLEAR).setVisible(true);
        menu.findItem(MENU_ITEM_DELETE).setVisible(true);
        menu.findItem(R.id.class_icon).setVisible(true);
        menu.findItem(R.id.class_icon).setIcon(classIcon);
    }

    return super.onCreateOptionsMenu(menu);
}

From source file:in.andres.kandroid.ui.TaskEditActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
    case android.R.id.home:
        onBackPressed();//from www.ja  v a 2 s.c o  m
        return true;
    case R.id.action_save:
        ownerId = spinnerProjectUsers.getSelectedItemPosition();
        if (spinnerProjectUsers.getSelectedItemPosition() != 0) {
            for (Enumeration<Integer> iter = projectUsers.keys(); iter.hasMoreElements();) {
                Integer key = iter.nextElement();
                if (projectUsers.get(key).contentEquals((String) spinnerProjectUsers.getSelectedItem())) {
                    ownerId = key;
                    break;
                }
            }
        }
        if (isNewTask) {
            kanboardAPI.createTask(editTextTitle.getText().toString(), projectid,
                    colorId != null ? colorId : defaultColor, columnId, ownerId, null, dueDate,
                    editTextDescription.getText().toString(), null, null, swimlaneId, null, null, null, null,
                    null, null, null, startDate);

        } else {
            kanboardAPI.updateTask(task.getId(), editTextTitle.getText().toString(),
                    colorId != null ? colorId : defaultColor, ownerId, dueDate,
                    editTextDescription.getText().toString(), null, null, null, null, null, null, null, null,
                    null, startDate);
        }
        ProgressBar prog = new ProgressBar(TaskEditActivity.this);
        prog.getIndeterminateDrawable().setColorFilter(Color.WHITE, android.graphics.PorterDuff.Mode.MULTIPLY);
        item.setActionView(prog);
        item.expandActionView();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:com.money.manager.ex.home.MainActivity.java

private void startSyncIconRotation(MenuItem item) {
    if (item == null)
        return;//w w  w  .j  a  v  a2  s.  c  o m

    // define the animation for rotation
    Animation animation = new RotateAnimation(360.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setInterpolator(new LinearInterpolator());
    animation.setDuration(1200);
    //        animRotate = AnimationUtils.loadAnimation(this, R.anim.rotation);
    animation.setRepeatCount(Animation.INFINITE);

    ImageView imageView = new ImageView(this);
    UIHelper uiHelper = new UIHelper(this);
    imageView.setImageDrawable(
            uiHelper.getIcon(GoogleMaterial.Icon.gmd_cached).color(uiHelper.getToolbarItemColor()));
    imageView.setPadding(8, 8, 8, 8);
    //        imageView.setLayoutParams(new Toolbar.LayoutParams());

    imageView.startAnimation(animation);
    item.setActionView(imageView);
}

From source file:com.chatwing.whitelabel.managers.ChatboxModeManager.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    AppCompatActivity activity = mActivityDelegate.getActivity();
    final DrawerLayout drawerLayout = mActivityDelegate.getDrawerLayout();
    activity.getMenuInflater().inflate(R.menu.chatbox_menu, menu);
    MenuItem onlineUsersItem = menu.findItem(R.id.online_users);
    mediaAddItem = menu.findItem(R.id.audio_add);

    /**//w w  w.j  av  a 2s  .  c om
     * Create badge view for online user item
     */
    ImageButton iconView = new ImageButton(activity, null, R.style.Widget_AppCompat_ActionButton);
    iconView.setImageDrawable(onlineUsersItem.getIcon());
    iconView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawerLayout.isDrawerOpen(DRAWER_GRAVITY_ONLINE_USER)) {
                drawerLayout.closeDrawer(DRAWER_GRAVITY_ONLINE_USER);
            } else {
                drawerLayout.openDrawer(DRAWER_GRAVITY_ONLINE_USER);
            }
        }
    });

    // The badge view requires target view (iconView in this case)
    // to have a ViewGroup parent
    LinearLayout container = new LinearLayout(activity);
    container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    container.addView(iconView);

    Resources res = activity.getResources();
    mOnlineUsersBadgeView = new BadgeView(activity, iconView);
    mOnlineUsersBadgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
    mOnlineUsersBadgeView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
            res.getDimension(R.dimen.badge_view_text_size));
    mOnlineUsersBadgeView.setBadgeMargin(res.getDimensionPixelSize(R.dimen.default_margin));
    onlineUsersItem.setActionView(container);
    mOnlineUsersBadgeView
            .setBadgeBackgroundColor(mActivityDelegate.getActivity().getResources().getColor(R.color.accent));

    return true;
}

From source file:gov.wa.wsdot.android.wsdot.ui.trafficmap.TrafficMapActivity.java

private void refreshOverlays(final MenuItem item) {

    // define the animation for rotation
    Animation animation = new RotateAnimation(360.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(1000);/*from   ww  w  .ja  va  2  s .  c  o  m*/
    animation.setRepeatCount(Animation.INFINITE);

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mToolbar.getMenu().getItem(menu_item_refresh).setActionView(null);
            mToolbar.getMenu().getItem(menu_item_refresh).setIcon(R.drawable.ic_menu_refresh);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });
    ImageView imageView = new ImageView(this, null, android.R.style.Widget_Material_ActionButton);
    imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_menu_refresh));
    imageView.setPadding(31, imageView.getPaddingTop(), 32, imageView.getPaddingBottom());
    imageView.startAnimation(animation);
    item.setActionView(imageView);

    if (mMap != null) {
        mapCameraViewModel.refreshCameras();
        mapHighwayAlertViewModel.refreshAlerts();
    }
}

From source file:dev.ukanth.ufirewall.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {

    /*case android.R.id.home:
       disableOrEnable();//from w ww  .ja  v  a  2s. c om
         return true;*/
    case R.id.menu_toggle:
        disableOrEnable();
        return true;
    case R.id.menu_apply:
        applyOrSaveRules();
        return true;
    case R.id.menu_exit:
        finish();
        System.exit(0);
        return false;
    case R.id.menu_help:
        showAbout();
        return true;
    /*case R.id.menu_setpwd:
       setPassword();
       return true;*/
    case R.id.menu_log:
        showLog();
        return true;
    case R.id.menu_rules:
        showRules();
        return true;
    case R.id.menu_setcustom:
        setCustomScript();
        return true;
    case R.id.menu_preference:
        showPreferences();
        return true;
    /*case R.id.menu_reload:
       Api.applications = null;
       showOrLoadApplications();
       return true;*/
    case R.id.menu_search:

        item.setActionView(R.layout.searchbar);
        final EditText filterText = (EditText) item.getActionView().findViewById(R.id.searchApps);
        filterText.addTextChangedListener(filterTextWatcher);
        filterText.setEllipsize(TruncateAt.END);
        filterText.setSingleLine();

        MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when collapsed
                return true; // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                filterText.post(new Runnable() {
                    @Override
                    public void run() {
                        filterText.requestFocus();
                        InputMethodManager imm = (InputMethodManager) getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(filterText, InputMethodManager.SHOW_IMPLICIT);
                    }
                });
                return true; // Return true to expand action view
            }
        });

        return true;
    case R.id.menu_export:

        new MaterialDialog.Builder(this).title(R.string.exports).cancelable(false)
                .items(new String[] { getString(R.string.export_rules), getString(R.string.export_all) })
                .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        switch (which) {
                        case 0:
                            Api.saveSharedPreferencesToFileConfirm(MainActivity.this);
                            break;
                        case 1:
                            Api.saveAllPreferencesToFileConfirm(MainActivity.this);
                            break;
                        }
                        return true;
                    }
                }).positiveText(R.string.exports).negativeText(R.string.Cancel).show();
        return true;
    case R.id.menu_import:

        new MaterialDialog.Builder(this).title(R.string.imports).cancelable(false)
                .items(new String[] { getString(R.string.import_rules), getString(R.string.import_all),
                        getString(R.string.import_rules_droidwall) })
                .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        switch (which) {
                        case 0:
                            //Intent intent = new Intent(MainActivity.this, FileChooserActivity.class);
                            //startActivityForResult(intent, FILE_CHOOSER_LOCAL);
                            File mPath = new File(Environment.getExternalStorageDirectory() + "//afwall//");
                            FileDialog fileDialog = new FileDialog(MainActivity.this, mPath, true);
                            //fileDialog.setFlag(true);
                            //fileDialog.setFileEndsWith(new String[] {"backup", "afwall-backup"}, "all");
                            fileDialog.addFileListener(new FileDialog.FileSelectedListener() {
                                public void fileSelected(File file) {
                                    String fileSelected = file.toString();
                                    StringBuilder builder = new StringBuilder();
                                    if (Api.loadSharedPreferencesFromFile(MainActivity.this, builder,
                                            fileSelected)) {
                                        Api.applications = null;
                                        showOrLoadApplications();
                                        Api.toast(MainActivity.this,
                                                getString(R.string.import_rules_success) + fileSelected);
                                    } else {
                                        if (builder.toString().equals("")) {
                                            Api.toast(MainActivity.this, getString(R.string.import_rules_fail));
                                        } else {
                                            Api.toast(MainActivity.this, builder.toString());
                                        }
                                    }
                                }
                            });
                            fileDialog.showDialog();
                            break;
                        case 1:

                            if (Api.getCurrentPackage(MainActivity.this).equals("dev.ukanth.ufirewall.donate")
                                    || G.isDo(getApplicationContext())) {

                                File mPath2 = new File(
                                        Environment.getExternalStorageDirectory() + "//afwall//");
                                FileDialog fileDialog2 = new FileDialog(MainActivity.this, mPath2, false);
                                //fileDialog2.setFlag(false);
                                //fileDialog2.setFileEndsWith(new String[] {"backup_all", "afwall-backup-all"}, "" );
                                fileDialog2.addFileListener(new FileDialog.FileSelectedListener() {
                                    public void fileSelected(File file) {
                                        String fileSelected = file.toString();
                                        StringBuilder builder = new StringBuilder();
                                        if (Api.loadAllPreferencesFromFile(MainActivity.this, builder,
                                                fileSelected)) {
                                            Api.applications = null;
                                            showOrLoadApplications();
                                            Api.toast(MainActivity.this,
                                                    getString(R.string.import_rules_success) + fileSelected);
                                            Intent intent = getIntent();
                                            finish();
                                            startActivity(intent);
                                        } else {
                                            if (builder.toString().equals("")) {
                                                Api.toast(MainActivity.this,
                                                        getString(R.string.import_rules_fail));
                                            } else {
                                                Api.toast(MainActivity.this, builder.toString());
                                            }
                                        }
                                    }
                                });
                                fileDialog2.showDialog();
                            } else {

                                new MaterialDialog.Builder(MainActivity.this).cancelable(false)
                                        .title(R.string.buy_donate).content(R.string.donate_only)
                                        .positiveText(R.string.buy_donate).negativeText(R.string.close)

                                        .icon(getResources().getDrawable(R.drawable.ic_launcher))
                                        .callback(new MaterialDialog.ButtonCallback() {
                                            @Override
                                            public void onPositive(MaterialDialog dialog) {
                                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                                intent.setData(Uri.parse("market://search?q=pub:ukpriya"));
                                                startActivity(intent);
                                            }

                                            @Override
                                            public void onNegative(MaterialDialog dialog) {
                                                dialog.cancel();
                                            }
                                        }).show();
                            }
                            break;
                        case 2:

                            new MaterialDialog.Builder(MainActivity.this).cancelable(false)
                                    .title(R.string.import_rules_droidwall).content(R.string.overrideRules)
                                    .positiveText(R.string.Yes).negativeText(R.string.No)
                                    .icon(getResources().getDrawable(R.drawable.ic_launcher))
                                    .callback(new MaterialDialog.ButtonCallback() {
                                        @Override
                                        public void onPositive(MaterialDialog dialog) {
                                            if (ImportApi
                                                    .loadSharedPreferencesFromDroidWall(MainActivity.this)) {
                                                Api.applications = null;
                                                showOrLoadApplications();
                                                Api.toast(MainActivity.this,
                                                        getString(R.string.import_rules_success) + Environment
                                                                .getExternalStorageDirectory().getAbsolutePath()
                                                                + "/afwall/");
                                            } else {
                                                Api.toast(MainActivity.this,
                                                        getString(R.string.import_rules_fail));
                                            }
                                        }

                                        @Override
                                        public void onNegative(MaterialDialog dialog) {
                                            dialog.cancel();
                                        }
                                    }).show();

                            break;
                        }
                        return true;
                    }
                }).positiveText(R.string.imports).negativeText(R.string.Cancel).show();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}