1

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());
Sam
  • 86,580
  • 20
  • 181
  • 179
H.S
  • 159
  • 1
  • 5
  • 12
  • What do you mean by your ui gets ugly when you put space in the first option? – Christine Jun 14 '12 at 05:48
  • means that it create a new row with empty data ..when i open the pop up .it not looks good.1- empty row 2- data row.but with out create this row i cn't make spinner blank(couse spinner takes first value by default) – H.S Jun 14 '12 at 05:52
  • A standard spinner works that way, there's not much you can do about it. You could create your own spinner, either from scratch, or by taking the source of Spinner and modify it. – Christine Jun 14 '12 at 05:54
  • if you have any option to remove that additional row but spinner should not take data row by default .it should show blank .than guide me – H.S Jun 14 '12 at 05:56
  • possible duplicate of [spinner with no select option](http://stackoverflow.com/questions/11013434/spinner-with-no-select-option) – Barak Jun 14 '12 at 06:35
  • Did you try any of the ideas I gave you when you asked this question this morning? – Barak Jun 14 '12 at 06:39
  • could not success to implement that..guide me how to implement that – H.S Jun 14 '12 at 06:42
  • Why did you ask the same question twice? Barak is right, the linked question is exactly the same – banzai86 Jun 14 '12 at 06:53
  • Try [this](http://www.mkyong.com/android/android-spinner-drop-down-list-example/) tutorial may help..I had a same problem.. but as Christina said that how a spinner works, she is right – user1430923 Jun 14 '12 at 06:03

3 Answers3

1

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.

Pramod
  • 172
  • 3
  • 10
  • Not working its working like my code...it also contain empty row .on selection that row ,it selected also – H.S Jun 14 '12 at 08:41
0

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.

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • these all the things i have done ..but no success found.................k can you guide that if i use "select" then i will also appear in list and also it will be selectable .....tell me how can i make this "select" ..unfocusable or unselectable(means when i click on "select" it should nutrul ) – H.S Jun 14 '12 at 06:03
0

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>
Pramod
  • 172
  • 3
  • 10