Example usage for android.support.v4.view MenuItem setActionView

List of usage examples for android.support.v4.view MenuItem setActionView

Introduction

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

Prototype

MenuItem setActionView(View view);

Source Link

Document

Set an action view for this menu item.

Usage

From source file:com.actionbarsherlock.sample.styledactionbar.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // TODO handle clicking the app icon/logo
        return false;
    case R.id.menu_refresh:
        // switch to a progress animation
        item.setActionView(R.layout.indeterminate_progress_action);
        return true;
    case R.id.menu_both:
        // rotation animation of green fragment
        rotateLeftFrag();//w ww . j  av a  2 s . com
    case R.id.menu_text:
        // alpha animation of blue fragment
        if (IS_HONEYCOMB) {
            ObjectAnimatorAlpha.invoke(rightFrag.getView());
        } else {
            AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
            alpha.setRepeatMode(Animation.REVERSE);
            alpha.setRepeatCount(1);
            alpha.setDuration(800);
            rightFrag.getView().startAnimation(alpha);
        }
        return true;
    case R.id.menu_logo:
        useLogo = !useLogo;
        item.setChecked(useLogo);
        getSupportActionBar().setDisplayUseLogoEnabled(useLogo);
        return true;
    case R.id.menu_up:
        showHomeUp = !showHomeUp;
        item.setChecked(showHomeUp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(showHomeUp);
        return true;
    case R.id.menu_nav_tabs:
        item.setChecked(true);
        showTabsNav();
        return true;
    case R.id.menu_nav_label:
        item.setChecked(true);
        showStandardNav();
        return true;
    case R.id.menu_nav_drop_down:
        item.setChecked(true);
        showDropDownNav();
        return true;
    case R.id.menu_bak_none:
        item.setChecked(true);
        getSupportActionBar().setBackgroundDrawable(null);
        return true;
    case R.id.menu_bak_gradient:
        item.setChecked(true);
        getSupportActionBar()
                .setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:com.actionbarsherlock.sample.styledactionbar.MainActivity.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 = 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);
                }/*from   ww w  .  j a v  a2 s . co  m*/
            }, 1000);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

From source file:com.actionbarsherlock.sample.demos.app.ActionBarActionItemCustomView.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add(0, android.R.id.copy, 0, "Test");

    final int twentyDp = (int) (20 * getResources().getDisplayMetrics().density);

    TypedArray a = getTheme().obtainStyledAttributes(R.styleable.SherlockTheme);
    final int abHeight = a.getLayoutDimension(R.styleable.SherlockTheme_abHeight, LayoutParams.FILL_PARENT);
    a.recycle();/* w  ww .  j av  a2  s . c  o  m*/

    LinearLayout l = new LinearLayout(this);
    l.setPadding(twentyDp, 0, twentyDp, 20);
    l.setBackgroundColor(0x55FF0000);

    TextView tv = new TextView(this);
    tv.setText("HI!!");
    tv.setGravity(Gravity.CENTER);
    tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, abHeight));
    l.addView(tv);

    l.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ActionBarActionItemCustomView.this, "Got custom action item click!",
                    Toast.LENGTH_SHORT).show();
        }
    });

    item.setActionView(l);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    return super.onCreateOptionsMenu(menu);
}