A simple and easy to use, highly customizable Spinner for Android Developers, written in Java.
<com.example.customspinner.BaseSpinner
android:id="@+id/spinner_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
app:headerColor="#000000"
app:baseColor="@color/black"
app:headerText="Select your site"
app:placeholder="Select"
app:enableHeader="true">
</com.example.customspinner.BaseSpinner>
ArrayList<GeneralModel> listCountry;
BaseSpinner spinnerCountry;
listCountry = new ArrayList<GeneralModel>();
listCountry.add(new GeneralModel("1231132","United States of America"));
listCountry.add(new GeneralModel("4451132","United Kingdom"));
spinnerCountry = findViewById(R.id.spinner_country);
spinnerCountry.setPlaceholder("Select");
spinnerCountry.setData(listCountry);
spinnerCountry.setSpinnerBackground(R.drawable.spinner_bg_transparent);
spinnerCountry.setOnValueChangedListener(new BaseSpinner.OnValueChangedListener() {
@Override
public void onValueChanged() {
if(spinnerCountry.getSelectedData().size() > 0) {
if (spinnerCountry.getSelectedData().get(0).getName().equalsIgnoreCase("United States of America")) {
//Do something
} else if (spinnerCountry.getSelectedData().get(0).getName().equalsIgnoreCase("United Kingdom")) {
//Do something
}
}
}
});
listCountry is the meta data to show on spinnerCountry spinnerCountry.setSpinnerBackground(R.drawable.spinner_bg_transparent); We have 3 kinds of background options
btnSubmit.setOnClickListener(view -> {
if (spinnerCountry.getSelectedData().size() <= 0) {
showSingleToast("Please select a country");
} else {
exit();
}
});
btnSubmit.selectPlaceholder();
This will also invoke onValueChanged method