action bar Menu Item - android

in my app I have a refresh button. clicking on it it rotates (animation) while the fragment is updating. suppose I navigate from the given fragment to another and then back. Is there any possibility when I navigate back the refresh Menu Item being clicked automatically without pressing on it.
menu item onclick method
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
item.setActionView(getAnimation());
getCurrency(code,item);
mViewpager.setVisibility(View.INVISIBLE);
Log.i("TAG", "refresh pressed =>");
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
I want this code to execute when navigating back without pressing the menu Item

Yes, you can execute all the actions your are doing when the optionMenu creation method starting from the animation and the real updating as following:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
//Perform the updating action
item.setActionView(getAnimation());
getCurrency(code,item);
mViewpager.setVisibility(View.INVISIBLE);
Log.i("TAG", "refresh pressed =>");
super.onOptionsItemSelected(item);
return true;
}

Related

how can i change the visibility of a menu item inside onOptionsItemSelected(..)

i have three menu items "enable, diable, exit". what i want to do is, when the R.id.menu_enable_bt is chosen, i want to disable it using
menu.findItem(R.id.menu_enable_bt).setVisible(false);
but i can not call
menu.findItem(R.id.menu_enable_bt).setVisible(false);
from inside the onOptionsItemSelected(..) method.
how can i change the visibility of a menu item inside onOptionsItemSelected(..)
CODE:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
Log.w(TAG, SubTag.msg("onCreateOptionsMenu"));
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
if (this.mBTAdapter.isEnabled()) {
menu.findItem(R.id.menu_enable_bt).setVisible(false);
menu.findItem(R.id.menu_disable_bt).setVisible(true);
} else {
menu.findItem(R.id.menu_enable_bt).setVisible(true);
menu.findItem(R.id.menu_disable_bt).setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Log.w(TAG, SubTag.msg("onOptionsItemSelected"));
switch (item.getItemId()) {
case R.id.menu_enable_bt:
Log.d(TAG, SubTag.bullet("onOptionsItemSelected", "menu_enable_bt"));
//menu.findItem(R.id.menu_enable_bt).setVisible(false); **how to do this**
this.mATEnableBT = new ATEnableBT();
this.mATEnableBT.execute();
break;
case R.id.menu_disable_bt:
Log.d(TAG, SubTag.bullet("onOptionsItemSelected", "menu_disable_bt"));
break;
case R.id.menu_exit:
Log.d(TAG,SubTag.bullet("onOptionsItemSelected", "menu_exit"));
finish();
break;
}
return super.onOptionsItemSelected(item);
}
you can use invalidateOptionsMenu() which will forse onCreateOptionsMenu to be called again.
From the documentation
Declare that the options menu has changed, so should be recreated. The
onCreateOptionsMenu(Menu) method will be called the next time it needs
to be displayed.
There you can check your conditions and undertake the needed actions
See the javadoc for onPrepareOptionsMenu. This is your opportunity to make changes to a menu just before it becomes visible to the user. So you should maintain some member variables describing what should be visible and use that to modify the menu items here.

Home Back button in Navigation bar doesn't work in my android app

After activating Navigation Up Button in Action Bar, Home Back button in Navigation bar doesn't work in my app. Where am I doing wrong? when I click on the navigation app up button, it works. But, Navigation back button as you can see on the pivture doesn't work...
Back Button
enter image description here
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_email:
emailMenuItem();
break;
case R.id.action_settings:
settingsMenuItem();
break;
}
return true;
}
First understand following things:
#Override
public void onBackPressed() {
super.onBackPressed();
// executed this when hardware back button is pressed
}
is called when you press back button.
AND
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
// executed this when back button on Actionbar is pressed
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
is called when you press back button on Actionbar.
Comment below if you have any queries.

How to replace menu dynamically with another in Android

I have a ListActivity (SherlockListActivity) and its content can be dynamically changed by the user. When the content changes, the options menu should be replaced.
As I learned, instead of onCreateOptionsMenu I should use onPrepareOptionsMenu that is (supposed to be) called every time the user selects the menu.
This is called right before the menu is shown, every time it is shown.
I have the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.clear();
if (UserOption == UserOption1)
getSupportMenuInflater().inflate(R.menu.menu_option1, menu);
else
getSupportMenuInflater().inflate(R.menu.menu_option2, menu);
return super.onPrepareOptionsMenu(menu);
}
It is called only once during debug, so I always have the same menu.
What should I set to make it work?
Create and prepare the options menu for changing and its item selection method
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if(menuString=="red"){
inflater.inflate(R.menu.red_menu, menu);
}else if(menuString=="green"){
inflater.inflate(R.menu.green_menu, menu);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
return true;
case R.id.help:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Like whenever you want to change menu call
String menuString="red or green";
invalidateOptionsMenu();
Like others told, if you want to have static menu use onCreateOptionsMenu, and if you want to change its visibility dynamically use onPrepareOptionsMenu along with onCreateOptionsMenu

Android: Use icon as back button without reloading previous activity

I have the following code to enable to home button to act as a back button. The problem I'm facing is from this activity if I use the real back button it simply goes back to the previous activity just as I left it. If I use the home button it's reloading the page so I lose what was previously done to it. I'm sure it's something simple I'm missing.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.census_management_search, menu);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, CensusManagementActivity.class);
NavUtils.navigateUpTo(this, intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Instead of Intent and NavUtils try using onBackPressed() method.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The Home/Up button is supposed to reload the new activity. However, if you want to emulate the back button functionality, you can call finish() to return to the previous activity:
case android.R.id.home:
finish();
return true;

option menu not clicked at first time in sherlockfragment

I have used option menu in my fragment.The problem is that when i go first time to fragment ,the option menu click event is not called.But when i go to another fragment.and again revist that fragment then the option menu click event is called...
following is the code
//Creating the option menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.newcarmenu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
break;
}
return super.onOptionsItemSelected(item);
}
please tell me why this happen?
Right now, you are returning super.onOptionsItemSelected(item) every time, so the selection is being passed on. You need to return true when your MenuItem has been selected. Try this instead:
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
return true;
default:
return super.onOptionsItemSelected(item);
}

Categories

Resources