Android spinner with search

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.

Last Look

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

Источник

The best Android spinner library for your android application with more customization

Available on Play Store

Dropdown Mode Dialog Mode Searchable Mode
  • Dropdown mode ( android:spinnerMode=»dropdown» ). By default it is dropdown mode
  • Dialog mode ( android:spinnerMode=»dialog» )
  • Searchable mode ( app:smsp_isSearchable=»true» )

  • smsp_isSearchable : Default is false. Set it to true to enable this feature.
  • smsp_enableSearchHeader : Default is true. Set it to false to hide search header.
  • smsp_searchHeaderText : Text display as header text
  • smsp_searchHeaderTextColor : For changing header text color
  • smsp_searchHeaderBackgroundColor : For changing header background color
  • smsp_searchHint : To set query text hint
  • smsp_searchHintColor : To set hint color to query search text
  • smsp_searchTextColor : To set color to query search text
  • smsp_searchBackgroundColor : To set background color to searchview
  • smsp_itemListBackgroundColor : To set background color to search item list
  • smsp_enableDismissSearch : To enable/disable dismiss button on search dialog
  • smsp_dismissSearchText : To set dismiss button text for search dialog
  • smsp_dismissSearchColor : To set dismiss button text color for search dialog
  • smsp_searchDropdownView : To custom search view item
  • smsp_itemSize : To set spinner item size
  • smsp_itemColor : To set spinner item color
  • smsp_hint : To set spinner hint text
  • smsp_hintSize : To set spinner hint text size
  • smsp_hintColor : To set spinner hint color
  • smsp_itemListHintColor : To set spinner hint text color (dropdown hint)
  • smsp_itemListHintBackgroundColor : To set spinner hint background (dropdown hint)
  • smsp_enableFloatingLabel : Default is true. Set it to false to disable floating label.
  • smsp_alwaysShowFloatingLabel : Default is false. Set it to true for always display.
  • smsp_floatingLabelText : Update it to what text you want. If the value is not set, it get from hint.
  • smsp_floatingLabelSize : For changing floating label size
  • smsp_floatingLabelColor : For changing floating label color
  • smsp_enableErrorLabel : Default it is true.
  • smsp_errorText : Your error message text.
  • smsp_errorTextSize : For changing error text size
  • smsp_errorTextColor : for changing error text color
  • smsp_multilineError : Default is false. Update it to show as single line or multiple line
  • smsp_errorTextAlignment : Align error text to left, center or right

Color customization to:

  • Item text color ( smsp_itemColor )
  • Hint color ( smsp_hintColor )
  • Floating Label color ( smsp_floatingLabelColor )
  • Error Text color ( smsp_errorTextColor )
  • List Item Hint color and background ( smsp_itemListHintColor & smsp_itemListHintBackgroundColor )
  • List Item color ( smsp_itemListColor )
  • Selected item color ( smsp_selectedItemListColor )
  • Arrow or selector color ( smsp_arrowColor )
  • Underline color ( smsp_underlineColor )
  • Dropdown list/Search ListView background ( smsp_itemListBackgroundColor )
  • Search dialog dismiss button color ( smsp_dismissSearchColor )

Empty dropdown clickable

  • In case if you want to design view with empty spinner and apply click event and here it is.
  • smsp_underlineSize : To set underline size (The bottom line of spinner)
  • smsp_underlineColor : To set underline color (The bottom line of spinner)
  • smsp_isReSelectable : Allow re-selectable for current selected item. By default it is false

RTL (Right To Left) feature

  • smsp_isRtl : To change position of view from right to left or left to right. I will update more on it.

Typeface (Custom font)

  • smsp_typeface : Apply custom font to whole spinner view. You can use this attr without specify font extension
  • smsp_typeface_xxx : Set typeface to specific part of spinner

Add the dependencies to your gradle file:

Add SmartMaterialSpinner to your layout:

In Kotlin class

Issue and new feature request

Each raised issue, should be provided with some information to be easy for me to replicate it like Device Model , Android version and log message or file

Where to report issue and new feature request

You can create issue on Github to record issue history then to make it fast alert to me, you can send me email or post to Facebook group for any discussion

If you want to contribute in this library, please fork this repository and create pull request.

Источник

We will make two examples of Android Spinner Searchable in this post.

Learn to create Android Spinner Searchable With Search Box Example.

In this article, we will create filterable drop down of spinner in android studio.

We can put a search box at the top of the spinner drop down to filter the options.

This feature gives us the same result as we get from the searchview.

Android’s in-built system do not give us chance to put searchview in the spinner in any way.

You may find any library on github regarding using searchview in spinner the you can use them easily.

In today’s article, I will use github library but it is not using searchview.

We just need to use the desired XML version of spinner in layout file.

Rest of the things are very easy and smooth.

Ending View

Step 1. Defining Dependencies

I am going to use this github library in this example.

So add the following line in your build.gradle(Module:app) file

  • Above line will download all the required classes from repository.
  • Then we can use this library’s sentences easily.

Step 2. Layout

Add the following coding lines in activity_main.xml file

As you can see that I have taken instead of classical tag.

This new tag will implement a spinner from the github library.

So this spinner will react according to the classes written in the library instead of traditional spinner.

Step 3. Adding Options

Generally, we define spinner options name in string array variable. We also define this variable in java class only.

But in current scenario, we will add spinner options in strings.xml file.

strings.xml file is created automatically in every android studio project.

You can find this file under res->values directory.

Add the below code lines in strings.xml file.

Traditionally, we create string array in java files but in this case we have define it as above.

We will use this variable “Animals”, in the JAVA file to populate the spinner.

Step 4. Final Change

Let us make a last change to make searchable spinner.

Write down the below coding lines in MainActivity.java file

Moving Deep in Above code

Below line is creating a string array variable.

  • I have written this line before the onCreate() method, so that we can use this variable (options) through out the whole class.

Now consider the below source code

First line is finding the spinner with use of it’s ID from the activity_main.xml file

Second line is creating an object of the ArrayAdapter class.

This object will get the strings from the string array variable (Animals) of the strings.xml file

simple_spinner_item from the above code is in-built file to represent the front view of the spinner.

Read the below coding lines

Above line will specify a file to be used to generate a view of drop down.

simple_spinner_dropdown_item is also a system generated file of android structure.

Let us understand the below lines

These lines will first set the adapter to our spinner.

Then compiler will set the onItemSelected Listener for the spinner.

Following line will generate string array from the strings.xml file

Now we can use the options variable anywhere in the class as a string array.

It contains all the spinner options in string format.

When the user selects the option from the spinner, compiler will call the below method.

When user have selects a specific options then compiler will call onItemSelected() method.

If user do not choose any option then compiler will call onNothingSelected() method.

onItemSelected() method will simply show a toast that will say that which value is selected by the user.

Thus, we have make android searchable spinner with very little coding lines.

That is the only purpose of DemoNuts to create quality apps with efficient and time saving code structure.

2. 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.

Last Look

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

One thought on “Android Spinner Searchable With Search Box Example”

Thanks, works just as I needed.
How can I change the default keyboard to numberDecimal|numberSigned as I need only numeric input

Источник

Читайте также:  Block strike для android
Оцените статью