Reputation: 5954
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!
Upvotes: 4
Views: 5484
Reputation: 7484
// 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 :)
Upvotes: 0
Reputation: 1890
You can try this :
get the reference of 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 click
Upvotes: 2
Reputation: 20500
Just do findViewById(R.id.action_Refresh).callOnClick();
or
findViewById(R.id.action_Refresh).performClick();
Upvotes: 6
Reputation: 1037
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);
}
Upvotes: 2