- Android: Spinner customizations
- Custom Spinner
- Adjusting the Spinner widget
- Adapter
- Drawbacks
- Custom Spinner implementation
- Bootstrap 5 Dropdown component
- Basic examples
- Regular button
- Link button
- Colors
- Split button
- Sizing
- Directions
- Dropup
- Dropright
- Dropleft
- Menu items
- Active
- Disabled
- Menu alignment
- Responsive alignment
- Alignment options
- Menu content
- Headers
- Dividers
- Forms
- Remove animation
- Dropdown options
- Dark variant
- Dropdowns — API
- Usage
- Via data attributes
- Via JavaScript
- Options
- Dropdown and popover position based on offset #1411
- Comments
- paglias commented Jan 30, 2012
- ghost commented Jan 31, 2012
- stoqe commented Feb 1, 2012
- MichaelCastelbuono commented Feb 9, 2012
- suigintou commented Feb 9, 2012
- mccombs commented Feb 21, 2012
- arhea commented Feb 23, 2012
- skattabrain commented Feb 27, 2012
- fat commented Mar 6, 2012
- coderberry commented Mar 9, 2012
- mccombs commented Mar 9, 2012
- mgcrea commented Apr 2, 2012
- tkrotoff commented Apr 18, 2012
- Cybolic commented Feb 4, 2013
- engineersmnky commented Feb 6, 2013
- Cybolic commented Feb 6, 2013
- engineersmnky commented Feb 6, 2013
- Cybolic commented Feb 6, 2013
- engineersmnky commented Feb 7, 2013
- archonic commented Feb 28, 2013
- Cybolic commented Mar 1, 2013
- archonic commented Mar 1, 2013
- Cybolic commented Mar 1, 2013
- Irrelon commented May 3, 2013
Android: Spinner customizations
Implement Android dropdown just the way you want.
Custom Spinner
In this article, I’ll describe two ways to achieve custom spinner for country selection that I had to implement at InCharge app. We officially support now 4 countries with charging stations, each country has specific legal rules, terms & conditions, etc.
The screen mockups I received from our designer were exactly these:
So the design for selection was not looking as a default Android Spinner as it had a header included when going to dropdown mode. Is it possible to implement using Spinner Widget? Let’s see.
Adjusting the Spinner widget
OK, so let’s begin with implementing layout:
And add a Spinner widget:
In this way, we’ve just built a Spinner widget on our UI. Thanks to tools:listitem=»@layout/item_country» we can see in Android Studio Designer how the Spinner will look like and adjust paddings/margins properly 🙂
With property android:background we can add a blue outline when Spinner is in the selected state and with android:popupBackground the background of dropdown view — easy so far.
Adapter
The magic of adding header will be done in our CountryAdapter class:
And an enum for supported countries is realized like this:
There are several parts that need to be explained…
Firstly, let’s see that getView() method will build UI when Spinner is in idle state and getDropDownView() will build particular items when Spinner dropdown gets opened. This is a place where we need to expect position == 0 to build a header with «Select option».
Unfortunately, there is no Spinner public method to programmatically close the dropdown. So how could we close it, when the header item gets clicked? There are couples of hacks to overcome this problem and here is one of them:
so we pretend as if the BACK button got clicked. Yeah, hacky…
Also to prevent selections for header view and not trigger its Spinner behavior we override isEnabled method to return false in this case.
Drawbacks
So we were able to implement the expected design somehow. But is it an ultimate solution? We had to use a hack with closing dropdown and make code dirty. Also, what if the designer suddenly changes his mind and wants to apply some animation for example arrow rotation? Built-in popup has entered animation which might interrupt these animations.
Technically android:popupAnimationStyle is applicable to Spinner style but it’s from SDK 24, quite high, right?
Would there be another solution?
Custom Spinner implementation
If we look into the Android source code of Spinner, we will see that underneath a PopupWindow is used to render the dropdown. Implementing classes are private so we cannot use them. But how much work could that be to make it yourself? Let’s try!
Let’s replace Spinner widget with anything else that could include item_country.xml Now, in setOnClickListener < . >part (trying to override OnClickListener on Spinner would result in RuntimeException).
So here we handle dropdown view by ourselves. We have full control of its UI! I chose ListView, could be anything else! All rendered by layout_country_dropdown.xml .
Note that you can use the very same CountryAdapter but rename the getDropDownView() method to getView() , and get rid of existing getView() .
Thus… Not a lot of work was required to transition 8) That’s what we all love. This solution seems way cleaner, with no dirty hacks — just add popupWindow?.dismiss() when header clicked. How does it look like?
What about animation management? It’s easy. We could set custom animation for a PopupWindow or simply disable it with a parameter:
then in CountryAdapter on header item creating add something like:
And the result is:
Try writing rotation animation on dropdown close by yourself 😉
Источник
Bootstrap 5 Dropdown component
Responsive dropdown built with the latest Bootstrap 5. Dropdowns are responsible for toggleable (collapsible> display a list of links.
Toggle contextual overlays for displaying lists of links and more with the MDB dropdown plugin.
Note: Read the API tab to find all available options and advanced customization
Basic examples
Wrap the dropdown’s toggle (your button or link) and the dropdown menu within .dropdown ent that declares position: relative; . Dropdowns can be triggered from or elements to better fit your potential needs. The examples shown here use semantic
- elements where appropriate, but custom markup is supported.
Regular button
Any single .btn can be turned into a dropdown toggle with some markup changes. Here’s how you can put them to work with either elements:
Link button
Colors
You can do this with any button variant:
Split button
Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of .dropdown-toggle-split for proper spacing around the dropdown caret.
We use this extra class to reduce the horizontal padding on either side of the caret by 25% and remove the margin-left that’s added for regular button dropdowns. Those extra changes keep the caret centered in the split button and provide a more appropriately sized hit area next to the main button.
Sizing
Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.
Directions
Dropup
Trigger dropdown menus above elements by adding .dropup to the parent element.
Dropright
Trigger dropdown menus at the right of the elements by adding .dropend to the parent element.
Dropleft
Trigger dropdown menus at the left of the elements by adding .dropstart to the parent element.
Menu items
You can also create non-interactive dropdown items with .dropdown-item-text . Feel free to style further with custom CSS or text utilities.
Active
Add .active to items in the dropdown to style them as active. To convey the active state to assistive technologies, use the aria-current attribute — using the page value for the current page, or true for the current item in a set.
Disabled
Add .disabled to items in the dropdown to style them as disabled.
Menu alignment
By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add .dropdown-menu-end to a .dropdown-menu to right align the dropdown menu.
Responsive alignment
If you want to use responsive alignment, disable dynamic positioning by adding the data-mdb-display=»static» attribute and use the responsive variation classes.
To align right the dropdown menu with the given breakpoint or larger, add .dropdown-menu<-sm|-md|-lg|-xl|-xxl>-end .
To align left the dropdown menu with the given breakpoint or larger, add .dropdown-menu-end and .dropdown-menu<-sm|-md|-lg|-xl|-xxl>-start .
Note that you don’t need to add a data-mdb-display=»static» attribute to dropdown buttons in navbars, since Popper isn’t used in navbars.
Alignment options
Taking most of the options shown above, here’s a small kitchen sink demo of various dropdown alignment options in one place.
Menu content
Headers
Add a header to label sections of actions in any dropdown menu.
Dropdown header
Dividers
Separate groups of related menu items with a divider.
Place any freeform text within a dropdown menu with text and use spacing utilities. Note that you’ll likely need additional sizing styles to constrain the menu width.
Some example text that’s free-flowing within the dropdown menu.
And this is more example text.
Forms
Put a form within a dropdown menu, or make it into a dropdown menu, and use margin or padding utilities to give it the negative space you require.
Remove animation
To remove the fade animation on click, add data-mdb-dropdown-animation=»off» attribute to the dropdown button.
Dropdown options
Use data-mdb-offset or data-mdb-reference to change the location of the dropdown.
Dark variant
Add .dropdown-menu-dark onto an existing .dropdown-menu to match a dark navbar.
If you want to support our friends from Tailwind Elements you can also check out the Tailwind dropdown documentation.
Dropdowns — API
Usage
Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the .show class on the parent list item. The data-mdb-toggle=»dropdown» attribute is relied on for closing dropdown menus at an application level, so it’s a good idea to always use it.
Note:
On touch-enabled devices, opening a dropdown adds empty mouseover handlers to the immediate children of the element. This admittedly ugly hack is necessary to work around a quirk in iOS’ event delegation, which would otherwise prevent a tap anywhere outside of the dropdown from triggering the code that closes the dropdown. Once the dropdown is closed, these additional empty mouseover handlers are removed.
Via data attributes
Add data-mdb-toggle=»dropdown» to a link or button to toggle a dropdown.
Via JavaScript
Call the dropdowns via JavaScript:
data-mdb-toggle=»dropdown» still required:
Regardless of whether you call your dropdown via JavaScript or instead use the data-mdb-api, data-mdb-toggle=»dropdown» is always required to be present on the dropdown’s trigger element.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-mdb- , as in data-mdb-offset=»» .
Name | Type | Default | Description |
---|---|---|---|
autoClose | boolean | string | true | |
boundary | string | element | ‘clippingParents’ | Overflow constraint boundary of the dropdown menu (applies only to Popper’s preventOverflow modifier). By default it’s ‘clippingParents’ and can accept an HTMLElement reference (via JavaScript only). For more information refer to Popper’s detectOverflow docs. |
display | string | ‘dynamic’ | By default, we use Popper for dynamic positioning. Disable this with static . |
dropdownAnimation | string | ‘on’ | Turn on / off dropdown animation. |
offset | array | string | function | 0 | |
popperConfig | null | object | function | null | |
reference | string | element | object | ‘toggle’ | Reference element of the dropdown menu. Accepts the values of ‘toggle’ , ‘parent’ , an HTMLElement reference or an object providing getBoundingClientRect . For more information refer to Popper’s constructor docs and virtual element docs. |
Note when boundary is set to any value other than ‘scrollParent’ , the style position: static is applied to the .dropdown container.
Источник
Dropdown and popover position based on offset #1411
Comments
paglias commented Jan 30, 2012
This is not a priority but it would be useful to Open dropdowns and popovers based on current position: for example if the popover toggle is positioned at the top of the page open the popover with position: bottom so that popovers and dropdiwns will be always visible.
I thought about jquery .offset()
The text was updated successfully, but these errors were encountered:
ghost commented Jan 31, 2012
My nav bar is positioned at the bottom instead of the top, and the dropdowns are all off-screen.
Edit: found a bottom-up class in the css files that solves my problem. Looks like there’s no documentation for this yet.
stoqe commented Feb 1, 2012
This is a bit of an issue. You can see in this screenshot for any popover element close to the right-side of page, that the popover goes off-page, lost in some dark corners of computer screens, far, far away (from sight).
MichaelCastelbuono commented Feb 9, 2012
I have a similar issue with a dropdown near the end of the screen on the right. It opens and you can only see a little bit of it before it fades out of view. It would be nice if it could guess whether to open on the bottom-right, bottom-left, top-right, or top-left.
suigintou commented Feb 9, 2012
mccombs commented Feb 21, 2012
arhea commented Feb 23, 2012
skattabrain commented Feb 27, 2012
+1 — this is especially a problem on my Droids (dont have an iPhone). but with the narrow strip of content, it seems to almost always be appearing outside the viewport.
fat commented Mar 6, 2012
You just need to provide a custom position function. There’s one floating around in the github issues if you search a bit, or you could try the mailing list 🙂
coderberry commented Mar 9, 2012
This fixes it if you do an inline style adjustment:
mccombs commented Mar 9, 2012
A custom position function and inline styles are messy. Wan’t offset included in v1 of bootstrap? I don’t think it’s an unreasonable request.
mgcrea commented Apr 2, 2012
I do need offset to achieve corner positionning (cf. #2915).
Please add it or allow use to set (or return via a lambda) an object
tkrotoff commented Apr 18, 2012
The existing custom position function fat talked about is here : #345
I didn’t manage to make it work.
Cybolic commented Feb 4, 2013
The dropdown function doesn’t seem to support a position function, so what would be the preferred way to handle positioning for a dropdown?
engineersmnky commented Feb 6, 2013
If your menu is on the right then use dropdown-menu pull-right this specifies the inline style suggested by cavneb.
Cybolic commented Feb 6, 2013
What about menus that are triggered from in-page content, meaning that the user can have scrolled to a point where the menu is not visible when clicked?
engineersmnky commented Feb 6, 2013
Bootstrap menus close when they lose focus so if a user scrolls to a point where the menu is not visible and interacts with any other object the menu will close. I don’t know if this is what you were asking.
Cybolic commented Feb 6, 2013
No, I mean something like a button on a page, that when the page is scrolled down will be at the top of the screen and would therefore naturally have the menu display below it, but when the page is scrolled up, the button would be at the bottom of the screen and the menu would then open outside of the viewport since its position is static.
How would one deal with a situation like this?
engineersmnky commented Feb 7, 2013
You may have to build a custom javascript to handle this situation to toggle class .bottom-up which supposedly pushes the menu up instead of down. I have not tested this.
archonic commented Feb 28, 2013
The function @fat was talking about is this
However, I’d recommend not using rel , just select a class like .bs-popover and note that the size of the popover is set. Adjust it as you need and keep in mind this doesn’t solve offset issues you may have.
Cybolic commented Mar 1, 2013
@archonic, I found that function as well, but since dropdown doesn’t support a position function, it’s of no use for that widget. Haven’t tested it for popover, where I assume it works as desired.
archonic commented Mar 1, 2013
I see what you mean. Could you use html: true for a popover and insert your dropdown menu there?
Cybolic commented Mar 1, 2013
@archonic I probably could, but in the meantime I’ve worked around it in the design. I’ll return with more if I hit the problem again later.
Irrelon commented May 3, 2013
While this doesn’t work based on offset, if you KNOW where you want it to open, I modified the bootstrap CSS to allow a data-placement=»left» on the .dropdown-menu element. Then you just add this after loading bootstrap’s CSS:
Источник