How to put hint in a spinner?

1

I have a Spinner, where the user should select their gender: So far everything works fine, however I need to leave a hint spelled the word sex in the spinner component, as in the image below:

CodethatIhavesofar:

<Spinnerandroid:id="@+id/spinner_sex"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/edittext_email"
    android:layout_marginTop="6dp"
    android:layout_marginLeft="23dp"
    android:layout_marginRight="23dp"
    android:entries="@array/spinner_genre_items"
    android:textColor="@color/h_gray"
    android:textColorHint="@color/h_gray"
    android:textSize="@dimen/h_three"
    android:fontFamily="sans-serif-medium">
 </Spinner>

Array with Spinner items:

<string-array name="spinner_genre_items">
    <item>Masculino</item>
    <item>Feminino</item>
</string-array>

Java:

private Spinner mSpinnerGender;
mSpinnerGender = (Spinner) findViewById(R.id.spinner_sex);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    R.array.spinner_genre_items,
    android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(
    android.R.layout.simple_spinner_dropdown_item);
mSpinnerGender.setAdapter(adapter);

How could I do this?

    
asked by anonymous 16.04.2016 / 22:44

3 answers

2

Complementing @CaiqueOliveira's answer: To achieve the desired effect you can do the following

  • Create an array of strings where the first element is hint
  • Use the following code for the adapter

    final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
            this,R.layout.spinner_item,plantsList){
    
        @Override
        public boolean isEnabled(int position){
    
            if(position == 0){
    
                // Disabilita a primeira posição (hint)
                return false;
    
            } else {
                return true;
            }
        }
    
        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
    
            View view = super.getDropDownView(position, convertView, parent);
            TextView tv = (TextView) view;
    
            if(position == 0){
    
                // Deixa o hint com a cor cinza ( efeito de desabilitado)
                tv.setTextColor(Color.GRAY);
    
            }else {
                tv.setTextColor(Color.BLACK);
            }
    
            return view;
        }
    });
    
    spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
    spinner.setAdapter(spinnerArrayAdapter);
    
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
            String selectedItemText = (String) parent.getItemAtPosition(position);
    
            if(position > 0){
    
                // Ação realizada quando um elemento diferente
               // do hint é selecionado
            }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    
  • Note: Retired and adapted from link link

        
    18.04.2016 / 14:23
    0

    The simplest way I found was this:

    Creates a TextView or LinearLayout and places it along with Spinner in a RelativeLayout. Initially the textview will have the text as if it were the "Select one ..." tip, after the first click this TextView becomes invisible, disabled and calls the Spinner that is right behind it.

    Step 1:

    In the activity.xml that finds the spinner, put:

     <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Spinner
            android:id="@+id/sp_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:spinnerMode="dropdown" />
        <LinearLayout
            android:id="@+id/ll_hint_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center">
            <TextView
    
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Select..."/>
    
        </LinearLayout>
    </RelativeLayout>
    

    Step 2:

    In your Activity.java:

      public class MainActivity extends AppCompatActivity {
    
        private LinearLayout ll_hint_spinner;
        private Spinner sp_main;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ll_hint_spinner = findViewById(R.id.ll_hint_spinner);
            sp_main = findViewById(R.id.sp_main);
            //Action after clicking LinearLayout / Spinner;
            ll_hint_spinner.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //By clicking "Select ..." the Spinner is requested;
                    sp_main.performClick();
                    //Make LinearLayout invisible
                    setLinearVisibility(false);
                    //Disable LinearLayout
                    ll_hint_spinner.setEnabled(false);
                    //After LinearLayout is off, Spinner will function normally;
                    sp_main.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            sp_main.setSelection(position);
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            setLinearVisibility(true);
                        }
                    });
                }
            });
        }
        //Method to make LinearLayout invisible or visible;
        public void setLinearVisibility(boolean visible) {
            if (visible) {
                ll_hint_spinner.setVisibility(View.VISIBLE);
            } else {
                ll_hint_spinner.setVisibility(View.INVISIBLE);
            }
        }
    }
    

    The image examples I used a custom Spinner, but the result of the last example will be the same.

    Note: I hope I have helped.

        
    17.12.2018 / 01:49
    -1

    Hello

    Just add in the first row of your spinner what you want and this line of items will be visible.

        
    30.01.2017 / 09:53