I am creating a spinner. The spinner shows the first row value as the default text.
I want the Spinner's text to be blank initially.
I could add a new empty row with list.add(" "); but I think that this approach looks ugly.
list.add("");//this make my ui ugly but with out this i can't make my spinner blank in starting.
list.add("1");//if i remove add("").then spinner take add("1") this should not happen
list.add("2");
How do I create a Spinner that initially doesn't display any text?
Update:
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("select");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text,ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
Write this code in oncreate method:
Spinner gender = (Spinner) findViewById(R.id.gender);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.gender_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender.setAdapter(adapter);
gender.setSelection(0);
and copy the below code and paste in string.xml
String.xml
<string-array name="gender_array">
<item> </item>
<item>Male</item>
<item>Female</item>
</string-array>
it will work properly.
You should include extra entry in adapter that represents "Select" or something , and make it the initial selected item in the Spinner.
OR may be you will have to create custom spinner or something which i am not sure of.
Take image look like dropdown and put them in background of textview.
Textview gender=(Textview)findViewById(R.id.gender)
gender.setText("");
gender.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder conductor = new AlertDialog.Builder(
Calculator.this);
conductor.setTitle("Select Gender");
int resId = getResources().getIdentifier("gender_array",
"array", getPackageName());
conductor.setItems(resId,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int index) {
int resId1 = getResources().getIdentifier(
"gender_array", "array",
getPackageName());
gender.setText(getResources()
.getStringArray(resId1)[index]);
}
});
AlertDialog alert = conductor.create();
alert.show();
}
});
String.xml
<string-array name="gender_array">
<item>Male</item>
<item>Female</item>
</string-array>
Related
I found and tried this code from developer.android.com to create a spinner populated from a string-array resource :
ArrayAdapter<CharSequence> produits_adapter = ArrayAdapter.createFromResource(this, R.array.produits_array, android.R.layout.simple_spinner_item);
produits_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I don't like the design of the dropdown list after clicking the arrow of the spinner :
I want a dropdown list like standard listbox's dropdown list which is also situated below the spinner. So what is the best layout for that instead of android.R.layout.simple_spinner_dropdown_item ?
You may implement your own structure easily with TextView and ListView
You can use AlertDialog.Builderfor showing list.
AlertDialog dialog;
CharSequence[] items = {"A", "B", "C"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("List")
.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if(item==0){
}
else if(item==1){
}
else if(item==2){
}
}
});
dialog=builder.create();
dialog.show();
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);
For spinner how to set tittle ,i tried with 'prompt' in both xml and activity file ,it is showing as tittle for dropdown list after clicking on spinner but i want to give tittle for spinner how to do it,help me.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setPrompt(getResources().getString(R.string.hello_world));
Try this :
spinner.setPrompt("Select Things");
I ended up using a Button instead. While a Button is not a Spinner, the behavior is easy to customize.
First create the Adapter as usual:
String[] items = new String[] {"One", "Two", "Three"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, items);
Note that I am using the simple_spinner_dropdown_item as the layout id. This will help create a better look when creating the alert dialog.
In the onClick handler for my Button I have:
public void onClick(View w) {
new AlertDialog.Builder(this)
.setTitle("the prompt")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO: user specific action
dialog.dismiss();
}
}).create().show();
}
And that's it!
use that
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/gender"
android:prompt="#string/Gender_prompt1"
/>
and string.xml
<string name="Gender_prompt1">Choose a Gender</string>
I want a Button pull up a menu like a Spinner but it doesn't need to store data like the prompt in a Spinner.
A Spinner looks like this:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.social_list, android.R.layout.simple_gallery_item);//select_dialog_multichoice);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
I need to work same thing as a Button... Thanks
i think you should put spinner.performClick(); on button click method
You can make use ofContextMenu.
Here is a link about Context Menu Demo.
http://mobile.dzone.com/news/context-menu-android-tutorial
But little modifications are required. Inside the button click event, you have to open the ContextMenu.
just open the dialog with list on button click will be looks same as spinner .....
as in
http://saga-androidapplication.blogspot.in/2011/05/dialog-list-item.html
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
use Resources res = getResources();
final String[] items = res.getStringArray(R.array.social_list);
//final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
Android Custom List Dialog
I have a spinner, that uses an array from strings.xml
if the array has 5 strings (1,2,3,4,5), and i want the spinner to show second
string (2) as default value, is this possible?
I know i can re-arrange the strings order so that first one is 2,
but this doesn't look very good if spinner dialog appears as (2,1,3,4,5).
Or does the array have to be created within my activity programatically
and then use setPostion()?
I have tried this, but get a fault when creating array in activity.
Can anyone please give me an example of how to create array and use
it in spinner()
I have also searched on here for answers but cant seem to find what i need.
Thanks for looking....
I recommend you check: http://developer.android.com/resources/tutorials/views/hello-spinner.html
You should create an ArrayAdapter in your Activity.
From the above link:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
You can use
spinner.setSelection(adapter.getPosition(value)));
to set the position.
Cheers for reply.....
Didn't do what i needed, but have managed to solve my problem as follows..
First i created an array within my activity, instead of strings.xml.
String[] NoCore_Array = new String [5];
{
NoCore_Array[0] = "1";
NoCore_Array[1] = "2";
NoCore_Array[2] = "3";
NoCore_Array[3] = "4";
NoCore_Array[4] = "5";
}
Then i created the spinner using...
Spinner spnNoCore = (Spinner) findViewById(R.id.spinner_nocore);
Then created the adapter, using above array....
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, NoCore_Array);
spnNoCore.setAdapter(NoCoreAdapter);
Then set default position of the adapter as follows...
//Set Default Selection
spnNoCore.setSelection(1);
Then rest of spinner code for actions...
spnNoCore.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Get item from spinner and store in string conductorSize........
NoCore = parent.getItemAtPosition(pos).toString();
if (NoCore.equals(NoCore1)) { CoreNo = 1 ; }
if (NoCore.equals(NoCore2)) { CoreNo = 2 ; }
if (NoCore.equals(NoCore3)) { CoreNo = 3 ; }
if (NoCore.equals(NoCore4)) { CoreNo = 4 ; }
if (NoCore.equals(NoCore5)) { CoreNo = 5 ; }
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
Hope this may help other people who are having same problem with
setting default selection on Spinner.
However the layout of the dialog is not as good when done this way,
Radio buttons are missing, just looks like a text selection
with lines dividing the sections.