Styling and manipulating data in android spinner is hard. Make it easy.
add your build.grade
:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.canner:android-easyspinner:<VERSION>'
}
<io.canner.easyspinner.HorizontalSpinner
android:id="@+id/spinner1"
style="@style/spinnerForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:entries="@array/spn_list"
app:itemLayout="@layout/test_item"
app:itemSelectedLayout="@layout/test_item_selected"
app:nothingSelectedLayout="@layout/test_nothing_select"
app:prompt="Select a car type... "
app:spinnerMode="dialog"
app:title="Horizontal spinner" />
<io.canner.easyspinner.VerticalSpinner
android:id="@+id/verticalSpinner"
style="@style/spinnerForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:entries="@array/spn_list"
app:itemLayout="@layout/test_item"
app:itemSelectedLayout="@layout/test_item_selected"
app:nothingSelectedLayout="@layout/test_nothing_select"
app:prompt="Select a car type... "
app:spinnerMode="dialog"
app:title="Vertical spinner" />
<io.canner.easyspinner.SimpleSpinner
android:id="@+id/simpleSpinner"
style="@style/spinnerForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:entries="@array/spn_list"
app:prompt="Select a car type... "
app:spinnerMode="dropdown" />
You can setup your spinner customized styles with following settings in layout attributes.
style attribute can setup your spinner style
passing your layout resourceId to setup your own itemLayout.
when the item selected it will be shown using app:itemSelectedLayout
when nothing is selected, it will display this layout app:nothingSelectedLayout
you could replace with your own placeholder here.
setup your spinner entries.
setup your spinner prompt message.
spinnerMode could be either dropdown
or dialog
.
setup title.
this.spinnerTest = (HorizontalSpinner) findViewById(R.id.spinner1);
spinnerTest.setOnItemSelectedListener((new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
Log.v("SpinnerTest1 position", String.valueOf(position));
Log.v("SpinnerTest1 id", String.valueOf(id));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
Log.v("SpinnerTest1", "nothing selected");
}
}));
clear entries in spinner.
add a data in entry.
spinnerTest.add("test data");
add collection to entries
ArrayList<String> testData = new ArrayList();
testData.add("test 1");
testData.add("test 2");
testData.add("test 3");
testData.add("test 4");
spinnerTest.addAll(testData);
reset data and set datum to app:entries
.
reset data and set collection to app:entries
.
set title.
set spinner prompt.
set spinner enabled or not.
set spinner selection position.
get spinner in the view.
get title in the view.
MIT