how to add items to the spinner dynamically in android?

how to add items to the spinner dynamically in android?

Asked by: Kelvin330 | Posted: 25-01-2022

Answer 1

Spinner spinner = (Spinner)findViewById(;
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,;

the above is in case of the array adapter, I believe you know how to populate values with ArrayAdapter .

How can we do this in case of the SimpleCursorAdapter, i.e if we have 2 spinners and if we select the values of one spinner (that is getting the value from SimpleCursorAdapter) depending on some criteria the other spinner should be filled with values. how can we achieve that?

Answered by: Ada347 | Posted: 26-02-2022

Answer 2

By calling ArrayAdapter.add to the Spinner's ArrayAdapter.

Answered by: Lucas703 | Posted: 26-02-2022

Answer 3

You can follow this way

public static void selectSpinnerItemByValue(Spinner spnr, long value){
SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
for (int position = 0; position < adapter.getCount(); position++)
    if(adapter.getItemId(position) == value)
} }

You can use the above like:

selectSpinnerItemByValue(spinnerObject, desiredValue);

you can also select by index directly like


Answered by: Lenny370 | Posted: 26-02-2022

Similar questions

android - How to update a spinner dynamically?

I've been trying to update my spinner in android dynamically but nothing I try has been working. This is the following code I'm using to update the spinner. typeList = dbAdapter.getList(); //array list with the values adapter.notifyDataSetChanged(); groupSpinner.postInvalidate(); groupSpinner.setAdapter(adapter); The values of typeList are correct but they're not being updated in...

How to create a Spinner dynamically in android?

I used the HelloTabWidget from here ( as a starting point. Now I edited the onCreate for the first Tab: // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("tab0").setIndicator("Tab0", res.getDrawable(R.drawable.ic_tab_artists)); spec.setContent(new MyTabContentFactory(this, R.layout.tab0...

android - How to add data dynamically into the spinner from xml

Hello I'm downloading XML and parsing data. I want to add data to the spinner. The data updates every time I run the application. public class Main extends ListActivity { TextView valueTextView; HashMap&lt;String, String&gt; name=null; private HashMap&lt;String, String&gt; array_spinner[]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle save...

android - How to select an item from dynamically added spinner?

I am adding a spinner dynamically in a layout, as; public class FRQuestionsDetail extends Activity implements OnClickListener, OnItemSelectedListener { private TextView tvCategoryType; private TextView tvCategoryObtainScore; private TextView tvCategoryTotalScore; private TextView tvQuestionNumber; private TextView tvQuestionPercent; private TextView tvQuestion; priva...

android - Add Spinner dynamically in a loop

I am adding say 'n' spinners in a loop. But only last added spinner is visible when I ran my code. Please see my code below Context context = this; ArrayList&lt;ArrayList&lt;String&gt;&gt; list = new ArrayList&lt;ArrayList&lt;String&gt;&gt;(); ArrayList&lt;String&gt; hi = new ArrayList&lt;String&gt;(); hi.add("Test 1"); hi.add("Test 2"); list.add(hi); Array...

Dynamically Adding Spinner Via java in android

I am currently trying to get a function working similar to how adding phone numbers work in your android phone contact list. What I mean by this is that if a contact has more than one phone number you can click the + button and it adds another field to the display for another number. What I'm trying to do is when the user clicks a + button it adds a new spinner right below the existing spinner with the same data i...

Android:Get result Dynamically through Spinner

I,m new to android can anybody help me out this simple one but I can't able to recognize how this execution process.I need to get Result Dynamically through Spinner but here spinner executes while at runtime only(Without click on spinner) Sorry to ask this question im unable to solve this...:( protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(sav...

android - How to set the spinner mode dynamically?

I have created a Spinner dynamically, which is easy with new Spinner(context). But now I need to set the spinnerMode dynamically, and I find no method to do so. What should I do?

Android Spinner dynamically change items

I want to use a Spinner in my App. The context (items) of the Spinner are depending on the user preferences done in the Settings of the App. I created the Spinner with no items. After loading the App the Spinner has one Entry. To get the Entries the User has to go to the menu and load an image. Based on this the items are loaded into a String Array. This String Array will be hand over to a Adapter. See Code: ...

android - Change spinner states dynamically

I am trying to change state style of a spinner programmatically. But when I press an item, all of them are changed! Before pressing everything's fine, the colors are good: When pressing, all items become green: I did this in my ...

eclipse - how to add buttons Dynamically in List Activity in Android?

friends, i want to put description and buttons dynamically can any one guide how can i do that?? i have data in the form of object list/array i.e ID,Title and want to display in list activity like.. TITLE [Details Button] [Edit Button] (repeating in list activity) any help would be appriciated.

Android: dynamically creating controls and orientation change

Currently I am working on an Android application that is dynamically creating controls. Everytime a user would click a button a new EditText appears below the button and the user can interact with the EditText. However if the screen orientation changes, the EditText's that the user created also disappears. Code sample of user creating a EditText: (located in a onClick(), p is basic layoutParamas, and layout is ...

Can we dynamically check if npot is supported in android?

Is there a way to query if the OpenGL ES implementation on android supports non-power of two textures?

android - Dynamically setting links to text in strings.xml

I'm trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenance). So, I have my links in res/values/strings.xml: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; ... &lt;string name="link1"&gt;;/string&gt; &lt;string name="link2"&gt;

android - How can dynamically add a View to a ViewGroup and vertically align with one of its child

I have a ViewGroup and it has a few children. And one of them is a TextView ("+id/text"). In my code, I would like to know how can I add a new View or ViewGroup which will be positioned vertically aligned and below the TextView (+"id/text")? Thank you. I have followed the advice below and try to use TableLayout. As a test, I try to layout statically to make sure things are aligned correctly. Here...

android - How to change the title of the Tab Dynamically

I have three tabs in my Application. On an event under one Tab, i want to change the title of an another existing Tab. This is the title that we provide while adding the tabs to the TabHost. Eg: TabHost.addTab(tabHost.newTabSpec("Tab2")).setIndicator("I need to be Changed dynamically").setContent.... In the above example, the title of the tab2 that i provided under setIndicator(), should be changed dynamic...

layout - how to set custom title bar TextView Value dynamically in android?

friends, i have created custom title bar using following titlebar.xml file with code &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="" android:id="@+id/myTitle" android:text="This is my new title" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/titletextcolor" andr...

android - Dynamically sizing Views as a percentage (or similar)?

To use as an example: lets say that I have 2 EditTexts and one Button that I'm using as a login form. I want the EditTexts to be the same size, one after the other, with the login Button half their widths. Someth...

android - Can I dynamically add a View to home screen widget?

I am writing a home screen widget. Is it possible to add a View, e.g. ImageView, to a home screen widget through RemoteViews? I want to generate the views for the home screen widget dynamically. Thanks. updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_news); updateViews.setTextViewText(, mTitle); updateViews.setImageViewBi...

android - Dynamically adjust the column and row of a Table Layout based on orientation

Is it possible for my android application to dynamically adjust the no of column and no of row of my TableLayout based on orientation? For example, when in landscape mode, the TableLayout is 3x2 and when in portrait mode, the TableLayout is 2x3? Thank you.

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android