Sunday, 27 January 2013

Ics Time Picker Example in Android

Download the following Libraries from the url :  https://github.com/SimonVT . You will find list of libraries and download it which is circled in the below image.


Ensure you have added number picker library to the time picker libray.

 And you should add this time picker libary to your project

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/time_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Time" />

</RelativeLayout>

MainActivity.java


package com.rajeshvijayakumar.timepicker;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import net.simonvt.app.TimePickerDialog;
import net.simonvt.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends Activity implements OnClickListener {

    private Button mTimeButton;

    private Calendar mCalen;
    private int hourOfDay;
    private int minute;
    private int ampm;

    private static final int Time_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTimeButton = (Button) findViewById(R.id.time_button);
        mCalen = Calendar.getInstance();
        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
        minute = mCalen.get(Calendar.MINUTE);
        ampm = mCalen.get(Calendar.AM_PM);
        mTimeButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case Time_PICKER_ID:
                return new TimePickerDialog(this, TimePickerListener,
                        hourOfDay, minute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener TimePickerListener =
            new TimePickerDialog.OnTimeSetListener() {

                // while dialog box is closed, below method is called.
                public void onTimeSet(TimePicker view, int hour, int minute) {

                    mCalen.set(Calendar.HOUR_OF_DAY, hour);
                    mCalen.set(Calendar.MINUTE, minute);

                    int hour12format = mCalen.get(Calendar.HOUR);
                    hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                    minute = mCalen.get(Calendar.MINUTE);
                    ampm = mCalen.get(Calendar.AM_PM);
                    String ampmStr = (ampm == 0) ? "AM" : "PM";
                    // Set the Time String in Button
                    mTimeButton.setText(hour12format + " : " + minute + " / " + ampmStr);
                }
            };

    @Override
    public void onClick(View v) {
        showDialog(Time_PICKER_ID);
    }
}


styles.xml

    <style name="SampleTheme" parent="android:Theme">
        <item name="numberPickerUpButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerUpButton</item>
        <item name="numberPickerDownButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerDownButton</item>
        <item name="numberPickerInputTextStyle">@style/NPWidget.Holo.EditText.NumberPickerInputText</item>
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
    </style>



In Manifest.xml give android:theme="@style/SampleTheme"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijayakumar.timepicker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/SampleTheme" >
        <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 :














Saturday, 26 January 2013

Ics Date Picker Example in Android

Download the following Libraries from the url :  https://github.com/SimonVT . You will find list of libraries and download it which is circled in the below image.

Ensure you have added calendarview and number picker library to the date picker libray.
 And your project should add this date picker library as follows :

activity_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Date" />

</RelativeLayout>

IcsDatePickerActivity.java


package com.rajeshvijayakumar.datepicker;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import net.simonvt.app.DatePickerDialog;
import net.simonvt.widget.DatePicker;

import java.util.Calendar;


public class MainActivity extends Activity implements OnClickListener {

    private Button mDateButton;

    private Calendar mCalen;
    private int day;
    private int month;
    private int year;

    private static final int DATE_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateButton = (Button) findViewById(R.id.date_button);
        mCalen = Calendar.getInstance();
        day = mCalen.get(Calendar.DAY_OF_MONTH);
        month = mCalen.get(Calendar.MONTH);
        year = mCalen.get(Calendar.YEAR);
        mDateButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, datePickerListener,
                        year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener =
            new DatePickerDialog.OnDateSetListener() {

                // while dialog box is closed, below method is called.
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // Set the Date String in Button
                    mDateButton.setText(day + " / " + (month + 1) + " / " + year);
                }
            };

    @Override
    public void onClick(View v) {

        showDialog(DATE_PICKER_ID);

    }
}

styles.xml


    <style name="SampleTheme" parent="@android:style/Theme">
        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
        <item name="datePickerStyle">@style/Widget.Holo.DatePicker</item>
        <item name="numberPickerUpButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerUpButton</item>
        <item name="numberPickerDownButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerDownButton</item>
        <item name="numberPickerInputTextStyle">@style/NPWidget.Holo.EditText.NumberPickerInputText</item>
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
    </style>

In Manifest.xml  give android:theme="@style/SampleTheme"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijayakumar.datepicker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/SampleTheme" >
        <activity
            android:name="com.rajeshvijayakumar.datepicker.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 Here



Friday, 25 January 2013

Seek Bar Example in Android


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="10" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="" />

</LinearLayout>

MainActivity.java

package com.rajeshvijayakumar.seekbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.rajeshvijayakumar.seekbar.R;

public class MainActivity extends Activity implements OnSeekBarChangeListener {

    private SeekBar mSeekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSeekBar = (SeekBar) findViewById(R.id.seek_bar);
        mSeekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Toast.makeText(MainActivity.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mSeekBar.setSecondaryProgress(seekBar.getProgress());
        Toast.makeText(MainActivity.this, "Stopped Tracking Seekbar", Toast.LENGTH_SHORT).show();
    }
}

Output :






Source Code : Download this Example here








Monday, 21 January 2013

Time Picker Example in Android

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/time_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Time" />

</RelativeLayout>

MainActivity.java

