ListPopupWindow
public class ListPopupWindow
Static library support version of the framework's android.widget.ListPopupWindow. Used to write apps that run on platforms prior to Android L. When running on Android L or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.
| See also | |
|---|---|
ListPopupWindow |
Summary
Constants |
|
|---|---|
static final int |
Mode for |
static final int |
Mode for |
static final int |
Mode for |
static final int |
MATCH_PARENT = -1Alias for |
static final int |
The provided prompt view should appear above list content. |
static final int |
The provided prompt view should appear below list content. |
static final int |
WRAP_CONTENT = -2Alias for |
Public constructors |
|---|
ListPopupWindow(@NonNull Context context)Create a new, empty popup window capable of displaying items from a ListAdapter. |
ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs)Create a new, empty popup window capable of displaying items from a ListAdapter. |
ListPopupWindow(Create a new, empty popup window capable of displaying items from a ListAdapter. |
ListPopupWindow(Create a new, empty popup window capable of displaying items from a ListAdapter. |
Public methods |
|
|---|---|
void |
Clear any current list selection. |
View.OnTouchListener |
Returns an |
void |
dismiss()Dismiss the popup window. |
@Nullable View |
Returns the view that will be used to anchor this popup. |
@StyleRes int |
Returns the animation style that will be used when the popup window is shown or dismissed. |
@Nullable Drawable |
|
@Nullable Rect |
Return custom anchor-relative bounds of the popup's transition epicenter |
int |
|
int |
|
int |
Return the current value in |
@Nullable ListView |
|
int |
|
@Nullable Object |
|
long |
|
int |
|
@Nullable View |
|
int |
Returns the current value in |
int |
|
int |
getWidth() |
boolean |
|
boolean |
isModal()Returns whether the popup window will be modal when shown. |
boolean |
|
boolean |
Filter key down events. |
boolean |
onKeyPreIme(int keyCode, @NonNull KeyEvent event)Filter pre-IME key events. |
boolean |
Filter key up events. |
boolean |
performItemClick(int position)Perform an item click operation on the specified list adapter position. |
void |
postShow()Post a |
void |
setAdapter(@Nullable ListAdapter adapter)Sets the adapter that provides the data and the views to represent the data in this popup window. |
void |
setAnchorView(@Nullable View anchor)Sets the popup's anchor view. |
void |
setAnimationStyle(@StyleRes int animationStyle)Set an animation style to use when the popup window is shown or dismissed. |
void |
Sets a drawable to be the background for the popup window. |
void |
setContentWidth(int width)Sets the width of the popup window by the size of its content. |
void |
setDropDownGravity(int gravity)Set the gravity of the dropdown list. |
void |
setEpicenterBounds(@Nullable Rect bounds)Specifies the custom anchor-relative bounds of the popup's transition epicenter. |
void |
setHeight(int height)Sets the height of the popup window in pixels. |
void |
setHorizontalOffset(int offset)Set the horizontal offset of this popup from its anchor view in pixels. |
void |
setInputMethodMode(int mode)Control how the popup operates with an input method: one of |
void |
setListSelector(Drawable selector)Sets a drawable to use as the list item selector. |
void |
setModal(boolean modal)Set whether this window should be modal when shown. |
void |
Set a listener to receive a callback when the popup is dismissed. |
void |
setOnItemClickListener(Sets a listener to receive events when a list item is clicked. |
void |
setOnItemSelectedListener(Sets a listener to receive events when a list item is selected. |
void |
setPromptPosition(int position)Set where the optional prompt view should appear. |
void |
setPromptView(@Nullable View prompt)Set a view to act as a user prompt for this popup window. |
void |
setSelection(int position)Set the selected position of the list. |
void |
setSoftInputMode(int mode)Sets the operating mode for the soft input area. |
void |
setVerticalOffset(int offset)Set the vertical offset of this popup from its anchor view in pixels. |
void |
setWidth(int width)Sets the width of the popup window in pixels. |
void |
setWindowLayoutType(int layoutType)Set the layout type for this popup window. |
void |
show()Show the popup list. |
Constants
INPUT_METHOD_FROM_FOCUSABLE
public static final int INPUT_METHOD_FROM_FOCUSABLE = 0
Mode for setInputMethodMode: the requirements for the input method should be based on the focusability of the popup. That is if it is focusable than it needs to work with the input method, else it doesn't.
INPUT_METHOD_NEEDED
public static final int INPUT_METHOD_NEEDED = 1
Mode for setInputMethodMode: this popup always needs to work with an input method, regardless of whether it is focusable. This means that it will always be displayed so that the user can also operate the input method while it is shown.
INPUT_METHOD_NOT_NEEDED
public static final int INPUT_METHOD_NOT_NEEDED = 2
Mode for setInputMethodMode: this popup never needs to work with an input method, regardless of whether it is focusable. This means that it will always be displayed to use as much space on the screen as needed, regardless of whether this covers the input method.
MATCH_PARENT
public static final int MATCH_PARENT = -1
Alias for MATCH_PARENT. If used to specify a popup width, the popup will match the width of the anchor view. If used to specify a popup height, the popup will fill available space.
POSITION_PROMPT_ABOVE
public static final int POSITION_PROMPT_ABOVE = 0
The provided prompt view should appear above list content.
POSITION_PROMPT_BELOW
public static final int POSITION_PROMPT_BELOW = 1
The provided prompt view should appear below list content.
WRAP_CONTENT
public static final int WRAP_CONTENT = -2
Alias for WRAP_CONTENT. If used to specify a popup width, the popup will use the width of its content.
Public constructors
ListPopupWindow
public ListPopupWindow(@NonNull Context context)
Create a new, empty popup window capable of displaying items from a ListAdapter. Backgrounds should be set using setBackgroundDrawable.
ListPopupWindow
public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs)
Create a new, empty popup window capable of displaying items from a ListAdapter. Backgrounds should be set using setBackgroundDrawable.
| Parameters | |
|---|---|
@NonNull Context context |
Context used for contained views. |
@Nullable AttributeSet attrs |
Attributes from inflating parent views used to style the popup. |
ListPopupWindow
public ListPopupWindow(
@NonNull Context context,
@Nullable AttributeSet attrs,
@AttrRes int defStyleAttr
)
Create a new, empty popup window capable of displaying items from a ListAdapter. Backgrounds should be set using setBackgroundDrawable.
| Parameters | |
|---|---|
@NonNull Context context |
Context used for contained views. |
@Nullable AttributeSet attrs |
Attributes from inflating parent views used to style the popup. |
@AttrRes int defStyleAttr |
Default style attribute to use for popup content. |
ListPopupWindow
public ListPopupWindow(
@NonNull Context context,
@Nullable AttributeSet attrs,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes
)
Create a new, empty popup window capable of displaying items from a ListAdapter. Backgrounds should be set using setBackgroundDrawable.
| Parameters | |
|---|---|
@NonNull Context context |
Context used for contained views. |
@Nullable AttributeSet attrs |
Attributes from inflating parent views used to style the popup. |
@AttrRes int defStyleAttr |
Style attribute to read for default styling of popup content. |
@StyleRes int defStyleRes |
Style resource ID to use for default styling of popup content. |
Public methods
clearListSelection
public void clearListSelection()
Clear any current list selection. Only valid when isShowing == true.
createDragToOpenListener
public View.OnTouchListener createDragToOpenListener(View src)
Returns an OnTouchListener that can be added to the source view to implement drag-to-open behavior. Generally, the source view should be the same view that was passed to setAnchorView.
When the listener is set on a view, touching that view and dragging outside of its bounds will open the popup window. Lifting will select the currently touched list item.
Example usage:
ListPopupWindow myPopup = new ListPopupWindow(context); myPopup.setAnchor(myAnchor); OnTouchListener dragListener = myPopup.createDragToOpenListener(myAnchor); myAnchor.setOnTouchListener(dragListener);
| Parameters | |
|---|---|
View src |
the view on which the resulting listener will be set |
| Returns | |
|---|---|
View.OnTouchListener |
a touch listener that controls drag-to-open behavior |
getAnchorView
public @Nullable View getAnchorView()
Returns the view that will be used to anchor this popup.
getAnimationStyle
public @StyleRes int getAnimationStyle()
Returns the animation style that will be used when the popup window is shown or dismissed.
| Returns | |
|---|---|
@StyleRes int |
Animation style that will be used. |
getEpicenterBounds
public @Nullable Rect getEpicenterBounds()
Return custom anchor-relative bounds of the popup's transition epicenter
| See also | |
|---|---|
setEpicenterBounds |
getHeight
public int getHeight()
| Returns | |
|---|---|
int |
The height of the popup window in pixels. |
getHorizontalOffset
public int getHorizontalOffset()
| Returns | |
|---|---|
int |
The horizontal offset of the popup from its anchor in pixels. |
getInputMethodMode
public int getInputMethodMode()
Return the current value in setInputMethodMode.
| See also | |
|---|---|
setInputMethodMode |
getPromptPosition
public int getPromptPosition()
| Returns | |
|---|---|
int |
Where the optional prompt view should appear. |
getSelectedItemId
public long getSelectedItemId()
| Returns | |
|---|---|
long |
The ID of the currently selected item or |
| See also | |
|---|---|
getSelectedItemId |
getSelectedItemPosition
public int getSelectedItemPosition()
| Returns | |
|---|---|
int |
The position of the currently selected item or |
| See also | |
|---|---|
getSelectedItemPosition |
getSoftInputMode
public int getSoftInputMode()
Returns the current value in setSoftInputMode.
| See also | |
|---|---|
setSoftInputMode |
|
softInputMode |
getVerticalOffset
public int getVerticalOffset()
| Returns | |
|---|---|
int |
The vertical offset of the popup from its anchor in pixels. |
isInputMethodNotNeeded
public boolean isInputMethodNotNeeded()
| Returns | |
|---|---|
boolean |
|
isModal
public boolean isModal()
Returns whether the popup window will be modal when shown.
| Returns | |
|---|---|
boolean |
|
isShowing
public boolean isShowing()
| Returns | |
|---|---|
boolean |
|
onKeyDown
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event)
Filter key down events. By forwarding key down events to this function, views using non-modal ListPopupWindow can have it handle key selection of items.
| Parameters | |
|---|---|
int keyCode |
keyCode param passed to the host view's onKeyDown |
@NonNull KeyEvent event |
event param passed to the host view's onKeyDown |
| Returns | |
|---|---|
boolean |
true if the event was handled, false if it was ignored. |
onKeyPreIme
public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event)
Filter pre-IME key events. By forwarding onKeyPreIme events to this function, views using ListPopupWindow can have it dismiss the popup when the back key is pressed.
| Parameters | |
|---|---|
int keyCode |
keyCode param passed to the host view's onKeyPreIme |
@NonNull KeyEvent event |
event param passed to the host view's onKeyPreIme |
| Returns | |
|---|---|
boolean |
true if the event was handled, false if it was ignored. |
| See also | |
|---|---|
setModal |
onKeyUp
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event)
Filter key up events. By forwarding key up events to this function, views using non-modal ListPopupWindow can have it handle key selection of items.
| Parameters | |
|---|---|
int keyCode |
keyCode param passed to the host view's onKeyUp |
@NonNull KeyEvent event |
event param passed to the host view's onKeyUp |
| Returns | |
|---|---|
boolean |
true if the event was handled, false if it was ignored. |
performItemClick
public boolean performItemClick(int position)
Perform an item click operation on the specified list adapter position.
| Parameters | |
|---|---|
int position |
Adapter position for performing the click |
| Returns | |
|---|---|
boolean |
true if the click action could be performed, false if not. (e.g. if the popup was not showing, this method would return false.) |
setAdapter
public void setAdapter(@Nullable ListAdapter adapter)
Sets the adapter that provides the data and the views to represent the data in this popup window.
| Parameters | |
|---|---|
@Nullable ListAdapter adapter |
The adapter to use to create this window's content. |
setAnchorView
public void setAnchorView(@Nullable View anchor)
Sets the popup's anchor view. This popup will always be positioned relative to the anchor view when shown.
setAnimationStyle
public void setAnimationStyle(@StyleRes int animationStyle)
Set an animation style to use when the popup window is shown or dismissed.
| Parameters | |
|---|---|
@StyleRes int animationStyle |
Animation style to use. |
setBackgroundDrawable
public void setBackgroundDrawable(@Nullable Drawable d)
Sets a drawable to be the background for the popup window.
setContentWidth
public void setContentWidth(int width)
Sets the width of the popup window by the size of its content. The final width may be larger to accommodate styled window dressing.
| Parameters | |
|---|---|
int width |
Desired width of content in pixels. |
setDropDownGravity
public void setDropDownGravity(int gravity)
Set the gravity of the dropdown list. This is commonly used to set gravity to START or END for alignment with the anchor.
| Parameters | |
|---|---|
int gravity |
Gravity value to use |
setEpicenterBounds
public void setEpicenterBounds(@Nullable Rect bounds)
Specifies the custom anchor-relative bounds of the popup's transition epicenter.
| See also | |
|---|---|
getEpicenterBounds |
setHeight
public void setHeight(int height)
Sets the height of the popup window in pixels. Can also be MATCH_PARENT.
| Parameters | |
|---|---|
int height |
Height of the popup window must be a positive value, |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if height is set to negative value |
setHorizontalOffset
public void setHorizontalOffset(int offset)
Set the horizontal offset of this popup from its anchor view in pixels.
| Parameters | |
|---|---|
int offset |
The horizontal offset of the popup from its anchor. |
setInputMethodMode
public void setInputMethodMode(int mode)
Control how the popup operates with an input method: one of INPUT_METHOD_FROM_FOCUSABLE, INPUT_METHOD_NEEDED, or INPUT_METHOD_NOT_NEEDED.
If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to the show method.
| See also | |
|---|---|
getInputMethodMode |
|
show |
setListSelector
public void setListSelector(Drawable selector)
Sets a drawable to use as the list item selector.
| Parameters | |
|---|---|
Drawable selector |
List selector drawable to use in the popup. |
setModal
public void setModal(boolean modal)
Set whether this window should be modal when shown.
If a popup window is modal, it will receive all touch and key input. If the user touches outside the popup window's content area the popup window will be dismissed.
| Parameters | |
|---|---|
boolean modal |
|
setOnDismissListener
public void setOnDismissListener(@Nullable PopupWindow.OnDismissListener listener)
Set a listener to receive a callback when the popup is dismissed.
| Parameters | |
|---|---|
@Nullable PopupWindow.OnDismissListener listener |
Listener that will be notified when the popup is dismissed. |
setOnItemClickListener
public void setOnItemClickListener(
@Nullable AdapterView.OnItemClickListener clickListener
)
Sets a listener to receive events when a list item is clicked.
| Parameters | |
|---|---|
@Nullable AdapterView.OnItemClickListener clickListener |
Listener to register |
| See also | |
|---|---|
setOnItemClickListener |
setOnItemSelectedListener
public void setOnItemSelectedListener(
@Nullable AdapterView.OnItemSelectedListener selectedListener
)
Sets a listener to receive events when a list item is selected.
| Parameters | |
|---|---|
@Nullable AdapterView.OnItemSelectedListener selectedListener |
Listener to register. |
| See also | |
|---|---|
setOnItemSelectedListener |
setPromptPosition
public void setPromptPosition(int position)
Set where the optional prompt view should appear. The default is POSITION_PROMPT_ABOVE.
| Parameters | |
|---|---|
int position |
A position constant declaring where the prompt should be displayed. |
setPromptView
public void setPromptView(@Nullable View prompt)
Set a view to act as a user prompt for this popup window. Where the prompt view will appear is controlled by setPromptPosition.
setSelection
public void setSelection(int position)
Set the selected position of the list. Only valid when isShowing == true.
| Parameters | |
|---|---|
int position |
List position to set as selected. |
setSoftInputMode
public void setSoftInputMode(int mode)
Sets the operating mode for the soft input area.
| Parameters | |
|---|---|
int mode |
The desired mode, see |
| See also | |
|---|---|
softInputMode |
|
getSoftInputMode |
setVerticalOffset
public void setVerticalOffset(int offset)
Set the vertical offset of this popup from its anchor view in pixels.
| Parameters | |
|---|---|
int offset |
The vertical offset of the popup from its anchor. |
setWidth
public void setWidth(int width)
Sets the width of the popup window in pixels. Can also be MATCH_PARENT or WRAP_CONTENT.
| Parameters | |
|---|---|
int width |
Width of the popup window. |
setWindowLayoutType
public void setWindowLayoutType(int layoutType)
Set the layout type for this popup window.
See type for possible values.
| Parameters | |
|---|---|
int layoutType |
Layout type for this window. |
| See also | |
|---|---|
type |