Source code for enamlnative.android.android_spinner
"""
Copyright (c) 2017-2022, CodeLV.
Distributed under the terms of the MIT License.
The full license is in the file LICENSE, distributed with this software.
Created on May 20, 2017
"""
from atom.api import Typed, set_default
from enamlnative.widgets.spinner import ProxySpinner
from .android_adapter import AdapterView, AndroidAdapterView, ArrayAdapter
from .android_content import Context
from .bridge import JavaMethod
class AbsSpinner(AdapterView):
__nativeclass__ = "android.widget.AbsSpinner"
pointToPosition = JavaMethod(int, int)
setAdapter = JavaMethod("android.widget.SpinnerAdapter")
class Spinner(AbsSpinner):
__nativeclass__ = "android.widget.Spinner"
__signature__ = [Context, int]
setDropDownHorizontalOffset = JavaMethod(int)
setDropDownVerticalOffset = JavaMethod(int)
setDropDownWidth = JavaMethod(int)
setEnabled = JavaMethod(bool)
setGravity = JavaMethod(int)
setPrompt = JavaMethod("java.lang.CharSequence")
[docs]class AndroidSpinner(AndroidAdapterView, ProxySpinner):
"""An Android implementation of an Enaml ProxySpinner."""
#: A reference to the widget created by the proxy.
widget = Typed(Spinner)
#: Reference to adapter
adapter = Typed(ArrayAdapter)
#: Wrap content by default
default_layout = set_default({"height": "wrap_content"}) # type: ignore
# -------------------------------------------------------------------------
# Initialization API
# -------------------------------------------------------------------------
[docs] def init_layout(self):
# Make sure the layout always exists
if not self.layout_params:
self.set_layout({})
super().init_layout()
[docs] def get_declared_items(self):
selection = None
for k, v in super().get_declared_items():
if k == "selection":
selection = (k, v)
else:
yield (k, v)
# Select last
if selection:
yield selection
# -------------------------------------------------------------------------
# OnSelectionListener API
# -------------------------------------------------------------------------
def on_item_selected(self, parent, view, position, id):
d = self.declaration
with self.widget.setSelection.suppressed():
d.selected = position
def on_nothing_selected(self, parent):
pass
# -------------------------------------------------------------------------
# ProxySpinner API
# -------------------------------------------------------------------------
def set_prompt(self, prompt):
self.widget.setPrompt(prompt)
def set_selected(self, selected):
self.widget.setSelection(selected)
[docs] def set_items(self, items):
"""Generate the view cache"""
self.adapter.clear()
self.adapter.addAll(items)
def set_item_gravity(self, gravity):
self.widget.setGravity(gravity)
def set_drop_down_horizontal_offset(self, offset):
self.widget.setDropDownHorizontalOffset(offset)
def set_drop_down_vertical_offset(self, offset):
self.widget.setDropDownVerticalOffset(offset)
def set_drop_down_width(self, width):
self.widget.setDropDownWidth(width)