Android Spinner Search Hint Prompt Text And Background Color

Android Spinner Search Hint Prompt Text And Background Color Example is for you.

You will learn to set prompt programmatically and to change prompt text and background color.

In android app, developer uses spinner to ask user to select any one choice from drop down menu or pop up dialog.

Here, you may want to give some hint or information to the user about your spinner options.

For example, if your spinner contains fruits as an options. Here, you can use some text like “Select Fruit” as a search hint.

Setting a search hint or prompt text in android spinner is not a tough task.

But customizing them like changing text color, changing background color need little tricks.

We will use two methods to implement search hint.

First by creating a drop down menu and second by using prompt dialog.

Implementing Drop Down

Step 1. Add Code in Default Activity

First of all, make a new project in the android studio.

Choose “Empty Activity” after you have given project name and SDK version.

Add the below source code in activity_main.xml

  • I have taken one spinner in main layout file.
  • Set the spinner mode as a dropdown or just don’t write this line.
  • By default, system will create drop down spinner only.

Write down the following coding lines in MainActivity.java file

Consider the below code snippet

  • First line is creating a boolean variable. By default it has true value.
  • Then, I have make one string array which contains the names of the fruits.
  • In this string array, set your search hint at the first position.

Look at the following code

  • Now above code will first set the adapter to the spinner.
  • I have also implemented onItemSelectedListener for this spinner. Here, we will use that boolean variable (first).
  • When the app is loaded for the first time on device, this compiler call this onItemSelectedListener. So it shows toast everytime when app is opened for fist time.
  • This case is not ideal for user experience or user satisfaction.

So that, boolean variable (first) will help us to overcome this problem.

Making Prompt Search Hint

Step 1. Write XML code

Add the following code in activity_main.xml

So the final code for activity_main.xml is something like

  • Here, I have taken three spinners to give them prompt text.
  • All these spinners must have the following line

Now you can add prompt text using two methods.

  1. Directly add your text in strings.xml and use that string in activity_main.xml
  2. Add ans set the prompt text programmatically in spinner.

Add text using strings.xml

Write the below line in res->values->strings.xml file

Following snippet from activity_main.xml file using this text reference

Now add the below code in MainActivity.java

Write Above code before onCreate() method and write below code inside the onCreate() method

Add Prompt text programmatically

Add the spinner in the activity_main.xml

Now add the following in MAinActivity.java

Write above code above the onCreate() method and below inside onCreate()

Changing Prompt Text And Background Color

Below code from activity_main.xml will get prompt text programmatically.

Now we will learn how to change the color of prompt text and background.

  • For this, we need to create a custom adapter and some layout files.
  • We will create separate layout files for prompt text and drop down items (spinner options).
  • So create a new xml layout file under res->layout directory

Give it a name like “vehicle_spinner.xml”

Code for “vehicle_spinner.xml” is as the following

  • This file will create the layout interface for main spinner’s main or first look.
  • Create another layout file under same directory named “vehicle_prompt.xml”

Write the below lines in it

  • This file will generate the view structure for the prompt text.
  • You can change the prompt text size, color, padding, margin etc. in this file.
  • Make another layout file with the name vehicle_dropdown.xml

Source code for vehicle_dropdown.xml is as the following

  • This will create a view for spinner options.
  • Prepare a new java class named “CustomApinnerAdapter.java”

Write the below source code in “CustomApinnerAdapter.java”

  • In the above code, getView() method creates a layout for default spinner look.
  • In the getDropDownView() method, there are two scenarios.
  • I have used two files to generate two different views.
  • When the position is 0, means that for the very first time, we will use it as a prompt text creation.
  • At this time, we will generate view using vehicle_prompt.xml file.
  • In all other cases, (when position is not 0) we will generate drop down items which are spinner options, from which user will choose his desired option.

Following lines of coding are making a spinner with custom adapter

So the final code for MainActivity.java is as below

Spinner Articles

Last Look

Download Source Code For android spinner search hint prompt text

[sociallocker]Dwonload Source Code For Spinner_Search_Hint[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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