Updating option menu item which is currently showing - android

Because of some reasons, some menu item is disabled and will be enabled after the data is arrived.
Here is the case that I conern:
When the menu is current showing to user and the data is arrived, how can I enable the menu item instantly?
Now I only enable/disable menu item in onPrepareOptionsMenu(), it is only called when menu is shown again. FYI, I am using android 2.x SDK
Thank You

When the opPrepareOptionsMenu is called, it gets a reference to the Menu, so you can save this reference in a Variable, and when the data is ready add or enable de option again.

I have already done this and it is working fine. Take a look at this code...
While creating your menu add different group to each of your menu option so that you can easily handle each button seperately. like : I am giving you example of hold and resume button. One time only one button will work so how to do that here is code.
Declare this in your class.
private static final int HOLD_CALL = 0;
private static final int RESUME_CALL = 1;
Write this code in your public boolean onCreateOptionsMenu(Menu menu)
menu.add(0, HOLD_CALL, 0, "Hold Call");
menu.add(1, RESUME_CALL, 1, "Resume Call");
Use this in your public boolean onMenuOpened(int featureId, Menu menu)
menu.setGroupEnabled(1, false);`
the above code will disable your menu option. Hope it will help you If still you are facing problem then let me know i will try to help you...

Related

How to programmaticallly remove submenu from navigation drawer in Android?

I am developing an Android app. Firstly, let me tell you that I am not professional. What I am doing now is I am adding submenu to menu depending on a condition. But I need to do it very often in my app. But my problem is I added a submenu to the menu as first time.
But second time when I update menu depending on condition, existing submenu is not removed and new submenu is appended to navigation drawer. How can I remove submenu that is programmatically added to menu? Why my code is not removing it?
Here is my code
public void updateAuthUI()
{
isLoggedIn = tempStorage.getBoolean(getResources().getString(R.string.pref_is_logged_in),false);
Menu menu = leftDrawer.getMenu();
menu.removeItem(getResources().getInteger(R.integer.logout_item_id));
menu.removeItem(getResources().getInteger(R.integer.login_item_id));
menu.removeItem(getResources().getInteger(R.integer.register_item_id));
SubMenu authSubMenu = menu.addSubMenu("Auth");
if(isLoggedIn)
{
authSubMenu.add(1,getResources().getInteger(R.integer.logout_item_id),99,"Sign out");
}
else{
authSubMenu.add(1,getResources().getInteger(R.integer.register_item_id),97,"Register");
authSubMenu.add(1,getResources().getInteger(R.integer.login_item_id),98,"Sign in").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openLoginActivity();
item.setChecked(true);
return true;
}
});
}
}
Here is the screenshot of my problem
As you can see Auth submenu is appended without removing existing one.
Try
authSubMenu.clear();
before your first
authSubMenu.add();
I just used SubMenu.clear() in an Android app where I was using a third-party library, and I needed to clear out an unwanted submenu from the action bar. (I actually wanted to remove the submenu completely, and this was the only way I could find to do it. It seemed to work.)
That's different from your situation, where authSubMenu is a menu you just added via menu.addSubMenu("Auth"), so you would expect it to be empty. But, as you've seen, it apparently isn't empty: rather, addSubMenu("Auth") returns the existing submenu of that title. (I can't find documentation to that effect; I'm just going by the results you've reported.)
If that really is the case, as it appears to be, then authSubMenu.clear() will remove all existing items from the submenu.
As #slymm said in a comment you can remove all menu and submenu items using
navigationView.getMenu().clear();
This can be used to remove submenu (and menu elements) and then recreate them with the new required items

sub menu loads on button press aand has an editable text box ANDROID

I am currently trying to add a sub menu and get it to do what I want. I've been looking online and can't seem to see if it's possible.
Right basically I have a button called 'sign-in' which the users will press to lad the submenu in which will have a label saying Username and then an editable text box allowing the user to type in their desired user name.
Is this possible?
The code I currently have is
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
TextView txtUsername = (TextView) findViewById(R.id.txtUsername);
EditText edit_username = (EditText) findViewById(R.id.edit_username);
super.onCreateOptionsMenu(menu);
SubMenu sub = menu.addSubMenu(0,1,0, "Sign-In");
sub.add(0,11,0,edit_username); //I have noticed this is wrong but the only way I thought it would work
sub.add(0,12,0,txtUsername); //same for here
//add button for okaying the username
return true;
}
obviously this is in the create options menu but like I said I would like it to load the menu when the user clicks sign in using the signIn void
public void signIn(View view)
{
//load menu here
}
You can add any number sub menus to the menu items but you cannot nest a sub menu. (Reference)
Menus with sub menus are used in rare cases where there are a number of related sub menu items for a menu item.
You can add a submenu to an item in any menu (except a submenu) by adding a element as the child of an . Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.).
Refer this document to learn more about options menu.
I don't see a point in adding the user name and password fields to an options menu. You could use a sub activity or a dialog fragment to display those fields.
In your code, you have two problems.
SubMenu sub = menu.addSubMenu(0,1,0, "Sign-In");
sub.add(0,11,0,edit_username);
sub.add(0,12,0,txtUsername);
you cannot nest submenus.
add (int groupId, int itemId, int order, CharSequence title) obviously takes CharSequence as parameter not the View widgets (your edittext and textview).

master/detail template with action menu - right way to show both

Beginner here, targetting sdk v14 and v17 for my learning...no need for older support.
I am using the master/detail template and trying to get an action menu (for SEARCH) to show up both in phone and tablet view. Actually I can get it to work, but I have to duplicate up my code in both ItemDetailActivity.java and ItemListActivity.java
These are the methods that I have to have in both for SEARCH to work:
public boolean onQueryTextChange(String newText) {
public boolean onQueryTextSubmit (String query) {
public boolean onClose () {
I only want to search the "detail", not the "list".
So my question: is there a way to associate the action bar with only the list fragment? That way I can keep the search functions in 1 file.
Thanks!
I'll go ahead and answer what I (think) I know as I don't want to leave this question open.
From tracing in the debugger, it looks to me like the phone activity and the tablet activity are separate and if you want to hook up an actionmenu, you have to hook it up to both separately.

android - change options menu dynamically , but by inflating from XML

i need to be able to change the options menu (the one that is shown upon pressing the menu button) on android , so that on one case (for example upon a button being pressed) , it will use a specific menu resource (XML file as in /res/menu/... ) for the menu , and on another case , use a different XML file.
so far i've seen only examples of doing it without xml (example here and here) , and they worked fine , but i want to be able to change the entire menu on some cases.
i've tried to modify the solutions i've found , but none of my trials worked.
if possible , i would prefer to re-create the menu only if the it needs to be updated with a menu resource that is different from the current one.
please help me.
If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method.
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear();
if (CASE_1 == 0) {
CASE_1 = 1;
getMenuInflater().inflate(R.menu.secondmenu, menu);
}
else {
CASE_1 = 0;
getMenuInflater().inflate(R.menu.firstmenu, menu);
}
return super.onPrepareOptionsMenu(menu);
}
where CASE_1 refer to the which menu you want to display depending on your requirement.

Getting selected text in a WebView via a contextual action bar

It's known to be difficult to get selected text in a WebView because WebView text selection is actually handled by a private class, WebTextView.
However, with the recently released Android 4.0 Design guidelines, there seems to be a glimmer of hope of achieving this through contextual action bars (CABs). It says:
Use CABs whenever you allow the user to select data via long press. You can control the action content of a CAB in order to insert the actions you would like the user to be able to perform.
Am I misinterpreting this? Is there a way to retrieve selected text from a WebView via a CAB?
After a long click and text selection mode begins, I can currently detect when the ActionMode starts and modify the original copy/paste Menu; however, I can't quite figure out how to actually retrieve the selected text.
You can't do that yet with the current API.
I filed a feature request for this - Issue 24841: WebView should allow applications to supply a custom Contextual Action Bar http://code.google.com/p/android/issues/detail?id=24841
Basically, WebView in 4.0 has hardcoded its own Contextual Action Bar (CAB). That CAB has a reference back to the WebView and with that reference, it can get the selected text. I'm not sure how you were able to detect the ActionMode starting and modify the menu, but if you were able to do all of that, then you are stuck because getSelection() is package-private currently. I filed that as a separate issue and linked it to the previous issue above.
You can use javascript to get the selected text: window.getSelection(), and use WebView's addJavascriptInterface function to return the result.
thanks for your information, I have solved a hard issue..
I just want to add some function into the actionmode.
The following is my code, May be helpful to others.
#Override
public ActionMode onWindowStartingActionMode(Callback callback) {
// TODO Auto-generated method stub
ActionMode mode = super.onWindowStartingActionMode(callback);
mode.getMenuInflater().inflate(R.menu.actions, mode.getMenu());
mode.getMenu().findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Log.i("", "onMenuItemClick add ");
return false;
}
});
return mode;
}

Categories

Resources