Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:19:58+00:00 2026-05-30T04:19:58+00:00

In my app I need a spinner on a Button click with dynamically loaded

  • 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.
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:20:00+00:00Added an answer on May 30, 2026 at 4:20 am

    it can be show using spinnner.performClick();

    but @jitendra sharma ‘ way is better

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my app I need to use the Carbon file manager API to get
In my app i need to save changed values (old and new) when model
In my app users need to be able to enter numeric values with decimal
I have a LAMP (PHP) web app which need to interface with programs on
I'm currently working on a Silverlight app and need to convert XML data into
I have an app I need to create an installer for, and all the
I have an app need to load photos from simulator photo library. After I
Do the lite version and the full version of an app need to have
In my app i need a map to loaded by passing address,city, state province
In my android app I need to do something like this: I have 5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.