Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Quick ( and inefficient) example if you want to create your own spinner. (Works with List View Adapters):</p> <p>Layout:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;FrameLayout android:id="@+id/container" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /&gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:orientation="vertical"&gt; &lt;Button android:id="@+id/btn_up" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="up"/&gt; &lt;Button android:id="@+id/btn_down" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="down"/&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>CustomSpinner Class:</p> <pre><code>public class CustomSpinner extends FrameLayout{ // ------------------------------ FIELDS ------------------------------ private FrameLayout mContainer; private SpinnerAdapter mAdapter; private int index = 0; // --------------------------- CONSTRUCTORS --------------------------- public CustomSpinner(Context context) { super(context); init(context); } public CustomSpinner(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context){ this.addView(LayoutInflater.from(context).inflate(R.layout.custom_spinner,this,false)); findViewById(R.id.btn_up).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { index--; refresh(); } }); findViewById(R.id.btn_down).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { index++; refresh(); } }); mContainer = (FrameLayout) findViewById(R.id.container); } // -------------------------- OTHER METHODS -------------------------- public void setAdapter(SpinnerAdapter adapter) { this.mAdapter = adapter; refresh(); } private void refresh() { //----needs recycling for better performance--- //---now, we'll just clear up-- mContainer.removeAllViews(); //---do we need to refresh ? ----- if(mAdapter == null || mAdapter.getCount() == 0){return;} //--clamp index-- index = Math.max(0,index); index = Math.min(mAdapter.getCount() - 1, index); //--get view and show it----- View currentView = mAdapter.getView(index,null,mContainer); mContainer.addView(currentView); } } </code></pre> <p>Use Case:</p> <p>Layout:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;com.example.CustomSpinner android:id="@+id/custom_spinner" android:layout_height="fill_parent" android:layout_width="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Activity:</p> <pre><code>public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}); ((CustomSpinner)findViewById(R.id.custom_spinner)).setAdapter(adapter); } } </code></pre>
 

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