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:fr.cph.chicago.activity.MainActivity.java

@Override
public final boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_refresh:
        if (mCurrentPosition != POSITION_DIVVY && mCurrentPosition != POSITION_NEARBY
                && mCurrentPosition != POSITION_ALERTS) {
            MenuItem menuItem = item;
            menuItem.setActionView(R.layout.progressbar);
            menuItem.expandActionView();

            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            boolean loadTrain = sharedPref.getBoolean("cta_train", true);
            boolean loadBus = sharedPref.getBoolean("cta_bus", true);
            boolean loadBike = sharedPref.getBoolean("divvy_bike", true);
            boolean loadAlert = sharedPref.getBoolean("cta_alert", true);

            MultiMap<String, String> params = new MultiValueMap<String, String>();
            List<Integer> trainFavorites = Preferences
                    .getTrainFavorites(ChicagoTracker.PREFERENCE_FAVORITES_TRAIN);
            for (Integer fav : trainFavorites) {
                params.put("mapid", String.valueOf(fav));
            }//from   w w w.j  a  v a 2s  .c  om
            MultiMap<String, String> params2 = new MultiValueMap<String, String>();
            List<String> busFavorites = Preferences.getBusFavorites(ChicagoTracker.PREFERENCE_FAVORITES_BUS);
            for (String str : busFavorites) {
                String[] fav = Util.decodeBusFavorite(str);
                params2.put("rt", fav[0]);
                params2.put("stpid", fav[1]);
            }
            GlobalConnectTask task;
            try {
                task = new GlobalConnectTask(mFavoritesFragment, FavoritesFragment.class,
                        CtaRequestType.TRAIN_ARRIVALS, params, CtaRequestType.BUS_ARRIVALS, params2, loadTrain,
                        loadBus, loadBike);
                task.execute((Void) null);
            } catch (ParserException e) {
                ChicagoTracker.displayError(this, e);
                return true;
            }
            // Google analytics
            if (loadTrain) {
                Util.trackAction(MainActivity.this, R.string.analytics_category_req,
                        R.string.analytics_action_get_train, R.string.analytics_action_get_train_arrivals, 0);
            }
            if (loadBus) {
                Util.trackAction(MainActivity.this, R.string.analytics_category_req,
                        R.string.analytics_action_get_bus, R.string.analytics_action_get_bus_arrival, 0);
            }
            if (loadBike) {
                Util.trackAction(MainActivity.this, R.string.analytics_category_req,
                        R.string.analytics_action_get_divvy, R.string.analytics_action_get_divvy_all, 0);
            }
            // Check if bus/bike or alert data are not loaded. If not, load them.
            // Can happen when the app has been loaded without any data connection
            boolean loadData = false;
            DataHolder dataHolder = DataHolder.getInstance();

            BusData busData = dataHolder.getBusData();
            AlertData alertData = dataHolder.getAlertData();

            Bundle bundle = getIntent().getExtras();
            List<BikeStation> bikeStations = bundle.getParcelableArrayList("bikeStations");

            if (loadBus && busData.getRoutes() != null && busData.getRoutes().size() == 0) {
                loadData = true;
            }
            if (!loadData && loadAlert && alertData.getAlerts() != null && alertData.getAlerts().size() == 0) {
                loadData = true;
            }
            if (!loadData && loadBike && bikeStations == null) {
                loadData = true;
            }
            if (loadData) {
                startRefreshAnimation();
                new LoadData().execute();
            }
            Util.trackAction(this, R.string.analytics_category_ui, R.string.analytics_action_press,
                    R.string.analytics_action_refresh_fav, 0);
        } else if (mCurrentPosition == POSITION_NEARBY) {
            mNearbyFragment.reloadData();
            Util.trackAction(this, R.string.analytics_category_ui, R.string.analytics_action_press,
                    R.string.analytics_action_refresh_nearby, 0);
        }
        return false;
    case R.id.action_search:
        Util.trackAction(this, R.string.analytics_category_ui, R.string.analytics_action_press,
                R.string.analytics_action_search, 0);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.actionbarsherlock.sample.styled.MainActivityICS.java

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

    // set up a listener for the refresh item
    final MenuItem refresh = (MenuItem) menu.findItem(R.id.menu_refresh);
    refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        // on selecting show progress spinner for 1s
        public boolean onMenuItemClick(MenuItem item) {
            // item.setActionView(R.layout.progress_action);
            handler.postDelayed(new Runnable() {
                public void run() {
                    refresh.setActionView(null);
                }/*  ww w  .  java  2 s  .  co  m*/
            }, 1000);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

From source file:fr.cph.chicago.activity.MainActivity.java

/**
 * Stop animation in menu//from w  w w. j  a  va 2s  . c  o  m
 */
public final void stopRefreshAnimation() {
    if (mMenu != null) {
        MenuItem refreshMenuItem = mMenu.findItem(R.id.action_refresh);
        refreshMenuItem.collapseActionView();
        refreshMenuItem.setActionView(null);
    }
}

From source file:li.klass.fhem.activities.core.FragmentBaseActivity.java

@SuppressLint("NewApi")
@Override/*from ww  w .j ava2 s . c o  m*/
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    if (getPackageName().equals(PREMIUM_PACKAGE)) {
        menu.removeItem(R.id.menu_premium);
    }
    this.optionsMenu = menu;

    MenuItem refreshItem = optionsMenu.findItem(R.id.menu_refresh);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (showProgressIcon) {
            refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
        } else {
            refreshItem.setActionView(null);
        }
    }

    return super.onCreateOptionsMenu(menu);
}

From source file:com.amaze.filemanager.ui.views.drawer.Drawer.java

private void addNewItem(Menu menu, int group, int order, @StringRes int text, MenuMetadata meta,
        @DrawableRes int icon, @DrawableRes Integer actionViewIcon) {
    if (BuildConfig.DEBUG && menu.findItem(order) != null)
        throw new IllegalStateException("Item already id exists: " + order);

    MenuItem item = menu.add(group, order, order, text).setIcon(icon);
    dataUtils.putDrawerMetadata(item, meta);
    if (actionViewIcon != null) {
        item.setActionView(R.layout.layout_draweractionview);

        ImageView imageView = item.getActionView().findViewById(R.id.imageButton);
        imageView.setImageResource(actionViewIcon);
        if (!mainActivity.getAppTheme().equals(AppTheme.LIGHT)) {
            imageView.setColorFilter(Color.WHITE);
        }/*from  www.  j  a  va  2  s.c  o  m*/

        item.getActionView().setOnClickListener((view) -> onNavigationItemActionClick(item));
    }
}

From source file:com.amaze.filemanager.ui.views.drawer.Drawer.java

private void addNewItem(Menu menu, int group, int order, String text, MenuMetadata meta, @DrawableRes int icon,
        @DrawableRes Integer actionViewIcon) {
    if (BuildConfig.DEBUG && menu.findItem(order) != null)
        throw new IllegalStateException("Item already id exists: " + order);

    MenuItem item = menu.add(group, order, order, text).setIcon(icon);
    dataUtils.putDrawerMetadata(item, meta);

    if (actionViewIcon != null) {
        item.setActionView(R.layout.layout_draweractionview);

        ImageView imageView = item.getActionView().findViewById(R.id.imageButton);
        imageView.setImageResource(actionViewIcon);
        if (!mainActivity.getAppTheme().equals(AppTheme.LIGHT)) {
            imageView.setColorFilter(Color.WHITE);
        }//from  www  . j av a 2 s.  c om

        item.getActionView().setOnClickListener((view) -> onNavigationItemActionClick(item));
    }
}

From source file:com.aboveware.actionbar.honeycomb.ActionBarHelperHoneycomb.java

@Override
public void setRefreshActionItemState(boolean refreshing) {
    // On Honeycomb, we can set the state of the refresh button by giving it a
    // custom action view.
    if (mOptionsMenu == null) {
        shouldBeRefreshing = refreshing;
        return;/*from w w  w  .  j a  v a  2s .c  om*/
    }

    final MenuItem refreshItem = mOptionsMenu.findItem(R.id.menu_refresh);
    if (refreshItem != null) {
        if (refreshing) {
            if (mRefreshIndeterminateProgressView == null) {
                LayoutInflater inflater = (LayoutInflater) getActionBarThemedContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mRefreshIndeterminateProgressView = inflater.inflate(R.layout.actionbar_indeterminate_progress,
                        null);
            }

            refreshItem.setActionView(mRefreshIndeterminateProgressView);
        } else {
            refreshItem.setActionView(null);
        }
    }
}

From source file:com.gmail.charleszq.picorner.ui.ImageDetailFragment.java

