First of all we need to create an android project. (I used eclipse)
File > New > Android Application Project
Then we need to declare an array-list in order to load values in spinner view. create array-list on Strings.xml file located under values folder.
Here the simple array-list in Strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Load Spinner</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="fruits_prompt">Choose a fruit</string> <string-array name="fruits" > <item >Apple</item> <item >Mango</item> <item >Orange</item> <item >Pineapple</item> </string-array> </resources> |
Now add spinner view and Auto complete text view inside activity_main.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <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" tools:context=".MainActivity" > <AbsoluteLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:id="@+id/spinner1" android:layout_width="116dp" android:layout_height="40dp" android:layout_x="22dp" android:layout_y="111dp" android:entries="@array/fruits" android:prompt="@string/fruits_prompt" android:visibility="visible" /> <AutoCompleteTextView android:id="@+id/txtFruitName" android:layout_width="184dp" android:layout_height="wrap_content" android:layout_x="23dp" android:layout_y="160dp" android:ems="10" android:hint="Fruit" > <requestFocus /> </AutoCompleteTextView> </AbsoluteLayout> </RelativeLayout> |
Finally edit the MainActivity.java file to load the spinner view and get the selected text to the text field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.example.loadspinner; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Spinner; public class MainActivity extends Activity { Spinner spinner1; AutoCompleteTextView selectedtxt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner1 = (Spinner) findViewById(R.id.spinner1); selectedtxt = (AutoCompleteTextView) findViewById(R.id.txtFruitName); spinner1 = (Spinner) findViewById(R.id.spinner1); @SuppressWarnings("rawtypes") ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.fruits, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(adapter); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { public void onNothingSelected(AdapterView<?> parentView) { } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); String text = spinner1.getSelectedItem().toString(); selectedtxt.setText(text); } }); } } |
Now we are done with coding. Run the app
Download sample android project(GitHub): Click here
(This is a Android Studio project)
Great Article
ReplyDeleteAndroid Final Year Project Ideas for Computer Science
Project Centers in Chennai