Custom Spinners

Introduction:
Spinners is one of the widgets in Android which allows the user to pick one item from a list of items. The items in the Spinner come from the Adapter and XML associated.

demo



Using this code

Step1:(1st Spinner)
1.use this image for dropdown icon 

2.In this spinner we have to use only xml files not adapter so its very esay to understand.
3,Their is one xml its used for spinner border(edittext_border.xml).
4.Using Framelayout for merging two or more widget in same place.one is border background                another one is dropdown icon.

Step2:(2st Spinner)
1.This spinner view is half of the widgets only so using linearlayout for dividing two different 
2.first half using textview and second half is spinner(same us 1st step)
3.use wight to divide areas between text and spinner.

 edittext_border.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

<stroke
  android:width="0.5dip"
  android:color="#A4A4A4" />

<solid android:color="#FFFFFF" />

</shape>


activity_main.xml :     
<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:background="#A9E2F3" >

<RelativeLayout
  android:id="@+id/firstspinner"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="30dp" >

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="15dp"
  android:background="@drawable/edittext_border"
  android:orientation="horizontal" >

<Spinner
  android:id="@+id/profile_info_status"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/edittext_border"
  android:entries="@array/name"
  android:padding="5dp"
  android:prompt="@string/select"
  android:spinnerMode="dropdown"
  android:textColor="#FFFFFF" />

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_gravity="center_vertical|right"
  android:background="@drawable/drop_down"
  android:contentDescription="@string/app_name" />
</FrameLayout>
</RelativeLayout>

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/firstspinner"
  android:layout_marginTop="30dp" >

<LinearLayout
  android:id="@+id/layout_comments"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:layout_marginBottom="10dp"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="15dp"
  android:background="@drawable/edittext_border"
  android:orientation="horizontal"
  android:weightSum="2" >

<TextView
  android:id="@+id/contactus_edittext_subject"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1.1"
  android:gravity="center|left"
  android:hint="Message"
  android:padding="5dp"
  android:paddingLeft="2dp"
  android:textAppearance="?android:attr/textAppearanceMediumInverse"
  android:textColor="#000000" />

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight=".9"
  android:gravity="center"
  android:orientation="horizontal" >

<Spinner
  android:id="@+id/spinner_commenttype"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@null"
  android:entries="@array/message_types"
  android:padding="5dp"
  android:prompt="@string/select"
  android:spinnerMode="dropdown"
  android:textColor="#000000" />

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_gravity="center_vertical|right"
  android:background="@drawable/drop_down"
  android:contentDescription="@string/app_name" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>

</RelativeLayout>

MainActivity.java
No neeed to declare spinners id in mainactivity.java because all functionality are declared in xml only 
package com.example.customspinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

}
}


                           
Previous
Next Post »

4 comments

Write comments
Unknown
AUTHOR
October 6, 2015 at 12:47 PM delete

very nice tutorial bro

Reply
avatar
Unknown
AUTHOR
May 8, 2017 at 11:01 AM delete

It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.Android Training institute in chennai with placement | Android Training in chennai

Reply
avatar
Unknown
AUTHOR
May 8, 2017 at 11:22 AM delete

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
android development course fees in chennai | android app development training in chennai|

Reply
avatar