package com.rajeshvijayakumar.timepicker;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends Activity implements OnClickListener {

    private Button mTimeButton;

    private Calendar mCalen;
    private int hourOfDay;
    private int minute;
    private int ampm;

    private static final int Time_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTimeButton = (Button) findViewById(R.id.time_button);
        mCalen = Calendar.getInstance();
        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
        minute = mCalen.get(Calendar.MINUTE);
        ampm = mCalen.get(Calendar.AM_PM);
        mTimeButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case Time_PICKER_ID:
                return new TimePickerDialog(this, TimePickerListener,
                        hourOfDay, minute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener TimePickerListener =
            new TimePickerDialog.OnTimeSetListener() {

                // while dialog box is closed, below method is called.
                public void onTimeSet(TimePicker view, int hour, int minute) {

                    mCalen.set(Calendar.HOUR_OF_DAY, hour);
                    mCalen.set(Calendar.MINUTE, minute);

                    int hour12format = mCalen.get(Calendar.HOUR);
                    hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                    minute = mCalen.get(Calendar.MINUTE);
                    ampm = mCalen.get(Calendar.AM_PM);
                    String ampmStr = (ampm == 0) ? "AM" : "PM";
                    // Set the Time String in Button
                    mTimeButton.setText(hour12format + " : " + minute + " / " + ampmStr);
                }
            };

    @Override
    public void onClick(View v) {
        showDialog(Time_PICKER_ID);
    }
}

Output :








Source Code : Download this Example Here




Date Picker Example in Android


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Date" />

</RelativeLayout>

MainActivity.java


package com.rajeshvijayakumar.datepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;

public class MainActivity extends Activity implements OnClickListener {

    private Button mDateButton;

    private Calendar mCalen;
    private int day;
    private int month;
    private int year;

    private static final int DATE_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateButton = (Button) findViewById(R.id.date_button);
        mCalen = Calendar.getInstance();
        day = mCalen.get(Calendar.DAY_OF_MONTH);
        month = mCalen.get(Calendar.MONTH);
        year = mCalen.get(Calendar.YEAR);
        mDateButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, datePickerListener,
                        year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener =
            new DatePickerDialog.OnDateSetListener() {

                // while dialog box is closed, below method is called.
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // Set the Date String in Button
                    mDateButton.setText(day + " / " + (month + 1) + " / " + year);
                }
            };

    @Override
    public void onClick(View v) {
        showDialog(DATE_PICKER_ID);

    }
}


Output :







Source Code : Download this Example Here




Sunday, 20 January 2013

Action Bar Sherlock : Creating Action Menu Items in ICS Action Bar in Android

Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this

MainActivity.java

package com.rajeshvijayakumar.actionmenuitem;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    private MenuItem mGoItem;
    private MenuItem mClearItem;
   
    private static final int GO_ITEM_ID = 1;
    private static final int CLEAR_ITEM_ID = 2;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        mGoItem = menu.add(0, GO_ITEM_ID, 0, null);
        mGoItem.setIcon(R.drawable.abs__ic_go).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);
        mClearItem = menu.add(0, CLEAR_ITEM_ID, 0, null);
        mClearItem.setIcon(R.drawable.abs__ic_clear).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case GO_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Go' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        case CLEAR_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Clear' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        }
       
        return false;
    }   
}

Output :



Source Code : Download this Example with Library Here

ActionBar Sherlock : Drop down Ics Spinner and applying Style in android


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

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

Friday, 18 January 2013

ActionBarSherlock - How to Add ActionBar in your Android Project


To Add Common ActionBar to your entire Android Application follow the steps :

In Manifest File, do as shown below :



In Activity, do as  shown below :






To Add ActionBar for Particular Screen / Activity follow the steps :

In Manifest.xml










(or)







How to add ActionBarSherlock Library to your Android Application Project


Step 1 :   Download the ActionBarSherlock (ABS) Library from the website        
               http://actionbarsherlock.com/

Step 2 : You will get that library in the ".zip / .tar" format. After Downloading that ABS library extract it.


Step 3 : Now Import that ABS library as a project in your eclipse. Below Screenshot demonstrates how to
             import project to your eclipse.













Step 4 : Now Open your Project,Make "Right Click" on Project Node, a list of options will open and you will find "Properties" at the bottom of the options. Select it and continue with the screen flow.








Now, you are ready to access ActionBarSherlock Library Features in your Android Application Project.




Spinner Example in Android


In this example, we will discuss and demonstrates how to implement spinner component in your application.

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

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp" />

</RelativeLayout>


MainActivity.java


package com.example.spinner_example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener {

    private Spinner mSpinner;
    private ArrayAdapter<CharSequence> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSpinner = (Spinner) findViewById(R.id.spinner);

        mAdapter = ArrayAdapter.createFromResource(this, R.array.names,
                android.R.layout.simple_spinner_item);

        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(mAdapter);
        mSpinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> spinnerAdapView, View view,
            int position, long id) {

        Toast.makeText(
                MainActivity.this,
                "You have Selected "
                        + spinnerAdapView.getSelectedItem().toString(),
                Toast.LENGTH_LONG).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
}

Output :