Create Material Design Style Spinner DropDown in android example tutorial

How to make Material spinner in Pre lollipop devices like KitKat, Jelly beans, Ice cream sandwich.

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.

android-project-download-code-button

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.

build-gradle

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

compile 'com.android.support:appcompat-v7:24.0.0'

compile 'com.weiwangcn.betterspinner:library-material:1.1.0'

compile 'com.android.support:design:24.0.0'

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

spinner_library

Next step is start coding.

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

Code for MainActivity.java file.

package com.android_examples.materialstylespinner_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner;

public class MainActivity extends AppCompatActivity {

    MaterialBetterSpinner materialBetterSpinner ;

    String[] SPINNER_DATA = {"ANDROID","PHP","BLOGGER","WORDPRESS"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        materialBetterSpinner = (MaterialBetterSpinner)findViewById(R.id.material_spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, SPINNER_DATA);

        materialBetterSpinner.setAdapter(adapter);

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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"
    app:context="com.android_examples.materialstylespinner_android_examplescom.MainActivity">

    <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
        android:id="@+id/material_spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Select Here"
        app:met_floatingLabel="normal"
        android:textColorHint="#CFD8DC"
        android:gravity="center"
        />

</RelativeLayout>

Screenshot:

Create Material Design Style Spinner DropDown in android example tutorial

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

8 Comments

  1. Hi nice tutorial, is it possible to change the background color the arrow color and the text color ?
    Thanks

  2. This Library is not good .It just work in onCreate(…) .

  3. why the OnItemClickListener doesnt work?….are there any events that i can apply? over the spinner?

  4. Hello
    I work with AIDE.
    Apps I make with
    “extends AppCompatActivity” ،
    *CloseApp* error.
    But with ” extends activity ” no.

    Can you tell me why
    Thank

    Please send me the post to my e-mail

  5. hii what to do if i want to apply onclick event on this spinner ??

  6. But what happened to SetOnItemSelectedListener?

Leave a Reply

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