0

In my app I need a spinner on a Button click with dynamically loaded values. My need is in the XML file I should not use spinner control. I have to show the spinner without xml on a button click:

i use the following code but i did not get any error and spinner. i know i miss some lines. please Help me.

my code:

showSpinner is a Button control.

The MyOnItemSelectedListene is outside of the onCreate(..) method.

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class DynamicSpinnerActivity extends Activity {
    /** Called when the activity is first created. */
    Button b;
    LinearLayout linearLayout1;
    String years[] = new String[12];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b = (Button) findViewById(R.id.button1);
        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//              Toast.makeText(DynamicSpinnerActivity.this, "sdfsdf", Toast.LENGTH_SHORT).show();
//              Log.e("tag", "msg");
                Spinner spinnner = new Spinner(DynamicSpinnerActivity.this);
                int thisYear = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = 0; i < 12; i++) {
                    years[i] = Integer.toString(thisYear);
                    thisYear = thisYear + 1;
                }
                Log.e("tag", "msg"+years.length);
                ArrayAdapter<String> expYearAdapter = new ArrayAdapter<String>(DynamicSpinnerActivity.this, android.R.layout.simple_spinner_item, years);
                expYearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnner.setAdapter(expYearAdapter);
//              linearLayout1.addView(spinnner);
                spinnner.performClick();
                spinnner.setOnItemSelectedListener(new MyOnItemSelectedListener());

            }
        });
    }

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
             Toast.makeText(parent.getContext(), "The planet is " +parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
             Log.e("values", parent.getItemAtPosition(pos).toString());
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }

}
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

3 Answers3

0

you have created spinner its okay, but you also do need to add this layout to your contents on screen, like if you have llHolder to hold spinner, add spinner to

llHolder.addView(spinner);
jeet
  • 29,001
  • 6
  • 52
  • 53
0

it can be show using spinnner.performClick();

but @jitendra sharma ' way is better

Ajay
  • 4,850
  • 2
  • 32
  • 44
0

If you dont want to display the spinner than default make it invisible and make it visible on button click

Hardi Shah
  • 29
  • 2
  • 7