Setting the value of the spinner dynamically

how to set selected value in spinner dynamically in android
android set spinner value from database
how to set default value in spinner dynamically in android
spinner kotlin
how to set string value in spinner in android
kotlin set spinner value
dynamic drop down list in android
android spinner with id and value

I have an ArrayAdapter as follows

ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);

This is the adapter which I am going to use for my Spinner.I have value sets (String array) for the Spinner in my strings.xml file.

How can I set the <string-array> in string.xml to the spinner pragmatically? Please help!

Just add it to your ArrayAdapter when you create it.

String[] yourArray = getResources().getStringArray(R.array.array_name);

ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, yourArray);

How to set selected item of Spinner by value, not by position , How to programmatically set drawableLeft on Android button? asked Jul 24, 2019 in Java by Nigam� How to set selected item of Spinner by value, not by position? (14) I have a update view, where I need to preselect the value stored in database for a Spinner. I was having in mind something like this, but the Adapter has no indexOf method, so I am stuck.

You can try something like this :

String[] mTestArray;

mTestArray = getResources().getStringArray(R.array.testArray);
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,mTestArray);

and then attach the adapter to the Spinner.


Hope this helps!!!

How to set selected item of Spinner programmatically in Android, Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the� The spinner provides a way to set the selected valued based on the position using the setSelection(int position) method. Now to get the position based on a value you have to loop thru the spinner and get the position. Here is an example

This tutorial: Android Spinners points out that you would use the following code to set the Spinner array based on a resource:

ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(this,
    R.array.string-array-id, android.R.layout.simple_spinner_item);
dataAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Using the createFromResource() method instead of new and providing the resource ID of your array should provide the intended result. See the tutorial for more details.

Spinners, This example demonstrates how do I set the selected item of Spinner by value instead of by position on Android.Step 1 − Create a new project� from - how to set value in spinner dynamically in android Set Key and Value in spinner (6) Better approach to populate spinner with key and value should be : Step 1 : Create POJO class which will take care of key and value

spinner2 = (Spinner) findViewById(;
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);  
String[] itemNames = getResources().getStringArray(R.array.stringArrayName); 

   for (int i = 0; i < 16; i++) // Maximum size of i upto --> Your Array Size 


How to set selected item of Spinner by value instead of by position , Android Spinner Programmatically add/remove Items add/remove items from Spinner</string> <string name="menu_settings">Settings</string> <string� In the code it states: A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed. but it doesn't seem possible given the code. Anyway - my code:

If you want to load data from web service and set that data in your spinner you can use this bellow code:-

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    List<String> usersList;
    ArrayAdapter<String> catAdapter;

    protected void onCreate(Bundle savedInstanceState) {

        spinner = (Spinner)findViewById(;
        usersList = new ArrayList<String>();

        catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, usersList);



    public void getHttpResponse() {

        String url = "";

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")

        client.newCall(request).enqueue(new Callback() {

            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);

            public void onResponse(Call call, Response response) throws IOException {

                final String mMessage = response.body().string();

                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        Log.e("Response", mMessage);
                        try {
                            JSONObject users = new JSONObject(mMessage);
                            JSONArray usersArr = users.getJSONArray("users");

                            for(int i =0; i < usersArr.length(); i++){
                                JSONObject user = usersArr.getJSONObject(i);
                                Log.e("user", user.getString("username"));


                        } catch (JSONException e) {


You can read complete information in this post:- Add dynamic data to spinner

Android Spinner Programmatically , Create a spinner from XML resource array; Create a spinner dynamically using String Array and ArrayAdapter; Ability to capture spinner selection on change� In this tutorial we are dynamically setting up spinner values on button click. So app user can only click on button and it will automatically setup its choice inside spinner. So here is the complete step by step tutorial for How to set selected item in spinner in android on button click programmatically.

Android spinner example using , Setting the value of the spinner dynamically. Question. I have an ArrayAdapter as follows ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this� after getting your data as a cursor from the database you will set the cursor to the adapter like this: cursorAdapter.changeCursor(cursorFromDB); However, if the data is in a java array you can user an Arrayadapter

Setting the value of the spinner dynamically, Default value of the android spinner will be currently selected value and by In this file, we use the TextView widget and also set its attributes. Spinner text size are called as spinner inside items font size which is easily changeable if we are setting up textview items all we have do is defining text size in textview layout xml. So here is the complete step by step tutorial for How to change spinner text size in android programmatically.

Dynamic Spinner in Kotlin, android spinner set default selected item how to set value in spinner dynamically in android android set spinner value from database spinner with textview in� If the resulting array has 0 (zero) elements, the property is not indexed. To set the value of an indexed property, call the SetValue(Object, Object, Object[]) overload. If the property type of this PropertyInfo object is a value type and value is null, the property will be set to the default value for that type.

  • Thanks a lot for the help! I really appreciate it!
  • We need to pass texview id as well. ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),R.layout.row_spinner_text,, dataArray);
  • Thanks a lot for the reply. I really appreciate it! +1 from me :)