Daniel Ryan
Daniel Ryan

Reputation: 7100

Looking for a option spinner/option wheel like control in android

Looking for a control that allows to select one text value at a time for android with 2 arrows on each side. Not sure what the control is called but here is an example image:

enter image description here

Upvotes: 0

Views: 883

Answers (3)

Daniel Ryan
Daniel Ryan

Reputation: 7100

Created my own, which works great for me. The Galley control helped a lot. Feel free to point out any issues though.

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import nz.co.nuffie.android.crichq.R;

import java.util.ArrayList;
import java.util.List;

public class ScrollWheelControl extends LinearLayout{
    private final ArrayList<String> textList = new ArrayList<String>();
    private ArrayAdapter optionAdapter = null;
    private AdapterView.OnItemSelectedListener itemSelectedListener = null;

    public ScrollWheelControl(Context context){
        super(context);
    }

    public ScrollWheelControl(Context context, AttributeSet attrs){
        super(context, attrs);
        initView(context);
    }

    public ScrollWheelControl(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        initView(context);
    }

    public void initView(Context context) {
        View.inflate(context, R.layout.scroll_wheel_control, this);

        optionAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_gallery_item, textList )
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position,convertView,parent);
                if(v instanceof TextView){//Just in case.
                    TextView txt = (TextView)v;
                    Gallery.LayoutParams glp = new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT);
                    txt.setGravity(Gravity.CENTER);
                    txt.setLayoutParams(glp);
                }
                return v;
            }
        };


        optionAdapter.notifyDataSetChanged();

        final Gallery g = (Gallery) findViewById(R.id.gOptionSelect);
        g.setAdapter(optionAdapter);

        final Button btnLeft = (Button) findViewById(R.id.btnLeft);
        final Button btnRight = (Button) findViewById(R.id.btnRight);
        g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> list, View view, int position, long id) {
                if(position >= textList.size()-1){
                    btnRight.setTextColor(Color.GRAY);
                }else
                    btnRight.setTextColor(Color.WHITE);

                if(position == 0){
                    btnLeft.setTextColor(Color.GRAY);
                }else
                    btnLeft.setTextColor(Color.WHITE);

                if(itemSelectedListener != null){
                    itemSelectedListener.onItemSelected(list,view,position,id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        btnLeft.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                g.onFling(null, null, 2000, 0);
            }
        });
        btnRight.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                g.onFling(null, null, -2000, 0);
            }
        });
    }

    public void setOnItemListener(AdapterView.OnItemSelectedListener setItemSelectedListener){
        itemSelectedListener = setItemSelectedListener;
    }

    public void addTextItems(List<String> list){
        textList.addAll(list);
        if(optionAdapter != null){
            optionAdapter.notifyDataSetChanged();
        }
    }
}

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp">

    <Button
        android:id="@+id/btnLeft"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="\u003C"
        android:background="@android:color/transparent"
        android:textColor="@android:color/white"
        android:shadowColor="@android:color/black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:textStyle="bold"
        android:minWidth="15dp"
            />

    <Gallery
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:id="@+id/gOptionSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:spacing="0dp">
    </Gallery>

    <Button
        android:id="@+id/btnRight"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="\u003E" 
        android:textColor="@android:color/white"
        android:shadowColor="@android:color/black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:textStyle="bold"
        android:minWidth="15dp"
            />

</LinearLayout>

Upvotes: 0

NullPointerException
NullPointerException

Reputation: 4008

Hey i think just take a look at this wheel control.. The custom control created by third party person. He also given other details in his blog.. which you can modify it if you want..

Upvotes: 1

user1418174
user1418174

Reputation:

If it is for date, I would probably advise you to use the DatePicker which looks like this. Else if it's for time then probably you may want something like this.

However if you are looking for something like a NumberPicker, it's seems to be only available API 11 and up. Therefore if you want, you may need to customize your own.

Upvotes: 2

Related Questions