How to create Spinner and perform action in Android

How to create Spinner and perform action in Android - Hello friends U-See, In the article you read in this time with the title How to create Spinner and perform action in Android, We have prepared this article well for you to read and take the information in it. Hopefully the contents of the post Artikel Android, Which we write you can understand. Okay, happy reading.

Title: How to create Spinner and perform action in Android
link : How to create Spinner and perform action in Android

Read also


How to create Spinner and perform action in Android

Open main XML layout and drop Spinner widget or paste below code:

<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/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Now open Your Java File and paste below code:

package sel.listG; //package name

importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemSelectedListener;
importandroid.widget.ArrayAdapter;

importandroid.widget.Spinner;
importandroid.widget.Toast;
importandroid.app.Activity;


public class MainActivity extends Activity implementsOnItemSelectedListener {
      String values[]={"Select One","January", "July", "June", "Ravi", "Ravinder", "Abhishek", "Abhinav", "Harkishore", "Hardev"};

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Spinner lv=(Spinner)findViewById(R.id.spinner1);
            ArrayAdapter<?> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values);
//you can left the below one line because it is merging two layout
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            lv.setAdapter(adapter);
            lv.setOnItemSelectedListener(this);          
      }

      @Override
      public voidonItemSelected(AdapterView<?> a, View v, int pos,long id)
      {

Toast.makeText(getApplicationContext(),a.getItemAtPosition(pos)+"Selected", Toast.LENGTH_SHORT).show();
           
      }

      @Override
      public voidonNothingSelected(AdapterView<?> arg0) {
            // TODOAuto-generated method stub
           
      }
 }


Now Run your code...


That's How to create Spinner and perform action in Android

That's an article How to create Spinner and perform action in Android This time, hopefully can benefit for you all. Well, see you in other article postings.

You are now reading the article How to create Spinner and perform action in Android With link address https://u-see1.blogspot.com/2017/06/how-to-create-spinner-and-perform.html

Postingan terkait:

Belum ada tanggapan untuk "How to create Spinner and perform action in Android"

Post a Comment