How to make an spinner that doesnt allows items to be selected? - 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>

Related

Set constant choices in Android Spinner from layout.xml

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>

unable to display spinner

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.

Values inside items in string-array xml

Is something like this possible??
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item value=My >Mercury</item>
<item value=Vs >Venus</item>
<item value=EA >Earth</item>
<item value=Ms >Mars</item>
</string-array>
</resources>
the listView will show the planets but when the user selects Mars
the value I get will be Ms.
No, there is no such direct way to use string array in android app resources. One of my suggestion is separate the label and value to two different string arrays, then use these string arrays in your code.
<string-array name="planet_label">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
<string-array name="planet_values">
<item>My</item>
<item>Vs</item>
<item>Ea</item>
<item>Ms</item>
</string-array>

Android translate navigation drawer item

I have a navigation drawer menu with diferent items in a listview. This item´s names are in arrays.xml like this one in values-es and other one in values:
<string-array name="nav_drawer_items">
<item>One</item>
<item>Two</item>
<item>Three</item>
<item>Four</item>
</string-array>
I have done a method to translate the app but this items not change when a different language is selected. The others textview work perfect. What I am doing wrong? it is possible to translate an array item?
Try this, but do not forget initially define variables.
<string-array name="nav_drawer_items">
<item>#string/one</item>
<item>#string/two</item>
<item>#string/three</item>
</string-array>

Android Spinner Option Value Just like Select Tag

Is it Possible I can create a spinner with options and values.
<select name=test>
<option value="1">Baran</option>
<option value="2">Khan</option>
</select>
with the spinner XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
</resources>
How can I accomplish such target. As I need to pass Ids to the Server.
You have to manage two list and both are dynamic as you want.
Step to achieve :
Create two ArrayList<String>.Depend on your data type here i make as String Array.
Add value to ArrayList.
Create custom adapter and pass two list adapter in that and get value according that.
Add list to Spinner Adapter. Get the index or position of
the Spinner.
Follow same index to get value from second list value.
Send that value to server.
Task over
See Demo Example that will Guide you to make easy.
Enjoy !!!
Not the best but one approach would be to create another string array with the ids:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
<string-array name="testIDS">
<item>1</item>
<item>2</item>
</string-array>
</resources>
Now when item i is selected from array test you can get the id from item i in array testIDS.

Categories

Resources