Pages

Wednesday, June 8, 2016

Android Drop Down list Example

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/static_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp" />

    <Spinner
        android:id="@+id/dynamic_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
MainActivity.java
package in.blogspot.kantapp.androiddropdownlistexample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);

        // Create an ArrayAdapter using the string array and a default spinner
        ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
                .createFromResource(this, R.array.topMovies,
                        android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        staticAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Apply the adapter to the spinner
        staticSpinner.setAdapter(staticAdapter);

        Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner);

        String[] items = new String[]{"Milk Tea", "Green Tea", "Black Tea"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);

        dynamicSpinner.setAdapter(adapter);

        dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                Log.v("item", (String) parent.getItemAtPosition(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });
    }
}
string.xml
<resources>
    <string name="app_name">Android Drop Down list Example</string>
    <string-array name="topMovies">
        <item>The Dark Knight</item>
        <item>Batman Begins</item>
        <item>Superman </item>
        <item>Deadpool</item>
        <item>Guardians of the Galaxy</item>
        <item>Captain America: The Winter Soldier</item>
        <item>Hellboy</item>
        <item>Blade</item>
        <item>Ant-Man</item>
        <item>The Amazing Spider-Man </item>
    </string-array>
</resources>

No comments:

Post a Comment