Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make selected item spinner function in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 156
    Comment on it

    In the below example I have created a Selected Item from spinner in android. Here I have added first aTextView and Spinner in activity_main.xml layout, after then you can see in MainActivity I have used String class and created a ArrayAdapter. I have used OnItemSelectedListener () and Toast function so when you select any Company name from string then that particular company name will be shown on your screen which you ahve selected with the help of toast function.You can see below program it clearly describe you "how to make selected item spinner function in android".
    Srep(1)activity_main.xml layout-
     

    <?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" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#eeb005ce"
            android:textSize="20dp"
            android:text="Company"  />
    
        <Spinner android:layout_height="wrap_content"
            android:id="@+id/spinner1"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content" />
    
    </LinearLayout>
    
    

    Srep(2)MainActivity-
     

    public class MainActivity extends AppCompatActivity {
    
    
            Spinner sp;
            ArrayAdapter<String> adapter;
            String company[] = { "Evon", "HCL", "Wipro", "TCS", "Apple", "Ericsson", "SEVEN"};
    
            public void onCreate(Bundle savedInstanceState)
            {
    
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                sp = (Spinner) findViewById(R.id.spinner1);
    
                adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, company);
                sp.setAdapter(adapter);
    
                sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
                {
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                                               int arg2, long arg3)
                    {
                        Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),
                                Toast.LENGTH_LONG).show();
                    }
                    public void onNothingSelected(AdapterView<?> arg0)
                    {
    
                    }
                });
            }
        }
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: