How to add a Hint in spinner
Asked Answered
K

2

6

XML code of spinner:

<Spinner
    android:id="@+id/mySpinner"
    https://stackoverflow.com/questions       
    style="@style/Widget.AppCompat.DropDownItem.Spinner"
    android:layout_width="match_parent"
    android:layout_height="70dp" />`

.kotlin:

val myStrings = arrayOf("One", "Two" , "Three", "Four")
mySpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, myStrings)
mySpinner.onItemSelectedListener = object : 
AdapterView.OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {
        TODO("not implemented") 
        //To change body of created functions use File | Settings | File Templates.
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        TODO("not implemented") 
        //To change body of created functions use File | Settings | File Templates.
    }
}}

Equal to the "hint" option in Edittext, I need a default text in a Spinner.

Kassandra answered 27/3, 2018 at 8:59 Comment(2)
There is no option for hint in Spinner . You can make your first element as hint like "Select" or whatever you need . Other option that you need to customized the Spinner .Fingerprint
have you considered using android:prompt?Latin
W
12

There is not any default way to display hint in spinner. For this you need to add one item manually in the array like below.

val myStrings = arrayOf("Select","One", "Two" , "Three", "Four")

Now, Define custom Adapter for the Spinner and disable the first item like below.

@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;
    }
}

You can change the color like below

@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;
}

For more info please visit:-

Add hint in spinner

Wessling answered 27/3, 2018 at 9:7 Comment(0)
G
0

The answer by Vir is working but if you want to hide the hint instead of disabling it from dropdown, you can use below code and add it to the top of getDropDownView:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (position == 0) {
        return new View(context);
    }

// Other Code
}

And edit the styling of Hint in the spinner like setting hint color:

@Override
public View getView(int position, View view, ViewGroup viewGroup) {

    TextView tv = (TextView) view;
    if (position == 0) {
        tv.setText(context.getString(HintValue));
        tv.setTextColor(context.getColor(R.color.placeholder_color));
    } else {
        tv.setText(itemLists.get(i));
    }
}

BONUS: I would recommend adding Hint at the end of the array, this will be beneficial later on when you have to store the selected value or access it later from the array (I was storing indexes of selected values)

Gadid answered 16/5 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.