Onclick of Spinner

We have created and example in which when you select any item from Spinner it will show respective data. For this, call setOnItemSelectedListener  and override onItemSelected method.

Keep all the images in the drawable folder To reference those images we write R.drawable.imageName.

MainActivity.java :

public class MainActivity extends Activity {

       Spinner spinner;
       TextView tv;
       ImageView iv;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              spinner = (Spinner) findViewById(R.id.spinner1);
              tv = (TextView) findViewById(R.id.textView1);
              iv = (ImageView) findViewById(R.id.imageView1);
             
              final String[] arr = {"Java" ,"Android", "C", "C++"};
             
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,arr);
              spinner.setAdapter(adapter);
             
              spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                     @Override
                     public void onItemSelected(AdapterView<?> arg0, View arg1,
                                  int arg2, long arg3) {
                           switch (arg2) {
                          
                           case 0:
                                  iv.setImageResource(R.drawable.java);
                                  tv.setText("Java is Object Oriented and Platform Indepenedent Langugage");
                                  break;
                           case 1:
                                  iv.setImageResource(R.drawable.android);
                                  tv.setText("Android is a Linux-based operating system for mobile devices such as smartphones and tablet computers. Applications in android are build using Java Programming language");
                                  break;
                           case 2:
                                  iv.setImageResource(R.drawable.c);
                                  tv.setText("C is an procedural language. It was designed to be compiled using a relatively straightforward compiler");
                                  break;
                           case 3:
                                  iv.setImageResource(R.drawable.cplus);
                                  tv.setText("C++ introduces object-oriented programming (OOP) features to C. It offers classes, which provide the four features commonly present in OOP (and some non-OOP) languages: abstraction, encapsulation, inheritance, and polymorphism.");
                                  break;
                           default:
                                  break;
                           }
                     }

                     @Override
                     public void onNothingSelected(AdapterView<?> arg0) {
                           // TODO Auto-generated method stub
                          
                     }
                    
              });
             
             
             
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="73dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="110dp"
        android:text="TextView" />

</RelativeLayout>































**********************************************************************************

Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com







No comments:

Post a Comment