In the previous post, you learn how to create simple spinner in android in two different ways,
- With adapter.
- Without adapter using android: entries attribute.
To change the color of spinner you'll need two different layouts,
- CheckedTextView: use for the selected item of the spinner.
- TextView:- use for the item of the dropdown list.
Follow these steps to change the color of the spinner.
- Create a Spinner element in your XML layout.
- Create a list for the spinner.
- Create a layout for the selected item of the spinner.
- Create a layout for the item of the dropdown list.
- Create the spinner adapter using the Spinner Adapter class and set the CheckedTextView layout as a spinner layout and TextView for the item of the dropdown list.
- Attach the adapter to Spinner and setOnItemSelectedListener to Spinner.
Create a Spinner element in your XML layout: write the following code in your XML file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Spinner | |
android:id="@+id/coloredSpinner" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="20dp" | |
android:entries="@array/Spinner_Items"/> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<string-array name="Spinner_items"> | |
<item>Spinner Item One</item> | |
<item>Spinner Item Two</item> | |
<item>Spinner Item Three</item> | |
<item>Spinner Item Four</item> | |
<item>Spinner Item Five</item> | |
</string-array> | |
Create a layout for the selected item of the spinner: create new layout file name color_spinner_layout.xml with CheckedTextView as a root element. Copy the following values in the color_spinner_layout.xml file:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:text="Androchunk" | |
android:textColor="@android:color/holo_red_light" | |
android:textSize="20sp"> | |
</CheckedTextView> | |
Create a layout for the item of the dropdown list: create new layout file name spinner_dropdown_layout.xml with TextView as a root element. Copy the following values in the spinner_dropdown_layout.xml file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:text="Androchunk" | |
android:textColor="@color/colorPrimary" | |
android:textSize="20sp"> | |
</TextView> |
Create the spinner adapter: Adapter is used as a bridge in the spinner. It attaches list data into the spinner and creates a dropdown list of that data.do this is when the view is created in the onCreate() method.
adapter from the resource(ArrayList ) take 3 parameters,
- context.
- list: here your list from the string.xml
- layout for the spinner selected item: your custom layout for spinner selected item.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayAdapter adapter = ArrayAdapter.createFromResource( | |
this, | |
R.array.Spinner_Items, | |
R.layout.color_spinner_layout | |
); | |
adapter.setDropDownViewResource(R.layout.spinner_dropdown_layout); | |
Attach the adapter to Spinner and setOnItemSelectedListener to Spinner:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spinner coloredSpinner = findViewById(R.id.coloredSpinner); | |
coloredSpinner.setAdapter(adapter); | |
coloredSpinner.setOnItemSelectedListener(this); | |
For handle the item selection you’ll need to attach onItemSelectedListener to the spinner and implement it’s two methods.
- onItemSelected: this method called when item selection is changed.
- onNothingSelected: this method call when no item is selected for ex: remove the selected item from the list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
Toast.makeText(this,adapterView.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
If you have any questions or suggestions please leave them in the comments.
Full Source Code : https://github.com/Androchunk/ColoredSpinner
Full Source Code : https://github.com/Androchunk/ColoredSpinner
Question of the Day
Create custom spinner like this,
Solution :
Official documentation:
AdapterView.OnItemSelectedListener:https://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener
0 Comments