Created
January 15, 2014 05:10
-
-
Save ishitcno1/8431165 to your computer and use it in GitHub Desktop.
Android spinner widget.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" > | |
<Spinner | |
android:id="@+id/main_animals" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.edinstudio.app.training.ui; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
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 ActionBarActivity implements | |
OnItemSelectedListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
String[] animals = { "Panda", "Tiger", "Lion", "Elephant" }; | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, | |
android.R.layout.simple_list_item_1, animals); | |
Spinner spAnimals = (Spinner) findViewById(R.id.main_animals); | |
spAnimals.setAdapter(adapter); | |
spAnimals.setOnItemSelectedListener(this); | |
} | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int pos, | |
long id) { | |
Toast.makeText(this, "Selected: " + parent.getItemAtPosition(pos), | |
Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment