Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically fire action bar menu item in Android

I have actionbar with menu item on the bar. When I click on the refresh icon I have method that shows up the progress bar.

I would like to do the on loading of this activity. Hence, I tried calling the refresh icon item click programatically:

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

I am calling the above after creating the menu.

But this gives null pointer exception on load my data. If I click on refresh button it is working fine, but if I call it programmatically I get an error.

Here is 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);
            }
        }
    }
}

Is it possible to call a menu item programmatically? if so how?

Thanks!

like image 684
TheDevMan Avatar asked Dec 25 '22 04:12

TheDevMan


2 Answers

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

or

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

like image 197
Pedro Oliveira Avatar answered Jan 05 '23 23:01

Pedro Oliveira


Why are you calling it from onCreateOptionsMenu?

You can write the code for loading in onCreate or onResume method 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);
}
like image 38
Pramod Ravikant Avatar answered Jan 05 '23 21:01

Pramod Ravikant