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.
Related
I have an actionbar which contains three tabs, I'm looking into adding a spinner to each of the tabs for a simpler UI.
All examples I've seen so far only show how to add a spinner to an actionbar but not to the tabs. It looks like the examples i've seen so far replaces the tabs with a spinner. Thats not what i want. I want a spinner to be activated when a tab is pressed. Or can i replace the tabs with several spinners?
Is this possible? Can anyone point me in a direction.
Yes, you can add a spinner in the actionbar. Check the link below:
Adding Spinner Drop-down to Action bar
First of all, If you are looking for NavigationTab, which is part of actionBar , take a look here- how to add navigation-tabs in actionBar. You need to implement custom adapter for the tab to integrate spinner inside navigation tab.
Look this links this will help you:
actionbar sherlock tabs with spinner and .setOnItemSelectedListener(this)
http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
http://developer.android.com/reference/android/widget/SpinnerAdapter.html
http://www.androidhive.info/2013/11/android-working-with-action-bar/
http://www.vogella.com/articles/AndroidActionBar/article.html
recently I've switched from the regular action bar implementation to the recently released appcompat implementation. My app made heavy use of the action bar to provide functionality. Since switching, on older spots
APIs (less than 11) don't have any menu items. And newer APIs do, but they don't show the image like configured (if room|withText). Has anyone else experienced this or came up with any solutions?
I found out what was up, when using the appcompat library. You can create your menu just like normal.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
But, in your menu xml files, add a xmlns:app attribute to the menu tag, like so:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
then, in each of your menu items, where you usually specify the "showAs" style (ifRoom, withText, etc.), include this alternative line alongside the regular one:
app:showAsAction="ifRoom|withText"
android:showAsAction="ifRoom|withText"
After this, your menus will show correctly on both current and old APIs. I got this information from here.
If there is a physical "Menu" button on device, it will show the contextual menu. If not, the menu item will be added to the ActionBar.
As the title suggests, I have an Android 4.0 Tablet app, that uses the Actionbar and tab mode.
If I add any more than 4 or 5 tabs to the action bar, a dropdown list is created instead. I have read the documentation, which states, "Note: In some cases, the Android system will show your action bar tabs as a drop-down list in order to ensure the best fit in the action bar."
Was just wondering if it is possible to override the default behavior and get the actionbar to scroll the items? The design document http://developer.android.com/design/patterns/actionbar.html talks about scrollable tabs, but I can't seem to find any information on them other than in the design document.
I have been struggling to get this to work. However, after playing with ActionBarSherlock, the solution is infuriatingly easy. If you download the sample Styled source and look in the MainActivity class, you can see it create and add the tabs, and beneath that:
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
I had called these BEFORE adding the tabs, and my tabs had been turned into a drop-down list. Turns out, all you need to do is call them after adding the tabs instead to keep the scrolling tabs.
Read the "Adding an Action View" section from http://developer.android.com/guide/topics/ui/actionbar.html
An action view is a widget that appears in the action bar as a substitute for an action item's button.
You can use HorizontalScrollView as action view by specifying
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mytabwidget"
android:title=""
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:actionViewClass="android.widget.HorizontalScrollView" />
</menu>
Get reference to this HorizontalScrollView in public boolean onCreateOptionsMenu(Menu menu)
Define a RadioGroup with some RadioButton where each RadioButton when selected, will update its associated tabs contents. Now add this RadioGroup to HorizontalScrollView which you will get in public boolean onCreateOptionsMenu(Menu menu)
You can use selectTab(ActionBar.Tab tab) or some other equivalent method to update tabs contents.
You can change the look of RadioButton by setting android:button to transparent image and then setting some StateListDrawable with states checked, selected and normal..
See below screen I got using this method.
In above image, each RadioButton when checked, selects different tab.
Have you checked out the Honeycomb Gallery sample app in the SDK? It might have what you are looking for. Install it on an ICS non-tablet device to see the scrollable tabs. A tablet has too much screen space for it to scroll.
http://developer.android.com/resources/samples/HoneycombGallery/index.html
The UI works the way it is for a reason. It is extremely confusing to have a scrollable set of tabs. There is a good chance that it won't be clear at all that there are more tabs available on the right, and the user will never know they can scroll the tabs to get to more features.
I recommend sticking with how the platform is designed to work. It generally does things for a reason.
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
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.