This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Showing posts with label Drop down Ics Spinner and applying Style in android. Show all posts
Showing posts with label Drop down Ics Spinner and applying Style in android. Show all posts

Monday, 21 January 2013

Drop down Ics Spinner and applying Style in android

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" />

</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 :






Android Source Code : Download this Example with Library Here