With Awesome Spinner you can implement a material spinner with access to define direction layout and hint text.
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency:
implementation 'com.github.sadra:AwesomeSpinner:1.5.1'
Step 1. From very firts, you should add the Awsome Spinner component inside of your layout:
<com.isapanah.awesomespinner.AwesomeSpinner
android:id="@+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
- If you want to add a default value for hint just add the
spinnerHint
into your component - The default spinner has
RTL
direction, if you want to declare the direction of spinner just add thespinnerDirection
to RTL or LTR so the complete example with LTR direction and hint text to Select a category is:
<com.isapanah.awesomespinner.AwesomeSpinner
android:id="@+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:spinnerDirection="ltr"
app:spinnerHint="Select a category" />
Step 2. Define the component class into your activity:
AwesomeSpinner my_spinner = (AwesomeSpinner) findViewById(R.id.my_spinner);
Then Create your adapter. For now, there is two way to define apadter:
- With String List, define a
List<String>
in your activity and the attach that with your adapter:
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Ariplane");
ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
my_spinner.setAdapter(categoriesAdapter);
- With String-Array, define a
string-array
in yourstrings.xml
file and attach that with your adapter:
ArrayAdapter<CharSequence> provincesAdapter = ArrayAdapter.createFromResource(this, R.array.iran_provinces, android.R.layout.simple_spinner_item);
spinnerRTL.setAdapter(provincesAdapter, 0);
Step 3. For final step, you can create a Listener for listening on item selection:
my_spinner.setOnSpinnerItemClickListener(new AwesomeSpinner.onSpinnerItemClickListener<String>() {
@Override
public void onItemSelected(int position, String itemAtPosition) {
//TODO YOUR ACTIONS
}
});
there is some controls for your spinner:
Control | Requierement | Info |
---|---|---|
getSelectedItem() | - | Result = String : Returns the selected item |
getSelectedItemPosition() | - | Result = int : Returns the selected item position. If there were no selected item, it returns -1 . |
isSelected() | - | Result = boolean : Returns true if any item selected, and vice versa. |
setSelection(NUMBER ) |
int position |
You should pass the position of item to select the item programatically. |
setSelection(TEXT ) |
String value |
You should pass the item value to select the item programatically. |
setSpinnerHint(TEXT ) |
String value |
You should pass a sting value as the hint text for the spinner. |
setSpinnerEnable(BOOLEAN ); |
Boolean value |
If you want enable or disable the spinner, you should call this method. |
isSpinnerEnable() | - | Result = boolean : Returns true if the spinner is enabled, and vice versa. |
setHintTextColor() | int Color |
If you need to change the Spinner Hint Text color, just call the method and pass you color, ex: Color.BLUE . |
setSelectedItemHintColor() | int Color |
If you need to change the Selected Item Hint color, just call the method and pass you color, ex: Color.BLUE . |
setDownArrowTintColor() | int Color |
If you need to change the Down Arrow hint color, just call the method and pass you color, ex: Color.BLUE . |
setDropDownViewResource(RESOURCE ) |
int Resource |
If you need to change the Spinner Row stype, just call the method and pass you resource, ex: R.layout.my_sipnner_row_item . |
clearSelection() | - | It clears the spinner selection. |
You can see the EXAMPLE PROJECT for more infromation.
The MIT License (MIT): https://github.com/amlashi-sadra/AwesomeSpinner/blob/master/LICENSE
Copyright (c) 2017 Sadra Isapanah Amlashi
Website: isapanah.com
Twitter: @sadra_amlashi