How to add a Hint in spinner in XML
I am trying to add a Hint in the spinner widget as there is no option of Hint as in EditText
, I want to show Gender as a Hint and when clicked it must show only Male and Female not the hint.
How it can be Done Only Using XML
XML code of spinner.
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:entries="@array/gender"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_gravity="center_horizontal" />
String Array of the spinner
<string-array name="gender">
<item>Male</item>
<item>Female</item>
</string-array>
Solution 1:
In the adapter you can set the first item as disabled. Below is the sample code
@Override
public boolean isEnabled(int position) {
if (position == 0) {
// Disable the first item from Spinner
// First item will be use for hint
return false;
} else {
return true;
}
}
And set the first item to grey color.
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
And if the user selects the first item then do nothing.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = (String) parent.getItemAtPosition(position);
// If user change the default selection
// First item is disable and it is used for hint
if (position > 0) {
// Notify the selected item text
Toast.makeText(getApplicationContext(), "Selected : " + selectedItemText, Toast.LENGTH_SHORT).show();
}
}
Refer the below link for detail.
How to add a hint to Spinner in Android
Solution 2:
There are two ways you can use spinner:
static way
android:spinnerMode="dialog"
and then set:
android:prompt="@string/hint_resource"
dynamic way
spinner.setPrompt("Gender");
Note: It will work like a Hint but not actually it is.
May it help!
Solution 3:
For Kotlin
What will you get:
- Gray color if the hint is selected
- Drop down list with gray color of the hint
- Black color if something else than the hint is selected
I have added 5. step what changes the color of the text in the spinner depending on the selected item, because I couldn't find it here. In this case it is needed to change the text color to gray when the first item is selected in order to it looks like a hint.
-
Define a spinner in your activity_layout.xml
<Spinner android:id="@+id/mySpinner" android:layout_width="match_parent" android:layout_height="wrap_content" />
-
Define the string array in string.xml where the first item will be a hint.
<string-array name="your_string_array"> <item>Hint...</item> <item>Item1</item> <item>Item2</item> <item>Item3</item> </string-array>
-
Set up the spinner in the onCreate method in your Activity.kt
Get string array from resources
val items= resources.getStringArray(R.array.your_string_array)
Create spinner adapter
val spinnerAdapter= object : ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items) { override fun isEnabled(position: Int): Boolean { // Disable the first item from Spinner // First item will be used for hint return position != 0 } override fun getDropDownView( position: Int, convertView: View?, parent: ViewGroup ): View { val view: TextView = super.getDropDownView(position, convertView, parent) as TextView //set the color of first item in the drop down list to gray if(position == 0) { view.setTextColor(Color.GRAY) } else { //here it is possible to define color for other items by //view.setTextColor(Color.RED) } return view } }
-
Set drop down view resource and attach the adapter to your spinner.
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) mySpinner.adapter = spinnerAdapter
-
Change the color of the text in the spinner depending on the selected item
mySpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{ override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected( parent: AdapterView<*>?, view: View?, position: Int, id: Long ) { val value = parent!!.getItemAtPosition(position).toString() if(value == items[0]){ (view as TextView).setTextColor(Color.GRAY) } } }