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>
Related
I am running into some trouble with my spinners. When every I select a value from them the spinner only takes the first value in the list.
For example I have a spinner with vales 1,2,3,4,5. When I select 4 the value taken from the spinner 1, this is the same if I select any of the other values.
Am I implementing the Spinner wrong? Or taking the values from the spinner wrong?
Spinner
<Spinner
android:id="#+id/heatSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/heat_title"
android:entries="#array/heat"/>
Array/Heat
<string-array name="heat">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
Taking Values from Spinner
Spinner heat = (Spinner) dialog.findViewById(R.id.heatSpinner);
final String heatValue = heat.getSelectedItem().toString();
final int finalHeat = Integer.parseInt(heatValue);
I had a similar problem, I dunno if this way is the best way to do it but it worked for me.
You need to set a setOnItemSelectedListener for each spinner, inside this you will be then able to set the value of your variable to the items of the spinner.
So your spinner might look something like this, again this may not be the nicest looking approach but it worked for me anyway, hope it helps
final Spinner heat = (Spinner) dialog.findViewById(R.id.heatSpinner);
heat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
final String heatValue = heat.getSelectedItem().toString();
int finalHeat = Integer.parseInt(heatValue);
db1.updateHeat(finalHeat, recipe_number);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Let's look at the 4ยบ block of code in your question.
Are you sure that
int servingSize = servings.getInt(0);
Is getting the value that you want?
And in
seekBar.setProgress(size);
what is this 'size' parameter? Are you sure that you are passing the right value to the setProgress() method? I didn't see this 'size' variable in your code.
I have been going through spinners for my application but I don't know where to declare the items that are to be shown in the spinner i.e either I declare them in the string.xml or in my main_activity like this ..
String[] data = { "Hello", "There", "I", "Am", "Taking", "Values" };
Which method is best to use and why?
After declaring values in resources tag in strings.xml like below:
<string-array name="spinner_values">
<item>a</item>
<item>b</item>
</string-array>
you can use entries attribute in spinner tag in your xml layout, instead of use it pro-grammatically in java file. Like below:
<Spinner
android:id="#+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_values" />
Declaring the array in string.xml is better because:
If you want to translate your app into another language, you just
need this one file which has all the arrays and strings that your app uses.
If you have to make any changes to any string/array, you know by default where to look for(All at one place).
Re-use is possible using this way. In any other activity you can just use from there, instead of declaring again.
you can try in this way ....
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Small");
list.add("Medium");
list.add("Large");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
apply = (Button) findViewById(R.id.apply);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(String.valueOf(spinner1.getSelectedItem()).equals("Small"))
{
// your code
}
if(String.valueOf(spinner1.getSelectedItem()).equals("Medium"))
{
// your code
}
else if(String.valueOf(spinner1.getSelectedItem()).equals("Large"))
{
// your code
}
}
});
If content of your spinner is fixed then declare it in String.xml . its best practice
You should declare array in strings.xml
<string-array name="data">
<item>Hello</item>
<item>There</item>
<item>I am</item>
<item>Taking</item>
</string-array>
and obtain values in activity
String [] array =getResources().getStringArray(R.array.data);
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 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"
/>
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];