private boolean likePhoto(final MenuItem item) {

    // prepare the animator
    ProgressBar pb = new ProgressBar(getActivity());
    item.setActionView(pb);

    switch (mPhoto.getMediaSource()) {
    case PX500://  w  w  w.j ava  2s . co m
        if (SPUtil.getPx500OauthTokenSecret(getActivity()) == null) {
            Toast.makeText(getActivity(), getString(R.string.pls_sing_in_first), Toast.LENGTH_SHORT).show();
            return false;
        }
        break;
    case FLICKR:
        if (!SPUtil.isFlickrAuthed(getActivity())) {
            Toast.makeText(getActivity(), getString(R.string.pls_sing_in_first), Toast.LENGTH_SHORT).show();
            return false;
        }
        break;
    case INSTAGRAM:
        if (SPUtil.getInstagramUserId(getActivity()) == null) {
            Toast.makeText(getActivity(), getString(R.string.pls_sing_in_first), Toast.LENGTH_SHORT).show();
            return false;
        }
        break;
    }

    if (mLoadedBitmap == null) {
        return false; // image not loaded yet.
    }

    IGeneralTaskDoneListener<Boolean> lis = new IGeneralTaskDoneListener<Boolean>() {
        @Override
        public void onTaskDone(Boolean result) {
            item.setActionView(null);
            if (result) {
                mUserLikeThePhoto = !mUserLikeThePhoto;
                mPhoto.setUserLiked(mUserLikeThePhoto);
                Activity act = ImageDetailFragment.this.getActivity();
                if (act != null) {
                    act.invalidateOptionsMenu();
                }
                // broadcast messages
                Message msg = new Message(Message.LIKE_PHOTO, mPhoto.getMediaSource(), mPhoto.getId(),
                        mUserLikeThePhoto);
                MessageBus.broadcastMessage(msg);
            } else {
                if (getActivity() != null)
                    Toast.makeText(getActivity(), getString(R.string.msg_like_photo_fail), Toast.LENGTH_SHORT)
                            .show();
            }
        }
    };
    String likeActionString = Boolean.toString(!mUserLikeThePhoto);
    switch (this.mPhoto.getMediaSource()) {
    case FLICKR:
        FlickrLikeTask ftask = new FlickrLikeTask(getActivity(), lis);
        ftask.execute(mPhoto.getId(), likeActionString);
        break;
    case INSTAGRAM:
        InstagramLikePhotoTask igtask = new InstagramLikePhotoTask(getActivity(), lis);
        igtask.execute(mPhoto.getId(), likeActionString);
        break;
    case PX500:
        PxLikePhotoTask pxTask = new PxLikePhotoTask(getActivity());
        pxTask.addTaskDoneListener(lis);
        pxTask.execute(mPhoto.getId(), likeActionString);
        break;
    }
    return true;
}

From source file:com.hagreve.android.fragments.HaGreveListFragment.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void refreshItems(MenuItem item) {
    ConnectivityManager conman = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo netinfo = conman.getActiveNetworkInfo();
    if (netinfo != null && netinfo.isConnected()) {
        try {//from   ww  w  . j  a v  a 2 s. c  om
            //setListShown(false);
            if (item != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                LayoutInflater inflater = (LayoutInflater) getActivity()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                refreshView = inflater.inflate(R.layout.action_refresh, null);
                refreshItem = item;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    item.setActionView(refreshView);
                }
            }
            if (adapter == null) {
                adapter = new StrikeAdapter<Strike>(getActivity(), R.layout.strike_row);
                adapter.setNotifyOnChange(true);
                setListAdapter(adapter);
            }

            getLoaderManager().restartLoader(0, null, this);
        } catch (Exception e) {
            Log.e(LOG_TAG, e.toString());
        }
    } else {
        Log.d(LOG_TAG, "No network connection available");
        View progressbar = getView().findViewById(R.id.loading_progress);
        progressbar.setVisibility(View.INVISIBLE);
        TextView loading_text = (TextView) getView().findViewById(R.id.status_text);
        loading_text.setText(R.string.no_connection);
    }
}

From source file:com.abewy.android.apps.klyph.app.MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    if (notificationsFragment != null && menu.findItem(R.id.menu_notifications) == null) {
        final MenuItem notificationItem = menu.add(Menu.NONE, R.id.menu_notifications, 2,
                R.string.menu_notifications);
        notificationItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        notificationItem.setActionView(R.layout.actionbar_item_notifications);

        final TextView notificationTextView = (TextView) notificationItem.getActionView()
                .findViewById(R.id.textView);

        int count = notificationsFragment.getUnreadCount();

        notificationTextView.setText(String.valueOf(count));

        if (count > 0) {
            notificationTextView//from   w w  w.  j  a va 2  s.c  o  m
                    .setBackgroundResource(AttrUtil.getResourceId(this, R.attr.notificationsItemBackground));
        } else {
            notificationTextView.setBackgroundResource(R.drawable.notifications_read_background);

        }

        notificationItem.getActionView().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onOptionsItemSelected(notificationItem);
            }
        });
    }

    if (!KlyphFlags.IS_PRO_VERSION && menu.findItem(R.id.menu_buy_pro) == null) {
        menu.add(Menu.NONE, R.id.menu_buy_pro, 2, R.string.menu_buy_pro)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }

    if (menu.findItem(R.id.menu_faq) == null) {
        menu.add(Menu.NONE, R.id.menu_faq, Menu.NONE, R.string.menu_faq)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }

    return super.onCreateOptionsMenu(menu);
}