How to add hint text to spinner in android

Create and display spinner with “Please select one” hint text.

Hint text tells app user that you have to select something here, but adding hint inside spinner is required more functionality because we have to display hint into spinner but also remember that user cannot be able to select hint text as its selection. So here is the complete step by step tutorial for How to add hint text to spinner in android.

android-project-download-code-button

How to add hint text to spinner in android.

Code for MainActivity.java file.

 package com.spinnerwithhint_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

 Spinner spinner;
 String[] spinnerValue = {
 "PHP",
 "ANDROID",
 "WEB-DESIGN",
 "PHOTOSHOP"
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinner =(Spinner)findViewById(R.id.spinner1);
 
 
 spinnerAdapter adapter = new spinnerAdapter(MainActivity.this, android.R.layout.simple_list_item_1);
 adapter.addAll(spinnerValue);
 adapter.add("This is Hint");
 spinner.setAdapter(adapter);
 spinner.setSelection(adapter.getCount());
 
 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

 @Override
 public void onItemSelected(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 if(spinner.getSelectedItem() == "This is Hint Text")
 {
 
 //Do nothing.
 }
 else{
 
 Toast.makeText(MainActivity.this, spinner.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
 
 }
 }

 @Override
 public void onNothingSelected(AdapterView<?> parent) {
 // TODO Auto-generated method stub
 
 }
 });
 
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.spinnerwithhint_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

</RelativeLayout>

 

Code for spinnerAdapter.java file.

 package com.spinnerwithhint_android_examples.com;

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

public class spinnerAdapter extends ArrayAdapter<String> {

 public spinnerAdapter(Context context, int textViewResourceId) {
 super(context, textViewResourceId);
 // TODO Auto-generated constructor stub
 
 }

 @Override
 public int getCount() {
 
 // TODO Auto-generated method stub
 int count = super.getCount();
 
 return count>0 ? count-1 : count ;
 
 
 }
 

}

Screenshots:

How to add hint text to spinner in android

Click here to download How to add hint text to spinner in android project.

2 Comments

  1. hi , thanks for your code , you can add items to Arraylist (show first item like hint) and then remove first item in setOnItemSelectedListener . too
    good luck my friend

Leave a Reply

Your email address will not be published. Required fields are marked *