This is my spinner in xml
<Spinner
android:id="#+id/product_details_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rl_3"
android:layout_alignRight="#+id/rl_3"
android:layout_below="#+id/rl_3"
/>
and this is my java code
List<String > sizelist = new ArrayList<String >();
spinner = (Spinner) rootView.findViewById(R.id.product_details_spinner);
sizelist.add("Select Size");
sizelist.add("small");
sizelist.add("medium");
sizelist.add("large");
ArrayAdapter<String > adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item,sizelist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I dont know why I am unable to display a simple spinner. I can view it in design but when I run on device spinner is not displayed.
Please help!
Edit: I tried using string array in xml its working but I need to give the array from java as this will be dynamic
<string-array name="items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
</string-array>
you set ` android:layout_alignLeft="#+id/rl_3"
android:layout_alignRight="#+id/rl_3" you are setting both so it is not working, you use only one
and aslo do it
ArrayAdapter<String > adapter = new ArrayAdapter<String >(getActivity(),android.R.layout.simple_spinner_item,sizelist);
I became really silly while testing your code, What happened was The spinner was behind the Navigation bar when I tested your code in new Project.
Only after adding these attribute It became visible and everything worked fine
<Spinner
android:id="#+id/product_details_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
May be you are facing the same, Have a look and be sure Your Spinner is visible at preview pane.
Related
I have a spinner in my layout that I would like to populate with pre-determined, hard-coded data.
Of course I can dynamically add the data from my Activity class, but I want to know if there is a way to do this in the xml itself.
All of the choices that I want to appear in the spinner are present in arrays.xml. Is there a way to plug in this data into the Spinner?
plug this into your spinner assuming that you have properly created you xml array...
android:drawSelectorOnTop="true"
android:entries="#array/array_name"
Your String Resources you should add the array like so...
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
This worked for me with a string-array named “count” loaded from the projects resources:
Spinner spinnerCount = (Spinner)findViewById(R.id.spinner_counts);
ArrayAdapter<String> spinnerCountArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.count));
spinnerCount.setAdapter(spinnerCountArrayAdapter);
let me know if it will fulfill your requirements.
This is my resource file (res/values/arrays.xml) with the string array:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name=“count”>
<item>0</item>
<item>5</item>
<item>10</item>
<item>100</item>
<item>1000</item>
<item>10000</item>
</string-array>
</resources>
I Have a simple spinner and would disable an element.
Could I disable elements from the xml files?
Or i need to code something in JAVA?
<Spinner
android:id="#+id/stato"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:entries="#array/feedbacktypelist"
android:layout_below="#+id/textview">
</Spinner>
arrays.xml
<resources>
<string-array name="feedbacktypelist">
<item>#string/stato1</item>
<item>#string/stato2</item>
<item>#string/stato3</item>
<item>#string/stato4</item>
<item>#string/stato5</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="stato1">ITALIA</string>
<string name="stato2">SPAGNA</string>
<string name="stato3">GERMANIA</string>
<string name="stato4">REPUBBLICA CECA</string>
<string name="stato5">INGHILTERRA</string>
</resources>
You can do two things, comment out the line in the resource array via highlighting the row and doing ctrl+/
or
You can remove the android:entries="#array/feedbacktypelist" line and add the list yourself but skipping the entry you don't want (most common me thinks) see how here How to create Spinner-list using CustomAdapter in android
Im currently working on an app and im trying to implement a spinner that drops down info (name, age, color) but which doesnt allows to select on the displayed info. Is there a wsy to do this?
you can try this link or use this code from the mentioned link
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
I have this following spinner in my main activity
My problem is that the spinner's value is required when submitting but i want its default value to be empty so i put in my arrays.xml an item with empty value. so when the app is lunched it will show a empty spinner.
The problem is that when they click the spinner and the drop down display the first selection is empty.
my solution is to put the first data as "Select" but is there a way that the value would be empty like in html select tag?
<option value="">Select</option>
<option value="data 1">data 1</option>
on android xml is this possible? or how to do it? i tried this but still not working. it passes the value "Select"
<item value="">Select</item>
<item value="data 1">data 1</item>
my spinner
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_arr"
android:tag="relation" />
my arrays.xml
<string-array name="spinner_arr">
<item></item>
<item>data 1</item>
<item>data 2</item>
<item>data 3</item>
<item>data 4</item>
<item>data 5</item>
</string-array>
Try using the prompt attr of Spinner, if that's not work you'll have to craete some adapter of your own.
take a look here:
How to make an Android Spinner with initial text "Select One"
In code, it's easy to use the Resources class to get an instance of an XML typed array resource and traverse its items. My question: is it possible to reference array resource items in XML itself as shown below
<resource>
<array name="items">
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</array>
<string name="itemThree">#array/items[2]</string>
</resource>
The format shown above does not work. Does anyone know if that is possible using a different format?
No, but I think the reverse works. Define your strings as string resources and refer to them as #string/... in the <item> elements.
I would just define itemThree in Java:
String itemThree = getResources().getStringArray(R.array.items)[2]
Ultimately, the XML gets inflated into Java objects, so it's not much difference IMO.