How to add a Hint in spinner in XML

2019-01-11 09:19发布

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>

12条回答
姐就是有狂的资本
2楼-- · 2019-01-11 09:40

The trick is this line

((TextView) view).setTextColor(ContextCompat.getColor(mContext, R.color.login_input_hint_color));

use it in the onItemSelected. Here is my code with more context

List<String> list = getLabels(); // First item will be the placeholder
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // First item will be gray
                if (position == 0) {
                    ((TextView) view).setTextColor(ContextCompat.getColor(mContext, R.color.login_input_hint_color));
                } else {
                    ((TextView) view).setTextColor(ContextCompat.getColor(mContext, R.color.primary_text));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-11 09:40

if your Spinner not support android:prompt, should use android.support.v7.widget.AppCompatSpinner instead.

<android.support.v7.widget.AppCompatSpinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="Gender" />
查看更多
女痞
4楼-- · 2019-01-11 09:46

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

查看更多
走好不送
5楼-- · 2019-01-11 09:46

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!

查看更多
Fickle 薄情
6楼-- · 2019-01-11 09:48

make your hint at final position in your string array like this City is the hint here

array_city = new String[]{"Irbed", "Amman", "City"};

and then in your array adapter

ArrayAdapter<String> adapter_city = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, array_city) {
            @Override
            public int getCount() {
                // to show hint "Select Gender" and dont able to select
                return 2;
            }
        };

so the adapter return just first two item and finally in onCreate() method or what ,,, make Spinner select the hint

yourSpinner.setSelection(2);
查看更多
孤傲高冷的网名
7楼-- · 2019-01-11 09:49

Step 1:

Your array looks like. last item your hint

Ex : private String[] yourArray = new String[] {"Staff", "Student","Your Hint"};

Step 2:

Create HintAdpater.java (Just copy & paste)

This class not return last item.. So your Hint not displays.

HintAdapter.java

package ajax.com.vvcoe.utils;

import android.content.Context;
import android.widget.ArrayAdapter;

import java.util.List;


public class HintAdapter  extends ArrayAdapter<String> {


public HintAdapter(Context context, int resource) {
    super(context, resource);
}

public HintAdapter(Context context, int resource, int textViewResourceId) {
    super(context, resource, textViewResourceId);
}

public HintAdapter(Context context, int resource, String[] objects) {
    super(context, resource, objects);
}

public HintAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
    super(context, resource, textViewResourceId, objects);
}

public HintAdapter(Context context, int resource, List<String> objects) {
    super(context, resource, objects);
}

public HintAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
    super(context, resource, textViewResourceId, objects);
}

@Override
public int getCount() {
    // don't display last item. It is used as hint.
    int count = super.getCount();
    return count > 0 ? count - 1 : count;
}
}

Step 3:

Set spinner adapter like this

    HintAdapter hintAdapter=new HintAdapter(this,android.R.layout.simple_list_item_1,yourArray);
    yourSpinner.setAdapter(hintAdapter);
    // show hint
    yourSpinner.setSelection(hintAdapter.getCount());

Credit goes to @Yakiv Mospan from this answer - https://stackoverflow.com/a/22774285/3879847

I modify some changes only..

查看更多
登录 后发表回答