My Action bar items were working fine until I added a menu drawer, now when the activity is first displayed, the switch button on the menu doesn't work and as soon as the drawer opens it starts working absolutely fine. Although I have not called it in my onDrawerOpened method.
// ----------For Options Menu-------------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
super.onCreateOptionsMenu(menu);
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen_menu, menu);
menuitem1 = menu.findItem(R.id.menu_item1);
menuitem2 = menu.findItem(R.id.menu_item2);
final Switch getView = (Switch) menuitem2.getActionView();
getView.setChecked(false);
getView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean isSelected) {
if (isSelected) {
method1();
} else {
method2();
}
}
});
} catch (Exception e) {
e.printStackTrace();
Log.e("OnCreateOptionsMenu", "exception", e);
}
mOptionsMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.menu_item1: {
return true;
}
case R.id.menu_item2: {
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
Once I open and close the Drawer than the switch is working perfectly!
Can anyone please help?
Thanks in advance.
You're supposed to call super.onCreateOptionsMenu(menu) after having inflated the menu (as shown in the docs : http://developer.android.com/guide/topics/ui/actionbar.html)
I would change your code to :
// ----------For Options Menu-------------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen_menu, menu);
(...)
return super.onCreateOptionsMenu(menu);
}
(Not totally sure that it could fix your issue, but it's worth the try)
Related
I have an activity with menu items. Every time when user come to this activity, i want to update the value of textView with some Utility value. This is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my2, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText(String.valueOf(Utility.ShoppingCartItemCount()));
return true;
}
This is updating the value only once (when launching activity). But when user moves from this activity and come on this activity again, this is not getting updated even value for Utility.ShoppingCartItemCount() is updated.
How to solve this?
Try invalidateOptionsMenu()
This will be call onCreateOptionsMenu(Menu) again.
edit)
I have some code like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (currentPage == 8) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_p, menu);
return true;
} else {
return false;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.purchaseRestore:
// TODO: Restore purchase
return true;
case R.id.purchaseTerm:
// TODO: Show Term by WebView
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When my user click other button, put invalidateOptionsMenu() after currentPage = 8 in Button's OnClickListener. In result, User can show Option Menu.
edit))
invalidateOptionsMenu() will force reload onCreateOptionsMenu and onPrepareOptionsMenu.
If this methods doesn't working for you, try to debug Utility.ShoppingCartItemCount().
I am having list of Colleges to display and click the perticular college it goes to the detail page. In there there is a menu bar with notification and Favourite_icon.Here i am having the Favourite_icon and Favourite_icon1. If users clcik the favourite_icon it stored as favourited in server and the icon changed as Favourite_icon1. After do some process i have visted the Favourited college. That time it should show the Favourite_icon1 in menu bar. I have tried the following method but nothing happend . I have added the the code which i have tried
menu_clg.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
this Activity code
private boolean canAddItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clg, menu);
mMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
if (item.getItemId() == R.id.action_favourite) {
invalidateOptionsMenu();
favouriteClg();
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (canAddItem) {
menu.getItem(1).setIcon(R.drawable.vijay);
canAddItem = false;
favouriteClg();
} else {
menu.getItem(1).setIcon(R.drawable.favourite_icon);
canAddItem = true;
favouriteClg();
}
return super.onPrepareOptionsMenu(menu);
}
this is the code for check wheather the college is already favourited or not in onCreate() method
public void chechFavourite() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... arg0) {
//method name changed here
//getAllEventFromUser method used for getting all previously send events of current user
return favouriteDelegates.getAllCollegeDetails(userMO, context);
}
#Override
protected void onPostExecute(String collegelists) {
if (collegelists != "null") {
initCollegeMO = gson.fromJson(collegelists, new TypeToken<InitCollegeMO>() {
}.getType());
collegeMOs = initCollegeMO.getCollegeMOs();
for (CollegeMO collegeMO1 : collegeMOs) {
//here the list of college has eceived from server so i checked all the college id with current college id
collegeId = collegeMO1.getCollegeId(); //here collegeMO isthe object which is accessed by parcelable from another activity
if (collegeMO.getCollegeId() == collegeId) {
canAddItem = true;
} else {
canAddItem = false;
}
}
} else {
canAddItem = false;
}
}
}.execute(null, null, null);
}
}
Hope below code would be helpful for you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.actionbar, menu );
userItem = menu.findItem(R.id.userItem);
return menu;
}
Edit 1:
Method which will update menu programatically.
private void updateMenuTitles() {
if(isFavorite){
userItem.setIcon(new BitmapDrawable(getResources(), favoriteBitmap));
}else{
userItem.setIcon(new BitmapDrawable(getResources(), unFavoriteBitmap));
}
invalidateOptionsMenu();
}
Hope this would help you.
You can change the title of your MenuItem runtime and manage the click event according to their title.
See this code, hope it will help you.
You can call updateMenuTitles() after getting response from API and change the title.
private Menu menu;
#Override
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);
// Create your menu...
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourite:
if (item.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
// do what you want
}
else if(item.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
// do what you want
}
return true;
}
return super.onOptionsItemSelected(item);
}
/*
Update title of your menuItem whenever you want
*/
private void updateMenuTitles() {
MenuItem MenuItem = menu.findItem(R.id.action_favourite);
if (MenuItem.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
}
else if(MenuItem.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
}
}
I have implemented Navigation Drawer with ActionBar which has only one item which opens the navigation drawer. So both the Home and menu item buttons open the drawer. And all works fine with no crashes.
But when I click on the ActionBar Home button or menu item, the Navigation drawer opens and menu item becomes invisible.
How can I prevent this? I want it be always visible, regardless if drawer is open or closed.
I tried multiple solutions already. For example, in the drawer class, method setHasOptionsMenu(true); is called in onCreate. Then I set menu item to be present always.
Then in the main Activity, I tried to capture the menu item during the creation
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
try {
MenuItem item = menu.getItem(0);
if (item != null) {
item.setVisible(true);
}
} catch (Exception e) {
Log.e(TAG, "Error: " + e.getLocalizedMessage());
}
return super.onPrepareOptionsMenu(menu);
}
but when drawer appears, the menu object return with 0 items.
I am out of ideas.
EDIT
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen())
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
This is RIGHT-SIDE Navigation drawer.
this is your problem
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen())
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
you dont have a menu when the drawer opens, remove that if statement
When controlling a MenuItem, I have been doing this:
Menu menu;
(...)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
return true;
}
(...)
public void handleSearch(View view) {
Button button = (Button) view;
if(menu.findItem(R.id.action_search).isVisible()) {
button.setText(R.string.button_search_show);
menu.findItem(R.id.action_search).setVisible(false);
} else {
button.setText(R.string.button_search_hide);
menu.findItem(R.id.action_search).setVisible(true);
}
}
"this" being referencing the menu created in onCreateOptionsMenu with a Menu that any method in the class can use. The handleSearch method controls MenuItems by using findItem twice. This doesn't feel very conventional or efficient (a very scientific observation, I might add). Is there a more conventional or efficient way to do this?
You can save MenuItem in a variable instead of using findItem twice.
MenuItem myMenuitem = menu.findItem(R.id.action_search);
There is a method that handles actionBar's menu itself.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return (true);
case R.id.menu_home:
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
return (true);
}
return (super.onOptionsItemSelected(item));
}
In Android if the textview is selected then a word gets selected and a contextual action bar comes at the top...i want to modify that CAB and make it look like a quick action bar...keeping the text selection feature intact...please help me...
you can select textview or anything like only in simple layout CAB appear in the top and there was some method onActionItemClicke that you want to click an item and then you modify your action in CAB default behavior................................
public class MainActivity extends Activity {
protected Object mActionMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.lay);
view.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = MainActivity.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// Assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.cab, menu);
return true;
}
// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
Toast.makeText(MainActivity.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.shan:
Toast.makeText(MainActivity.this, "Selected shani",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
and in menu xml file create menu that you would like to appear in top CAB
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/toast"
android:title="Toast">
</item>
<item
android:id="#+id/shan"
android:title="shani">
</item>
</menu>