I have to problems:
• When the user taps on the three dots (the overflow menu), I don't want the app to display a menu. Instead I want it to do an action instantly.
• The action should be a change to the AppTheme. So when the user taps on the overflow menu, the app theme changes to dark, if the user taps it again to light, etc.
Since it's not a good way or as #David Medenjak said, it might be a different design or bad design (something like out of the rules).
By the way, you can use this for handling that OverflowMenu:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (disableMenu())
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
And then use the following answer:
https://stackoverflow.com/a/18301723/4409113
Finally, use the following link for checking what the current theme is and at the end, set your theme.
Hope that helps.
Related
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.
I am trying to set up a search interface using the ActionBar in the context of a ChromeCast application (using code from CastCompanionLibrary and VideoBrowserActivity git projects). i need a way to hide the ChromeCast MediaRoute MenuItem (the ChromeCast button, for short). it is juxtaposed next to a search icon, and when the user clicks on the search icon, the ChromeCast button should disappear so as to allow the search view to expand (as much as possible the ActionBar).
first, the XML defining my ActionBar looks like the following.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
...
<item android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always"/>
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
android:actionViewClass="android.widget.SearchView"
app:showAsAction="always"/>
</menu>
then, in my activity (sub-class of ActionBarActivity), i create the menu as follows.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflator().inflate(R.menu.main, menu);
MenuItem miSearch = menu.findItem(R.id.action_search);
SearchView view = (SearchView)miSearch.getActionView();
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
view.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mediaRouteMenuItem = mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
return true;
}
i tried to hide the MediaRoute menu item as follows (this approach was taken from another SO post).
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_search:
mediaRouteMenuItem.setVisible(false);
invalidateOptionsMenu();
return true;
}
}
however, when the user clicks on the search icon, the MediaRoute menu item is still visible.
it would also be nice to know (if it's possible to hide the ChromeCast button) how to make the button visible again if the user cancels the search operation.
any help is appreciated.
I think that what you are seeing (or, now, saw) was dictated by the order of the action providers/action classes. An expanded collapsible action view will take over space to the end of the action bar, but not clobber things before it. Hence, putting SearchView first will let it take over the whole bar.
What I am saying here is just some thoughts that I have not tested, so take that into account when you read this. Since MediaRouterActionProvider is managed by the framework, I don't think you can manually hide it; I imagine system will override that upon its periodic scan. I see two options here:
Use MediaRouteButton instead of ActionProvider; that is also supported by CCL and the visibility of that is completely up to you (CCL provides some callbacks that can tell you when there is a device matching your filters or not and you can clearly do as you see fit with that data). This is my recommended approach
Use a hack: have two MediaRouteSelector; one that is associated with an appId that doesn't exist (or is not published and no device is associated with that) and a second one which is associated with your good appId. If you use the first selector, the button will disappear since there is no device qualified for that appId and second one wold behave normally.
Again, I think the first option is cleaner. Let us know if it works out for you or not.
In my application i want to add toggle button right side of the Application-name (ie)Right side of BluetoothTextMessaging
Please help me thanks...
May this help you:
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.addSubMenu(0, 1, 1, " Button ").setIcon(R.drawable.file_icon)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
return true;
}
And you can access that button using following code & write logic what to perform on click of that button inside the following code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1: {
// Your Logic
break;
}
return super.onOptionsItemSelected(item);
}
I believe that what you're searching for is called an options menu. On Android 3.0+ it is supported. This is how Google's own tutorial describes it:
If you've developed your application for Android 2.3.x (API level 10)
or lower, the contents of your options menu appear at the bottom of
the screen when the user presses the Menu button, as shown in figure
When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six
items, Android places the sixth item and the rest into the overflow
menu, which the user can open by selecting More.
If you've developed your application for Android 3.0 (API level 11)
and higher, items from the options menu are available in the action
bar. By default, the system places all items in the action overflow,
which the user can reveal with the action overflow icon on the right
side of the action bar (or by pressing the device Menu button, if
available). To enable quick access to important actions, you can
promote a few items to appear in the action bar by adding
android:showAsAction="ifRoom" to the corresponding elements
Follow this link: (Menu tutorial) to get to the tutorial.
I can provide code snippets, however the ones on the website are much better :-)
I have in my application a ListView activity called ResultListViewActivity and which display contents from a database. The content displayed in the listview depends on a button clicked in a previous activity. So if the user clicks the "Button 1" in the Main Activity, it will display a specific content in ResultListViewActivity, and so on.
I want to add a menu to this activity : the thing is, the items of this menu are also supposed to change according to the button clicked before. There are 9 cases, each cases has a different number of items. So far I have this code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Bundle bundle = getIntent().getExtras();
int filterVariable = bundle.getInt("filterVariable");
switch (filterVariable)
{
case 1:
getSupportMenuInflater().inflate(R.menu.filter_fastfood, menu);
break;
case 2:
getSupportMenuInflater().inflate(R.menu.filter_restaurants, menu);
break;
case 3:
getSupportMenuInflater().inflate(R.menu.filter_coffeeshops, menu);
break;
case 4:
getSupportMenuInflater().inflate(R.menu.filter_bars, menu);
break;
// [... and so on]
}
return super.onCreateOptionsMenu(menu);
}
It works well, for every case I indeed have my different items in the menu. But I don't want these items to be reachable through the "menu button" of my device (which is absolutely not instinctive and hard for the user to guess). These items are importants so I would like to have a button on the upper right corner, which would open this menu instead (drop down menu / pop up menu).
Do you know how such a thing can be done ? I have seen this in some google application but couldn't find tutorial showing which tools to use for doing this.
Thanks !
ps: I am using actionbarsherlock in my application.
If you are using the latest version 4.2.0 of ABS, then support for .ForceOverflow themes has been removed.
Source: Version 4.2.0 Changelog
Extract of the Change Log:
Add SearchView widget for standard search interaction (API 8+ only)
Fix: ShareActionProvider in the split action bar no longer fills the entire screen.
Fix: ShareActionProvider now does file I/O on a background thread.
Fix: Automatically correct ColorDrawable not respecting bounds when used as a stacked background.
Fix: Ensure fragments collection is present before dispatching events.
Fix: XML-defined onClick searches the correct context for the declared method.
Fix: Ensure action mode start/finish callbacks are invoked on the activity for the native action bar.
Fix: Allow tab callbacks to have a fragment transaction instance for any FragmentActivity.
Fix: Ensure CollapsibleActionView callbacks are dispatched in both native and compatbility action bars.
Fix: Remove .ForceOverflow themes. These never should have been included.
To workaround that, you will need to download an older version (version 4.1.0) of ABS that had it working. You can get a list of download as per their release history here: http://actionbarsherlock.com/download.html
This is from a production app, tested and functioning.
You will also need to make a minor change in your applications default theme. For example:
<style name="MyTheme" parent="#style/Theme.Sherlock.ForceOverflow">
<item name="android:windowBackground">#color/background</item>
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="MyTheme.ForceOverflow">
<item name="absForceOverflow">true</item>
</style>
Note the use of #style/Theme.Sherlock.ForceOverflow in the parent attribute for the primary theme. Also not the additional <style> with the name of MyTheme.ForceOverflow. Doing the above will get the Overflow menu working for you.
You will also need to add this attribute to your menu items:
android:showAsAction="ifRoom|withText"
Now, to force the device into thinking that it has a physical menu key, use this code:
NOTE: This is a hack and for the obvious lack of access to multiple devices, I have personally never tested if it works on every Android device that exists. Use at your own risk etc. FYI, I have used this before and it worked with no complaints till date.
try {
ViewConfiguration config = ViewConfiguration.get(MainPage.this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
Forcing the Overflow Menu to appear in the ActionBar as against not showing an indicator that more options exist is a personal preference. I am answering this from the stand point of a technically possible solution as against promoting the use of such a feature. Whether it is advisable from the UX point of view is another subject matter.
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