Programmatically configure action bar menu item in Android

I have an action bar with a menu item in the bar. When I click on the refresh icon, I have a method showing a progress bar.

I would like to do this when loading this activity. Hence, I tried calling the update icon element programmatically:

onOptionsItemSelected(menu.findItem(R.id.action_Refresh)); 

      

I call this after the menu is created.

But this gives a null pointer exception to load my data. If I click the Refresh button, it works fine, but if I call it programmatically, I get an error.

Here's what I have:

 @Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    this.optionsMenu = menu;
    getMenuInflater().inflate(R.menu.main, menu);

    onOptionsItemSelected(menu.findItem(R.id.action_Refresh));

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case R.id.action_about:
        aboutapp();
        return true;

    case R.id.action_Refresh:
        Log.e("REfressh","Clicked");
        Mapsthree.refreshValue = 0;
        timer = new Timer();
        timerMethod();
        setRefreshActionButtonState(true);
        displayView(1);
        return true;

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

private void timerMethod()
{
    timer.scheduleAtFixedRate(new TimerTask() 
    {
        @Override
        public void run() 
        {
            updateProgressBar();
        }

    }, 0, 800);
}

private void updateProgressBar() 
{
    runOnUiThread(new Runnable() 
    {
        public void run() 
        {
            if (Maps.refreshValue == 1)
            {
                setRefreshActionButtonState(false);
                timer.purge();
                timer.cancel();
            }
        }
    });
}

public void setRefreshActionButtonState(final boolean refreshing) 
{
    if (optionsMenu != null) 
    {
        final MenuItem refreshItem = optionsMenu.findItem(R.id.action_Refresh);
        if (refreshItem != null) 
        {
            if (refreshing) 
            {
                refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
            } 
            else 
            {
                refreshItem.setActionView(null);
            }
        }
    }
}

      

Can a menu item be called programmatically? if so, how?

Thank!

+3


source to share


4 answers


Why are you calling him from onCreateOptionsMenu

?

You can write the code to load in a method, onCreate

or onResume

depending on the requirement:



@Override
protected void onCreate(Bundle arg0) {

    super.onCreate(arg0);

    //whatever you are doing

    //now code for refresh

    Log.e("REfressh","First time");
    Mapsthree.refreshValue = 0;
    timer = new Timer();
    timerMethod();
    setRefreshActionButtonState(true);
    displayView(1);
}

      

+2


source


Just do findViewById(R.id.action_Refresh).callOnClick();

or



findViewById(R.id.action_Refresh).performClick();

+6


source


You can try this
to get a link to the menu class opMenu

inside your activity here:

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

    onOptionsItemSelected(opMenu.findItem(R.id.action_Refresh));

    return super.onCreateOptionsMenu(menu);;
}  

      

Use opMenu.findItem()

to trigger push

+2


source


// check is assignable to menu item implementation
if(menuItem !=null 
    && MenuItemImpl.class.isAssignableFrom(menuItem.getClass())){
  // if so cast 
  MenuItemImpl impl = (MenuItemImpl)menuItem;
  // invoke listener 
  impl.invoke();
}

      

which is equal to implementation;

public boolean invoke() {
    if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
        return true;
    }

    if (mIntent != null) {
        mContext.startActivity(mIntent);
        return true;
    }
    return false;
}

      

you can always use reflections :)

0


source







All Articles