Android Spinner Example - Javatpoint
Android Spinner Example - Javatpoint
com/android-spinner-example
Content Menu ▼
1 de 4 27/12/2015 17:45
Android Spinner Example - javatpoint http://www.javatpoint.com/android-spinner-example
Android spinner is like the drop down menu with multiple values from which
the end user can select only one value.
Android spinner is associated with AdapterView. So you need to use one of the
adapter classes with spinner.
activity_main.xml
Drag the Spinner from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
<RelativeLayout xmlns:androclass="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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" />
</RelativeLayout>
Activity class
Let's write the code to display item on the spinner and perform event handling.
File: MainActivity.java
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
2 de 4 27/12/2015 17:45
Android Spinner Example - javatpoint http://www.javatpoint.com/android-spinner-example
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
3 de 4 27/12/2015 17:45
Android Spinner Example - javatpoint http://www.javatpoint.com/android-spinner-example
Output:
← prev next →
Share 0
4 de 4 27/12/2015 17:45