Tuesday, September 18, 2012

Spinner View

Spinner View - Hallo sahabat Google Android Developer Tutorial, Pada Artikel yang anda baca kali ini dengan judul Spinner View, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Spinner View
link : Spinner View

Baca juga


    Spinner View


     SPINNER VIEW Tutorial


    Spinners are like drop down menu which provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.You can add a spinner to your layout with the Spinner object. You should usually do so in your XML layout with a <Spinner> element.

    1.Create  Android Project as per the details listed in the table
    Property name
    Property value
    Project name
    SRM_SpinnerViewTutorial
    Package name
    in.ac.srmuniv.spinnerviewtutorial
    Activity name
    MainActivity
    Layout xml name
    activity_main


    2.Copy the code to activity_main.xml in res/layout folder 

    <?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/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"/>       
    </LinearLayout>
    2.Copy the code in MainActivity.java Activity
    package in.ac.srmuniv.spinnerviewtutorial;
    importandroid.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    importandroid.widget.AdapterView;
    importandroid.widget.ArrayAdapter;
    importandroid.widget.Spinner;
    importandroid.widget.Toast;
    importandroid.widget.AdapterView.OnItemSelectedListener;
    public  class MainActivity extends Activity

    {
        String[] bond_films = {

                "Dr.No",
                "Man with Golden gun",
                "You only Live twice",
                "Live and Let die",
                "Thunderball",
                "License to Kill",
                "From Russia with Love",
                "Moonraker",
                "Octopussy",
                "A View to Kill",
                "On Her Majesty Service",
                "Golden Eye"

        };

        Spinner s1;
        @Override   
        public  void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            s1 = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<String> adapter = new
                    ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, bond_films);     
            s1.setAdapter(adapter);
            s1.setOnItemSelectedListener(newOnItemSelectedListener()    
            {
                public  void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3)
                {
                    int index = s1.getSelectedItemPosition();
                    Toast.makeText(getBaseContext(),
                            "You have selected item : " + bond_films [index],Toast.LENGTH_SHORT).show();                       }
                public  void onNothingSelected(AdapterView<?> arg0){}

            });
        }

    }
    3.The AndroidManifest.xml file need not be modified.
    4.Run the application on the Android emulator. Figure 1 shows the Spinner view in action.
    Figure 1 Shows The Spinner view in action Figure 2 Shows selected item from Spinner list
    CODE EXPLANATION
             The above program creates an ArrayAdapter object and associates it with the Spinner view.When an item in the Spinner view is selected, onItemSelectionListener interface  callback method onItemSelected() is called  Toast notification is used to display the item selected.Instead of displaying the items in the ArrayAdapter as a simple list, you can also display them using radio buttons. To do so, modify the second parameter in the constructor of the ArrayAdapter class:
      ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,bond_films);    
                                                Figure 3: The Spinner view with multiple choice



    Demikianlah Artikel Spinner View

    Sekianlah artikel Spinner View kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

    Anda sekarang membaca artikel Spinner View dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2012/09/spinner-view.html

    Artikel Terkait

    Spinner View
    4/ 5
    Oleh

    Berlangganan

    Suka dengan artikel di atas? Silakan berlangganan gratis via email