How to put hint on a spinner?

Asked

Viewed 3,871 times

3

I have a Spinner, where the user should select his gender: So far everything works well, but I need to leave a hint written the word sex in the spinner component, as well as in the image below:

Exemplo de Spinner com hint

Code I have so far:

<Spinner
    android: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 that?

  • Are you wearing a ArrayAdapter or a class inheriting BaseAdapter?

  • Actually I was not using any of the 2. Now I am using an Arrayadapter, I updated the code in the question.

  • 1

    Pass 3 values to your Adapter: Male, Female and Sex; Set the starting value is the position(spinner) as being 3(sex). on getCount say that your Adapter is size 2(male, female);

  • In this case I would not use the array-list to popular Spinner but Arrayadapter?

3 answers

4


Complementing @Caiqueoliveira’s reply: To achieve the desired effect you can do the following

  1. Create a string array where the first element is hint
  2. Use the following code for 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) {
    
        }
    });
    

Obs.: Withdrawn and adapted from http://android--code.blogspot.com.br/2015/08/android-spinner-hint.htmlhttp://android--code.blogspot.com.br/2015/08/android-spinner-hint.html

0

The simplest way I could find was this:

Create a Textview or Linearlayout and place it together with Spinner in a Relativelayout. Initially the textview will have the text as if it were the tip "Select a ...", after the first click this Textview becomes invisible, disabled and calls the Spinner that is just 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);
        }
    }
}

Exemplo1 Exemplo2 Exemplo3

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

Note: My example is in github queiro case: https://github.com/MurilloComino/android-spinner-hint

-1

Hello!

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.