Search Tutorials

Monday, 7 October 2013

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

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


public class MainActivity extends Activity implements OnItemSelectedListener {
      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 void onItemSelected(AdapterView<?> a, View v, int pos,long id)
      {

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

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


Now Run your code...

7 comments:

  1. As/a mohsin.i want to get he screen size at runtime. Can u plz help me

    ReplyDelete
    Replies
    1. w/a ..like this app https://play.google.com/store/apps/details?id=helper.plus ?

      check information page and than screen property.

      Delete
  2. Thanks for replying me.actually i want that every andriod mob support my app having diferrent width 'height.i used defferent methods for it but showing some error..can u plz help me brother?

    ReplyDelete
    Replies
    1. For this, android gives us multiple folders facility for putting xml according to density. Don't use any method.

      Delete
  3. How can i use this brother?

    ReplyDelete
  4. I have button on the top of which i have a te.xtview.the height of button changes on every click but the textview goes here and there.now i want to change the position of textview with the change of hight of bttn.can u plz help me brother

    ReplyDelete
  5. sir ,could you plz upload source code for spinner with multiple selection option populate from mysql php??

    ReplyDelete

Back to Top