android honeycomb - how to make options menu item look disabled? - android

I am trying to disable one of the options menu items using the foll code:
public boolean onPrepareOptionsMenu (Menu menu){
menu.findItem(R.id.menu_item_id).setEnabled(false);
return true;
}
However after the method is executed, disabled item looks just like the rest (enabled) of the menu.
Is it possible to make it look disabled (grayed out or something)? How?
Thanks in advance

Have you looked at Themes? Check out this post: How to change the Text color of Menu item in Android?
Once you set up a Theme, you may find this helpful for finding what values can be set: Themes XML

Related

Android: Hide 3 dots from Navigation view header at right

How to hide 3 dots from Navigation header which comes in the right of header? This could be repeated question. I found few similar questions and their answers but they were for older version of Android. I am using Android sdk 21.
Any idea how to hide that 3 dot button?
Just Remove Override Method like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_and_add, menu);
return true;
}
This Override Method is responsible to for creating three dote as you mention it's really OptionMenu. If you don't want it, don't override onCreateOptionsMenumethod.
Alternative
Don't Inflate the menu xml. Just block the line like this
//getMenuInflater().inflate(R.menu.search_and_add, menu);
other code can remain same. No problem at all..
Those "3 Dots" are the "Overflow" menu, and is created when you establish a menu using a file in the menu resources directory.
If you have buttons or functionality you are wanting to expose via you action bar, you will need to have the overflow buttons (or instead, you can choose to have your buttons exposed at the top level inside the Action bar.
If you really don't want a menu, get rid of the menu.xml file describing this menu, and then get rid of the onCreateOptionsMenu() from your Activity.
Here are the official docs, which describe how this works.
I think you are speaking about the options menu, to get rid of it remove the override of the method onCreateOptionsMenu
In your menu folder the xmlfile that is used by your activity, change the app:showAsAction="never" to app:showAsAction="always" or some other you can see the options that are availabe by pressing ctrl+space.
Or else to get rid of it completely just remove the whole code and it's corresponding usages.

What does onPrepareOptionsMenu do?

I want to make Option Menu for Android, I have visit this site. In their script, I found onPrepareOptionsMenu, I try to compile and run using Android 2.3.3 compiler with and without onPrepareOptionsMenu, both works, but I didn't see any difference.
public boolean onCreateOptionsMenu(Menu menu){
//code here
}
public boolean onOptionsItemSelected(MenuItem item){
//code here
}
public boolean onPrepareOptionsMenu(Menu menu){
//code here
}
What is actually onPrepareOptionsMenu method do? Is that method important? Could I just delete the method?
Addition
Oh, I also hear about Action Bar in Android 3.0, it says that Action Bar is the alternative way for make Option Menu, and it using onPrepareOptionsMenu. Is that right?
Thank you...
Take a look in the API:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
If you want to alter the menu before it's shown to the user, you can put code to do that into onPrepareOptionsMenu. I've used that dynamically to disable some menu options in some circumstances.
As an example of when one might want to disable a menu option, I had an app where there was a way of specifying a destination. One of my menu options was to calculate a route to the destination. However, if a destination wasn't specified, that option didn't apply, so I used onPrepareOptionsMenu to disable that menu option when it wasn't applicable.
From Android 3.0 and beyond, there's the ActionBar, which is a menu bar. The most important items go into the ActionBar itself, but then there's an overflow for when there's not enough room on the action bar. One can specify that menu items should always be in the overflow menu and never on the action bar itself. On some devices, the action bar overflow corresponds to the permanent menu button on the device, whereas on other devices which don't have a menu button the overflow menu is seen on the right hand side of the action bar as three vertical dots.
onCreateOptionsMenu is called once, when your activity is first created. If it returns false, no option menu is shown and onPrepareOptionsMenu is never called.
If onCreateOptionsMenu returns true, onPrepareOptionsMenu is also called before the activity is displayed, and also every time the options menu is invalidated. Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.
If your menu does not change, use onCreateOptionsMenu.
example
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
if(!URLUtil.isValidUrl(news.geturl())){
menu.findItem(R.id.share).setVisible(false);
}
}

How to add menuItem to actionbarsherlock dialog?

I am using Theme Theme.Sherlock.Dialog tho show a dialog. Now, how can I add menu items at the top right corner of that dialog?
I use this code but I can't.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Close")
.setIcon(R.drawable.button_close)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
That is not possible. The action bar of the dialog has none of the features of the normal action bar.
Words of the ActionBarSherlock developer in this question.
please check if you are setting holo theme correctly and make sure you have initialized the actionbar in oncreate of activity. Above code works fine if you have followed the steps correctly

How to add dropdown menu to ActionBar in Android project?

I had been developing Android applications, but I don't know much about 4+ versions of Android well. Therefore, please help me - I have made Android application with tabs for navigation:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
But I also need to add dropdown menu to ActionBar for other goals. Can I do it? Please, if it's possible, give me an example. Thank you in advance.
You can use something called an android spinner. It looks like this:
You can customize it in many ways to suit your apps design too.
Here's a great tutorial by Google on how to use these:
http://developer.android.com/guide/topics/ui/controls/spinner.html
If you want to add this to an action bar, you can do it via a spinner adapter as detailed here:
http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
If you want to add icons to do certain actions, then you can see this:
http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
If you want to do certain actions in the bar itself (like search in the google search app) then see this:
http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
If you want to add navigation tabs, then see this:
http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
Absolutely the best and and the simplest answer I found so far is here.
Basically, no need for custom layout in this case. Just set the actonViewClass:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/spinner"
yourapp:showAsAction="ifRoom"
yourapp:actionViewClass="android.widget.Spinner" />
</menu>
And then handle it in onCreateOptionsMenu, as usual:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
MenuItem item = menu.findItem(R.id.spinner);
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
s.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection
This is by far the simplest and cleanest solution. Credits to François POYER, the original author.

Android ActionBar setCheckable() not working

I'm trying to make one of the MenuItems on my Menu have a checkmark ability, but it doesn't seem to work. All other MenuItems are working, this one does too, except for the checkmark display. What am I doing wrong?
MenuItem actionPickMode = menu.add(0, 3, 0, "pickmode");
actionPickMode.setTitle("Pick Mode");
actionPickMode.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT
| MenuItem.SHOW_AS_ACTION_ALWAYS);
actionPickMode.setVisible(true);
actionPickMode.setCheckable(true);
Looks like you're trying to add a checkmark to a MenuItem that is actually on the Action Bar. According to this question it isn't possible: Android action bar checkable menu item does not work/show properly?
What you can do is implement it yourself--when the item is clicked, use setIcon to change the drawable, and maintain the state of the toggle yourself. This question describes how you can get the built-in checkmark Drawables: How to access checkmark drawable in Android OS?
You have to create a custom layout for your action containing a checkbox. See my answer here.

Categories

Resources