Android spinner is used to display a drop down list to be selected by user. To make android spinner working, just follow these simple steps.
1. Create activity layout activity_spinner.xml
Thank you for visiting our website. Just comment below if you have any question to ask.
1. Create activity layout activity_spinner.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="12dp"> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true"> </Spinner> </RelativeLayout>2. Copy and paste array of string below to strings.xml file inside values folder.
<string-array name="month_array"> <item>January</item> <item>Febuary</item> <item>March</item> <item>April</item> <item>May</item> <item>June</item> <item>July</item> <item>August</item> <item>September</item> <item>October</item> <item>November</item> <item>December</item> </string-array>3. Make activity class SpinnerActivity.java
package com.example.myapplication.spinner; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.example.myapplication.R; /** * Created by SONY on 16/10/2016. */ public class SpinnerActivity extends Activity { private Spinner spinner; private ArrayAdapter<CharSequence> spinnerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spinner); spinner = (Spinner) findViewById(R.id.spinner); spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.month_array, android.R.layout.simple_spinner_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // do something after selected item here } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }4. Make AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.example.myapplication.spinner.SpinnerActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>5. After you run the application and click the spinner, it should display the dropdown item like the picture below.
Thank you for visiting our website. Just comment below if you have any question to ask.
No comments:
Post a Comment