Android Spinner hint?
I have two spinners in my current and开发者_开发技巧roid app, I would like to have a default value like the editText's android:hint ability. Is there a way to do so, but in a way that doesn't add the hint to the string array that populates the spinner. e.g. it's impossible to choose the hint from the spinner list.
Also, is there a way to check if the user has selected a valid value and if not take an action. I do this in my editTexts by getting the value to a string and then:
if (calculator_ShutterSpeed_TempValue.equals("") ){
action
}
And can I set the spinner back to it's hint value on another button press?
So what I am for asking is:
- Spinner hint that is not possible to be redisplayed after the user has clicked on the spinner?
- Check if the user has selected a valid value from the spinner, (have they selected a value beyond the default hint one)?
- Redisplay the hint on click of a reset button?
To set the default value in Spinner you have to set first that index selected setSelection(index)
and save it to the preferences by using SharedPreferences
. Please check out the code to set and load the preferences.
So onResume()
highlight that index value selected.
To get the value from spinner use spinner.getSelectedItem()
and cast it to String. Now you can compare it to your criteria.
To redisplay the hint, first it must be as an item in spinner and then regiter the OnItemSelectedListener. In that Listener's code reset the selected index to yout hint index again.
I handle this by using a button instead of a Spinner. I have the sample project up on GitHub.
In the project, i'm displaying both the Spinner and button to show that they indeed look identical. Except the button you can set the initial text o whatever you want.
Here's what the activity looks like:
package com.stevebergamini.spinnerbutton;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner spinner1;
Button button1;
AlertDialog ad;
String[] countries;
int selected = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
button1 = (Button) findViewById(R.id.button1);
countries = getResources().getStringArray(R.array.country_names);
// You can also use an adapter for the allert dialog if you'd like
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries);
ad = new AlertDialog.Builder(MainActivity.this).setSingleChoiceItems(countries, selected,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
button1.setText(countries[which]);
selected = which;
ad.dismiss();
}}).setTitle(R.string.select_country).create();
button1.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
ad.getListView().setSelection(selected);
ad.show();
}});
}
}
精彩评论