Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found out that the easiest way was to extend a ImageButton and add a ListPopupWindow.</p> <pre><code>public class MenuDropDown extends ImageButton { private ListPopupWindow mListDropDownWindow; private int mDropDownAnchorId; private ListAdapter mAdapter; private DropDownOnClickListener mDropDownOnClickListener; private OnItemClickListener mOnItemClickListener; public MenuDropDown(Context context, AttributeSet attrs) { this(context, attrs,R.attr.menuDropDown); } public MenuDropDown(Context context) { this(context, null); } public MenuDropDown(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mListDropDownWindow = new ListPopupWindow(context); mListDropDownWindow .setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuDropDown, defStyle, 0); mDropDownAnchorId = a.getResourceId( R.styleable.MenuDropDown_dropDownAnchor, View.NO_ID); mListDropDownWindow .setOnItemClickListener(new DropDownItemClickListener()); mListDropDownWindow.setModal(true); a.recycle(); setFocusable(true); mDropDownOnClickListener = new DropDownOnClickListener(); super.setOnClickListener(mDropDownOnClickListener); } private class DropDownItemClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { dissmissDropDown(); if(mOnItemClickListener != null){ mOnItemClickListener.onItemClick(parent, v, position, id); } } } private class DropDownOnClickListener implements OnClickListener { @Override public void onClick(View v) { showDropDown(); } } private void dissmissDropDown() { mListDropDownWindow.dismiss(); } public &lt;T extends ListAdapter&gt; void setAdapter(T adapter) { mAdapter = adapter; mListDropDownWindow.setAdapter(mAdapter); } public boolean isPopupShowing() { return mListDropDownWindow.isShowing(); } public void setOnItemClickListener(AdapterView.OnItemClickListener listener){ mOnItemClickListener = listener; } private void showDropDown() { if (mListDropDownWindow.getAnchorView() == null) { if (mDropDownAnchorId != View.NO_ID) { mListDropDownWindow.setAnchorView(getRootView().findViewById( mDropDownAnchorId)); } else { mListDropDownWindow.setAnchorView(this); } } mListDropDownWindow.show(); if (VERSION.SDK_INT &gt;= 9) { mListDropDownWindow.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS); } } public ListAdapter getAdapter() { return mAdapter; } } </code></pre> <p>attrs.xml:</p> <pre><code>&lt;attr name="menuDropDown" format="reference" /&gt; &lt;declare-styleable name="MenuDropDown" perent="ImageButton"&gt; &lt;attr name="dropDownAnchor" format="reference" /&gt; &lt;/declare-styleable&gt; </code></pre> <p>Layout: </p> <pre><code> &lt;AutoCompleteTextView android:id="@+id/autoText" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight=".8" android:gravity="bottom" android:dropDownAnchor="@id/parent"/&gt; &lt;mycode.MenuDropDown android:id="@+id/customSpinner" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight=".2" android:drawSelectorOnTop="true" mycode:dropDownAnchor="@id/parent" android:gravity="bottom"/&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload