Android spinner material design

Create Material Design Style Spinner DropDown in android example tutorial

Material design spinner is most likable by android user because of its glossy, smooth shaded design. But the pre lollipop devices dose not support Material spinner so in this tutorial we are going to create Material drop down spinner with the use of GitHub library. This type of spinner works with all the android versions. So here is the complete step by step tutorial for Create Material Design Style Spinner DropDown in android example tutorial.

Note: Read below steps very carefully to add Material Style Spinner GitHub library inside your current project.

1. Open your project’s build.gradle ( Module : app ) file.

2. Please add below code inside your build.gradle ( Module : app ) file.

3. Screenshot of build.gradle ( Module : app ) file after adding above code.

Next step is start coding.

How to Create Material Design Style Spinner DropDown in android example tutorial.

Code for MainActivity.java file.

Code for activity_main.xml layout file.

Screenshot:

Click here to download Create Material Design Style Spinner DropDown in android example tutorial project with source code.

Источник

Android spinner material design

Add the spinner to your layout XML:

Add items to the spinner and listen for clicks:

You can add attributes to customize the view. Available attributes:

name type info
ms_arrow_tint color sets the color on the drop-down arrow
ms_hide_arrow boolean set to true to hide the arrow drawable
ms_background_color color set the background color for the spinner and drop-down
ms_background_selector integer set the background resource for the dropdown items
ms_text_color color set the text color
ms_dropdown_max_height dimension set the max height of the drop-down
ms_dropdown_height dimension set the height of the drop-down
ms_padding_top dimension set the top padding of the drop-down
ms_padding_left dimension set the left padding of the drop-down
ms_padding_bottom dimension set the bottom padding of the drop-down
ms_padding_right dimension set the right padding of the drop-down
ms_popup_padding_top dimension set the top padding of the drop-down items
ms_popup_padding_left dimension set the left padding of the drop-down items
ms_popup_padding_bottom dimension set the bottom padding of the drop-down items
ms_popup_padding_right dimension set the right padding of the drop-down items

Download the latest AAR or grab via Gradle:

Источник

Android Material Design Spinner DropDown Example

Android spinner allows us to select an item from dropdown menu. Many android applications have used spinner component in their application. In this tutorial, you will learn to implement material design spinner component (dropdown menu) in your application.

You can display spinner where you want using XML or java. Here we work with both XML and java code. Implementing material design spinner in android application is not much difficult; you can just add it with little bit XML and java code.

Android Example: How to Make Material Design Android Spinner

To implement material design spinner first you have to add a dependency compile ‘com.weiwangcn.betterspinner:library-material:1.1.0’ in your project build.gradle file. Your build.gradle file somehow looks like below.

build.gradle

XML Layout File

In XML layout file you need to add material design spinner inside a layout like LinearLayout, RelativeLayout. Following is the complete content of XML layout file, you can paste it in yout project XML layout file.

res/layout/android_material_design_spinner.xml

Java Activity File

In java activity file, you need to add some strings for dropdown option list and link them to the spinner. Following is the complete code of java activity file; you can directly paste it in your project java activity file.

Источник

Android spinner material design

MaterialSpinner aims to provide a Material Design Spinner.

This widget is based on TextInputLayout.

For more information please check:

Add this in your root build.gradle file (not your module build.gradle file):

Then, add the library to your module build.gradle

If your using the Material Components for Android, make sure you have android.useAndroidX=true and android.enableJetifier=true in your gradle.properties file!

  • Quick way to select a value from a list.
  • Support for having no value selected or clearing it.
  • Styling and theming just like for TextInputLayout.
  • Showing a hint.
  • Showing an error.
  • Showing a prompt when spinnerMode is dialog .
  • Showing a bottom sheet when spinnerMode is bottomsheet .
  • Custom spinner drawables.
  • RTL support.

There is a sample provided which shows how to use the library, but for completeness, here is all that is required to get MaterialSpinner working:

If you want the options to be presented to the user as a dialog window just add:

If you want the options to be presented to the user as a bottom sheet window just add:

Kotlin (with synthetics):

Источник

Android Material Spinner Example

A spinner is an android widget that basically displays items in dropdown. Thus allowing ussers to select only one item at a time.

The parent class of the inbuilt android spinner is the AbsSpinner, a an abstract adapterview.However, sometimes we are not so much attracted to the inbuilt or default spinner, due to functionality limitations or we just want something nicer.

In that case we can look at GitHub and obtain a third party library. Material Spinner is such library. It was first created by Jared Rumler more than 2 years ago. Since then it’s been receiving updates.

This library provides us an awesome spinner view for android. Here is the demo that will be created shortly:

MaterialSpinner Definition

MaterialSpinner is not built from the Spinner widget provided by android. Instead it’s a custom Spinner built from TextView.

First it belongs to the following package:

It’s a public class deriving from TextView:

Creating Material Spinner

MaterialSpinner provides three public contructors for programmatic creation:

No. Contsructor
1. public MaterialSpinner(Context context) <>
2. public MaterialSpinner(Context context, AttributeSet attrs) <>
3. public MaterialSpinner(Context context, AttributeSet attrs, int defStyleAttr) <>

Internally, these contructors call a method called init() where they pass their parameters to that method. It is the init() method which then retrieves the default properties to be used from the resources. These properties include

  • TextColor
  • BackgroundColor
  • HintColor
  • PopupWindow Height dimensions etc

Furthermore a ListView gets instantiated inside this init() method among other things. It is this ListView that will be used to render the dropdown list of items.

Creation Via XML

You can also set the MaterialSpinner via XML layout:

then reference it from the Code:

Populating the MaterialSpinner

MaterialSpinner, even though it derives from TextView, is a spinner, a custom one.
Hence it renders collection of items in a dropdown list.

Normally with the default android spinner, you create an adapter explicitly to bind to your adapter.

However, MaterialSpinner is easier to use and saves you from this.

It internally maintains an adapter for you, a custom adapter called MaterialSpinnerAdapter.

That adapter is a child of BaseAdapter.

MaterialSpinner provides us with a public method called setItems() which we overload with different parameter types.

No. Method Description
1. public void setItems(@NonNull T… items) Internally it calls the other setItems() method converting our passed array to a list and passing it as a parameter
2. public void setItems(@NonNull List items) Passes this List to a MaterialSpinnerAdapter constructor. Then sets the adapter instance internally to the MaterialSpinner.
Usage Example

Here’s an example:

Note you haven’t set any adapter explicitly.

Via Adapter

You can also use an adapter.

This can be a custom adapter by passing in a ListAdapter reference:

This will instantiate a MaterialSpinnerAdapterWrapper class, passing in your ListAdapter object. Then moving the MaterialSpinnerAdapterWrapper instance into setAdapterInternal private method.

Or by passing a MaterialSpinnerAdapter instance into the setAdapter() method:

This will also pass that adapter into the setAdapterInternal() method.
The setAdapterInternal() is a private method residing in the MaterialSpinner class with the following responsibilities:

  1. Set the the MaterialSpinnerAdapter instance it receieves as the adapter for our ListView. Note that the MaterialSpinnerAdapter itself is a child of BaseAdapter, so ListView can accept it.
  2. Check if the passed adapter has items in it. Then set the hint text, hint color and text color.

Listening To Selection Change Events

We can listen to selection change events for our spinner in a very easy manner:

Installing MaterialSpinner

This library can be installed in three ways depending on your build system and preferences:

Grabbing via Gradle:

Material Spinner Example

Let’s now look at a material spinner example.

Questions this Example Answers
  1. Android Material Spinner Library and example.
  2. How to populate material spinner with an array of data.
  3. How to listen to selection events of material spinner.
Gradle Scripts
(a)Build.gradle

First we need to install Material Spinner by adding the following implementation statement in our app level build.gradle:

