To make a spinner we take the spinner in xml file. Directly drag and drop the spinner. Spinner the way to select any item from the list that we want to choose. Spinner view is like a drop down list on any thing.
In java file we take the array to store the values that we want to see in the drop down list. After storing the values in the array we apply the toast on that values. when a user select any item then the result will show for some period of time. We apply the toast so result will show for certain period of time.
In manifest.xml no need to change any data. All services in the manifest file is already in correct format so no need to change in manifest file.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop = "true"></Spinner>
</LinearLayout>
In java file we take the array to store the values that we want to see in the drop down list. After storing the values in the array we apply the toast on that values. when a user select any item then the result will show for some period of time. We apply the toast so result will show for certain period of time.
In manifest.xml no need to change any data. All services in the manifest file is already in correct format so no need to change in manifest file.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop = "true"></Spinner>
</LinearLayout>
SpinnerView.java
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 android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerView extends Activity {
String[] androidBooks =
{
"Cup-Cake",
"Donut",
"Eclair",
"Froyo",
"Gingerbeard",
"Honeycomb",
"Ice-cream sandwitch",
"Jelly bean",
"Kitkat",
"Lollipop"
};
Spinner sp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,androidBooks);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getBaseContext(),arg0.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sai.samples.views"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SpinnerView"
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>
<uses-sdk android:minSdkVersion="7" />
</manifest>
No comments:
Post a Comment