I'm using the Navigation Drawer Example downloadable here.
I'd like to extend this example to disable the selection of one of the planets. For simplicity, lets say I want to permanently disable the selection of Saturn, and change the text of Saturn to dark gray, and have it not highlight when a user selects it. (in reality, I would like to disable navigation programmatically when a user has changed certain values on the screen not yet saved to the device).
The closest thing I've gotten to this is to stop the selectItem() method from being called from within the onItemClick click listener, but an issue remains even if I do this - the text of "Saturn" still appears selectable and highlights when a user clicks it.
What portion of the widgets do I need to change to prevent the text of Saturn from being highlighted?
I've tried changing
mDrawerLayout.setClickable(false);
mDrawerList.setClickable(false);
But neither of these options appear to have any affect.
Any suggestions or ideas as to how to approach this problem?
A couple helpful notes:
I'm aware that alternatively I can Set the Drawer Lock Mode to prevent the navigation drawer from being opened, but this is much less intuitive than having grayed out nav actions
Is the more appropriate thing to do here to remove the items from the list that cannot be accessed at this time? Similar to as described in the Remove ListView implementation here? The biggest disadvantage to this is that I would probably have to change the listview to describe why items have been removed from this list temporarily.
I'd like to extend this example to disable the selection of one of the planets
Create your own subclass of ArrayAdapter where you override areAllItemsEnabled() to return false and isEnabled() to return true or false as needed.
Related
I have a problem.
I have an action bar. It has a drop down list and it has an overflow button and another button some where in between.
Because I use a drop down, I am setting the action bar to use this mode:
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mActionBarNavigationAdapter, mOnNavigationListener);
mActionBar.setDisplayShowTitleEnabled(false);
My mOnNavifationListener is set to call a REST api depending on which item I chose in the drop down list.
This works as excepted.
The problem is, if I click on the overflow button to reveal extra options (such as settings or more importantly, Signout), I still get the navigation listener activated.
So I thought. Ok let's try to distinguish between the buttons using their position or id which are passed as parameters in onNavigationItemSelected method.
So I added an if statement that checks the position parameter. But...
It seems that the position of the overflow button is ALSO 0 (exactly like the position of the first item in the drop down list, so it passes the check and calls the REST api which is not good.
Also, the third button (not the drop down list or the overflow), has a position of 1 which effectively calls the other REST api...
I can't find a way to distinguish between the items in the action bar.
Any suggestions?
First, I'd strongly consider replacing your use of list navigation, as that is officially deprecated in the "L" Developer Preview and should remain deprecated in the next shipping version of Android.
I still get the navigation listener activated
I am not clear on what you mean by this. I also don't know which action bar you are referring to:
native API Level 11+?
appcompat_v7?
ActionBarSherlock?
Ideally, onNavigationItemSelected() will be called when your activity first appears, then if the user chooses something from the Spinner. If you are saying that it is being called at other points in time, while unfortunate, this can be worked around by simply keeping track, in your own data member, of where you are in the Spinner, and only doing work if the Spinner position actually has changed. So, for example, if you start off showing position 0, and the user taps something which causes onNavigationItemSelected() to be called again with position 0, you know that nothing needs changing, because you are still on position 0.
I want an acitivy to show a Contextual Action Bar, not normal ActionBar. The reason is that I show this activity in resonse for user long-clicking an item.
I know how to hide title and in general make the normal ActionBar look similar to CAB, but not quite. May be there is a simple way of doing it?
Update:
My question is similar, but not a complete duplication of how to invoke the actionbars context menu like behavior question
The difference is that I dont want at all to show a "normal" ActionBar, always the one that looks like Contextual ActionBar.
The suggestion to move to ActionMode right in the activity onCreate.
ActionMode.Callback onDestroyActionMode() that is called when ActionMode ends, we would simply finish() the activity.
It is a good one and almost does it. There are two subtle problems with this approach.
One, there is an observable flicker when ActionBar shows and immediately goes to CAB.
Second, the more substatial one is that there is no way to differentiate between edits that we want to "accept" and "decline". I would want checkmark mean that edits need to be accepted, and "back" button mean that edits need to be discarded. Unfortunately the suggested hack results in both routes ending with the call to onDestroyActionMode()
I could have added more CAB action items, for saving or exiting without saving, but this defeats the purpose of having separate editing activity.
I want to provide simple formatting buttons for an EditText, for example, a button that inserts <strong></strong> if no text was selected and wraps the selected text in <strong> tags otherwise.
I am using the Support Library v7 with ActionBarCompat. I managed to start a custom ActionMode upon focus change of my EditText. However, when I long-touch on text in that EditText, another ActionMode menu opens with copy/paste/... buttons, which presumably is Android's default behaviour. How can I suppress this second menu, but still let the user select portions of the EditText's content?
Alternatively I would like to be able to customize the default menu using setCustomSelectionActionModeCallback(...) but using the Support library. How could I do that?
Unfortunately, TextView and EditText don't provide an API for you to use ActionBarCompat classes. But, you should be able to override this behavior.
Set a custom onLongClickListener in which you set your own state information in your activity and call supportInvalidateOptionsMenu(). Be sure to return 'true' from your OnLongClickListener.onLongClick() method, indicating you consumed the click. Then, in your onPrepareOptionsMenu you can add whatever new menu items you wish for your custom action bar. I would recommend not using TextView context menu items IDs 'selectAll', 'cut', 'copy' and 'paste', just to be on the safe side.
I wanted to achieve the same sort of functionality found in Google Now where you can tap the cards overflow icon and a menu appears next to it with options (shown here).
Does anyone know how to implement this sort thing to achieve the same style etc?
I think you can do this by placing a listview under your button, and set its visibility to invisible.
Then place an onclicklistener on your button to turn your visibility to visible.
I have an app that in a certain Activity (drawing) can be in any one of a number of states, such as draw, erase, select, etc.
To enable these states, I've got a button for each in the ActionBar, but one thing I'd like to do is to 'show' the user which state is enabled by keeping the button pressed active or pressed until I turn it off (when they have switched state by pressing one of the other buttons).
Searches here and other places have me coming up blank... can anyone recommend a possible solution? I've though about rolling my own toolbar, and while this might be my final solution, using the ActionBar would speed things up greatly at this point.
Thanks.
There is no direct way to do that, as far as I know, but you could implement your custom component to do that. However, my advice to you is having a separate toolbar that's not on the action bar, since the users expect all buttons on the Action Bar to be "Action Items" which perform something immediate, so having toggle items on the Action Bar might break this expectation. This allows you to save space on the action bar for things that the user expects to see there like: navigation, "Undo", "Save", "Delete"...
I agree with Bruno about the fact its probably best if you do it in a separate toolbar for the sake of user experience standard and your code will probably look better because you cant fully customize the action bar but if you do decide to go with it i can think of something really simple like, when clicking one of the menu buttons you set the pressed button to a new drawable (pressed button) and the others to their normal drawable (not pressed) so each menu icon will have a pressed and not pressed icon. you will have to invalidateOptionsMenu though.
I had the same problem and did not find a solution. However, I found a workaround that works for me:
In the onOptionsItemSelected(MenuItem item) method, if item is the button you want to toggle, simply call item.setIcon() and point to another drawable. I use the same image but with another color to show the user that the button is in "pressed" state. And when the button is pressed again, revert to the original drawable.