Resources

We will have two XML Layouts:

(a). activity_main.xml

Here’s our activity_main.xml. At the root we have a CoordinatorLayout.

Take note we are including the content_main.xml :

(b) content_main.xml

This is where we add our MaterialSpinner. At the root we have a RelativeLayout.

Java Code

(a). MainActivity.java

This is our launcher and only activity. We will populate our MaterialSpinner with an array of strings.

First we are referencing our material spinner from our layout using findViewById() method of the Activity class:

To fill our materialspinner with data we simply use the setItems() method:

Then we also listen to the item selection events our Spinner:

Here’s the full code:

Download

Also check our video tutorial it’s more detailed and explained in step by step.

No. Location Link
1. GitHub Direct Download
2. GitHub Browse

Credit to the Original Creator Jared Rumler

Example 2: Android MaterialSpinner with Floating Labels

Android MaterialSpinner Library and Example.

We will use a library created by @Ganfra called MaterialSpinner.

This is differnt from Jared Rumler’s MaterialSpinner.

MaterialSpinner(Ganfra’s) will provides us a Spinner with the Material style. You can use it like any regular Spinner.

It allows you to:

  1. Add floating label text
  2. Add hint and
  3. Add error messages.

Here’s the example demo we explore to learn how to use MaterialSpinner library.

Requirements

MaterialSpinner is used with android projects and requires at least API 14.

Installation

Android Studio uses the gradle as it’s build system.

Therefore we normally write gradle scripts to do several tasks related to our project. Such a task can be fetching a library as a dependency and adding it to our project.

So to add MaterialSpinner into your project, navigate over to the app-level(located in app folder of your project) and add the following under the dependencies closure:

[notice] If you use other libraries requiring appcompat-v7 like MaterialEditText make sure to exclude them if you have issue at compile time.
[/notice. Check below.]

How to use it

First you need to add it in your xml layout where you want it rendered:

You can see there are various options:

MaterialSpinner allows you to set a hint and a floating label text so that suppose no floating label text is provided, the hint gets displayed.

Then in your java code you use it like a regular spinner with an adapter like ArrayAdapter:

You can activate the error mode or deactivate:

Full MaterialSpinner Example

Let’s now look at a full material spinner example.

(a) hint_item.xml

We have a TextView as our root element.

(b) dropdown_hint_item.xml

Also a TextView .

(c) activity_main.xml

This layout will be inflated into the main activity.

At the root we have a ScrollView. Inside it are several MaterialSpinner widgets arranged linearly and vertically and a button at the bottom.

(b) MainActivity.java

This is the main activity and as class constants we have an error message as well as string array of items.

Then an ArrayAdapter and our spinner widgets as instance fields.

We will instantiate the ArrayAdapter and set invoke it’s setDropDownViewResource method, passing in our dropdown view resource as android.R.layout.simple_spinner_dropdown_item . setDropDownViewResource() is a method responsible for Setting the layout resource to create the drop down views.

We then create several methods, each responsible for referencing a given MaterialSpinner and setting it’s adapter.

Here’s the full code:

Download

Also check our video tutorial it’s more detailed and explained in step by step.

No. Location Link
1. GitHub Direct Download
2. GitHub Browse

Credit to the Original Creator Ganfra

How to Run

  1. Download the project.
  2. Go over to sample folder and edit import it to your android studio.Alternatively you can just copy paste the classes as well as the layouts and maybe other resources into your already created project.
  3. Then edit the app level build/gradle to add our dependency.

report this ad

Oclemy

Thanks for stopping by. My name is Oclemy(Clement Ochieng) and we have selected you as a recipient of a GIFT you may like ! Together with Skillshare we are offering you PROJECTS and 1000s of PREMIUM COURSES at Skillshare for FREE for 1 MONTH. To be eligible all you need is by sign up right now using my profile .

Источник

Читайте также:  Android auto разделение экрана
Оцените статью