Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this
Project Structure :
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="@+id/ics_spinner"
style="@style/spinner_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="@+id/ics_spinner"
style="@style/spinner_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
</RelativeLayout>
drop_down_view.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:src="@drawable/abs__ic_clear_normal" />
</RelativeLayout>
styles.xml
<resources><style name="spinner_style">
<item name="android:dropDownWidth">fill_parent</item>
<item name="android:showDividers">beginning|middle|end</item>
<item name="android:alignmentMode">alignBounds</item>
<item name="android:dividerHeight">2dp</item>
<item name="android:scrollbars">none</item>
<item name="android:scrollbarAlwaysDrawVerticalTrack">false</item>
<item name="android:scrollbarTrackVertical">@android:color/transparent</item>
<item name="android:dropDownSelector">@android:color/white</item>
<item name="android:requiresFadingEdge">none</item>
</style>
</resources>
MainActivity.java
package com.rajeshvijayakumar.icsspinner;import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsAdapterView;
import com.actionbarsherlock.internal.widget.IcsAdapterView.OnItemSelectedListener;
import com.actionbarsherlock.internal.widget.IcsSpinner;
public class MainActivity extends SherlockActivity implements OnItemSelectedListener {
private IcsSpinner mIcsSpinner;
private IcsAdapter mAdapter;
private String[] mNameList = { "Akash", "Akshay", "Mahesh", "Mrithula",
"Rajesh", "Sonika" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIcsSpinner = (IcsSpinner) findViewById(R.id.ics_spinner);
mAdapter = new IcsAdapter(MainActivity.this, R.layout.drop_down_view, mNameList);
mIcsSpinner.setAdapter(mAdapter);
mIcsSpinner.setOnItemSelectedListener(this);
}
public class IcsAdapter extends ArrayAdapter<String> {
String[] nameList = new String[mNameList.length];
public IcsAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
this.nameList = objects;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public void setNotifyOnChange(boolean notifyOnChange) {
super.setNotifyOnChange(notifyOnChange);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.drop_down_view, parent, false);
TextView spinnerText = (TextView) row.findViewById(R.id.name_view);
if (position < mNameList.length) {
String cateName = mNameList[position];
spinnerText.setText(cateName);
}
return row;
}
}
@Override
public void onItemSelected(IcsAdapterView<?> spinAdapView, View view,
int position, long id) {
Toast.makeText(MainActivity.this, mNameList[position], Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(IcsAdapterView<?> parent) {
}
}
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rajeshvijayakumar.icsspinner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output :
Source Code : Download this Example with Library Here
Thanks for giving amazing tutorials... working good for me.. thanks.
ReplyDeleteThank You Ramachandran
DeleteReally useful for me.. Thanks.
ReplyDeleteThank you Prabhakaran
DeleteI have a question. when I change android:theme="@style/Theme.Sherlock" > all my widget change to theme Sherlock but I don't need to use theme Sherlock.I want to Drop down Ics Spinner theme sherlock. just Drop down Ics Spinner. what would I do ?
ReplyDeletePlease help me !!
You can apply background for spinner and you apply your own style for drop down layout like layout bg, text color etc... So Even if you apply Theme.Sherlock, it won't affect your custom style.
Deletehow to set own style ? Could you show an example ?
DeleteI'm inexperienced.
Thank you for answer me.
Thank for a I'm done.it's work.
DeleteVery Good !!!!
ReplyDeleteThank you....
DeleteNice tutorial .. It work's fyn .. Thank You ..
ReplyDeleteVery Very useful article. Thanks for the help
ReplyDeleteVery Nice tutorial, thanks for saving and sharing :). thank you
ReplyDelete