Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically set entries of spinner in android?

Tags:

android

I have made various string-arrays in the string.xml file and I have to set different arrays as entries for the spinner according to certain condition in Java. Is it possible or is database the only way to do so. Thanks in advance.

like image 691
Sagar Acharya Avatar asked Jan 23 '17 15:01

Sagar Acharya


People also ask

How do you use a spinner in an android app explain with an example?

Example to demonstrate the SpinnerUse ArrayAdapter to store the courses list. Create a single MainActivity that contains the spinner and on clicking any item of spinner Toast with that course name will be shown. Creating the activities: There will be one activity and hence one XML file for MainActivity. activity_main.


1 Answers

You need to use an adapter and populate with tha array in xml file.

Specify the name of your array in xml at createFromResource method (second parameter).

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.my_array, android.R.layout.simple_spinner_item);   
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
like image 62
adalpari Avatar answered Oct 20 '22 05:10

adalpari