I have a spinner with three items and I use an XML string-array resource to feed it. When you open an activity the spinner normally shows the first item that's in the array list. I'd like to change that and show the text "Select one" in the spinner, before an item is selected.
How can I do that?
You can do that one of two ways.
1) Add "Select One" as the first item in your xml and code your listener to ignore that as a selection.
2) Create a custom adapter to insert it as the first line,
EDIT
In your resources
<string-array name="listarray">
<item>Select One</item>
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</string-array>
In your onItemSelected Listener:
spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0) {
}else {
// Your code to process the selection
}
}
});
To set a default text for the spinner you have to use android:prompt=#string/SelectOne for your spinner Where SelectOne is defined in your string.xml .
Example :
<Spinner android:id="#+id/spinnerTest"
android:layout_marginLeft="50px"
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:layout_marginTop="5dip"
android:prompt="#string/SelectOne"
android:layout_marginRight="30px"
android:layout_height="35px"
/>
Related
I'm making this android app. The idea is that when the user selects a name it display the info as a drop down. For this effect I have used android documentation on spinners. The problem radicates in that it allows the user to select one of the items in the list (after all that is what Spinner does):
Is there a way I can modify the spinner, si it just show the info without the possibility of this being selected by the user?
this is the code of the spinner:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/spinner1"
android:background="#color/Artes"
/>
String Array
<string-array name="names">
<item>Home</item>
<item>Work</item>
<item>Other</item>
<item>HCustom</item>
</string-array>
Spinner Adapter
Spinner arquitecturaNacional = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>
(NacionalInicial.this,android.R.layout.simple_list_item_1,
her getResources().getStringArray(R.array.names));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arquitecturaNacional.setAdapter(myAdapter);
well, you can override the OnItemSelected event of the spinner.
Lets suppose the item you are setting as default selected is the index = 1, you can override this method like the following:
//manage automatic set of the selection
bool isAutomaticSelected = false;
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
//manage the bool
if(isAutomaticSelected){
isAutomaticSelected = false;
}
else{
mySpinner.setSelection(1); //or any index u want
isAutomaticSelected = true;
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
if(isAutomaticSelected){
isAutomaticSelected = false;
}
else{
mySpinner.setSelection(1); //or any index u want
isAutomaticSelected = true;
}
}
});
In this way any time an user select an item from the dropdown view or select anything,
I set the boolean because elseway it would have resulted in an infinite loop.
hope this helps
Okay spinner 1 uses Manu_array
spinner 2 uses Manu 1xsd, Manu 2xrsd or 3x4rsd
all I need for now is to be able to populate the second spinner, after selecting the a value from the Manu_array in spinner 1.
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
My java file code:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// 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
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
I can process code that will allow the spinner to populate with the array data, but I cant get the second spinner to populate based on the selection of the first spinner, surely its simple if equals value use array.
The spinner above shows the values for the Manu_array, the next spinner has to take the value selected and populate with the appropriate array values, but all I have managed to do is generate a second spinner with the array values I have manual typed in, not selected.
if(Manu.equals("Manu 1xsd")){spinner2 languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......
Any suggestions where I may look for examples please looked at a few on here and confused me something chronic.
Set the second spinner adapter when an item from the first spinner is clicked
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch(position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd;
break;
};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
spinner2.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I want to get Selected Value of a spinner.
First, I created a STRING-ARRAY in res/values/string
<string-array name="location">
<item name="AUH">ABU DHABI</item>
<item name="AAN">AL AIN</item>
<item name="DMM">DAMMAM</item>
</string-array>
Spinner Definition in Layout:
<Spinner
android:id="#+id/spnOrigin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/location"/>
Now I need to complete the button click body, if user selects ABU DHABHI, he should return AUH.
GETSELECTITEM returns ABU DHABI, not the value behind this. If I try something like this, can this approach allow me to get NAME attribute?
String[] _location =getResources().getStringArray(R.array.location);
Button Handler:
bttProcess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
});
I don't believe the name attribute is valid on items of a string array.
I think your best bet is to set up another array to have the values in it. You can do ((AdapterView)getViewById(R.id.spnOrigin)).getSelectedItemPosition() then look up the relevant name from that other array.
Try to follow the exemple in the android developer website to get started using spinner
First your activity have to implement AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner.
public class SpinnerActivity extends Activity implements OnItemSelectedListener
Second you need to register for the interface implementation by calling:
spinner.setOnItemSelectedListener(this);
Finally within "onItemSelected" method of that class, you can get the selected item:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
use your string array like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="locations">
<item>ABU DHABI</item>
<item>AL AIN</item>
<item>DAMMAM</item>
</string-array>
</resources>
In this scenario where Spinner need to bind by KEY - VALUE PAIR List,
The spinner can be get values from
1. DATABASE
2. An static String Array.
3. By creating Bi- Resource String Array.
FOR 3rd Point:
If you need to bind spinner by resource String Array, u need to create two array. One which will hold KEY [NAMES Of Countries], Second Will contains VALUES [Short Code of Countries].
Than on Click Button,
bttProcess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int position = spinner.getSelectedItemPosition();
Log.i("Code:","Selected Country Code: "+
getStringFromArray(R.array.locations_code, position ));
});
private String getStringFromArray(int id, int index) {
try {
String[] bases = getResources().getStringArray(id);
return bases[index];
}
catch (Exception e) {
return "";
}
}
RESOURCES would look like,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="locations">
<item>ABU DHABI</item>
<item>AL AIN</item>
<item>DAMMAM</item>
</string-array>
<string-array name="locations_code">
<item>ABU</item>
<item>AL</item>
<item>DAM</item>
</string-array>
</resources>
i am developing one spinner this spinner i am string array
spinner = (Spinner)this.findViewById(R.id.mlg);
final CharSequence[] itemArray =getResources().getTextArray(R.array.RectBeam);
final List<CharSequence> itemList =new ArrayList<CharSequence>(Arrays.asList(itemArray));
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,itemList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
............................
<string-array name="RectBeam">
<item value="3000000" > Steel</item></string-array>
this is the spinner related string array i am get the spinner item i am using parent.getItemAtPosition(pos).toString(),done my problem is particular item value how can get
example : steel----------->3000000
I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.
Create two arrays in your array.xml file like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>Nokia 1200</item>
<item>Nokia 1600</item>
<item>Nokia 5130</item>
</string-array>
<string-array name="values">
<item>1000</item>
<item>2000</item>
<item>5000</item>
</string-array>
</resources>
Now load first array in your adapter and store the second one in other Array to hold values of items as:
String items [] = getResources().getStringArray(R.array.items);
String values [] = getResources().getStringArray(R.array.values);
And you can simply get the respective item name and value in your onItemSelected() method like this:
String item = parent.getItemAtPosition(pos).toString();
String value = values [pos];
Is it possible to populate the options of a Spinner right in the layout xml? This page suggests I should use an ArrayAdapter? It seems awkward not being able to do it..
I'm not sure about this, but give it a shot.
In your strings.xml define:
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
In your layout:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="#array/array_name"
/>
I've heard this doesn't always work on the designer, but it compiles fine.
Define this in your String.xml file and name the array what you want, such as "Weight"
<string-array name="Weight">
<item>Kg</item>
<item>Gram</item>
<item>Tons</item>
</string-array>
and this code in your layout.xml
<Spinner
android:id="#+id/fromspin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/Weight"
/>
In your java file, getActivity is used in fragment; if you write that code in activity, then remove getActivity.
a = (Spinner) findViewById(R.id.fromspin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.weight, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
a.setAdapter(adapter);
a.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (a.getSelectedItem().toString().trim().equals("Kilogram")) {
if (!b.getText().toString().isEmpty()) {
float value1 = Float.parseFloat(b.getText().toString());
float kg = value1;
c.setText(Float.toString(kg));
float gram = value1 * 1000;
d.setText(Float.toString(gram));
float carat = value1 * 5000;
e.setText(Float.toString(carat));
float ton = value1 / 908;
f.setText(Float.toString(ton));
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
// Inflate the layout for this fragment
return v;
}
In regards to the first comment: If you do this you will get an error(in Android Studio). This is in regards to it being out of the Android namespace. If you don't know how to fix this error, check the example out below. Hope this helps!
Example -Before :
<string-array name="roomSize">
<item>Small(0-4)</item>
<item>Medium(4-8)</item>
<item>Large(9+)</item>
</string-array>
Example - After:
<string-array android:name="roomSize">
<item>Small(0-4)</item>
<item>Medium(4-8)</item>
<item>Large(9+)</item>
</string-array>