2

I would like to have a fix value that is always displayed in the Spinner and when clicking on the Spinner this value should not be listed in the drop down selection. Until now I have the following

   Definition XML:
   <Spinner
        android:layout_width="76dp"
        android:layout_height="40dp"
        android:id="@+id/right_shift"
        android:layout_row="0"
        android:layout_column="0"/>

    Java:
    final Spinner right = (Spinner) findViewById(R.id.right_shift)
    ArrayList<String> rightShift = new ArrayList<String>();

    rightShift.add("  >>"); //THIS SHOULD BE THE VALUE THAT IS ALWAYS DISPLAYED

    for (int i=0; i<5; i++)
        ...//add other values to arraylist

    ...//set values of arraylist to spinner

    right.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            right.setSelection(0);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });

But when clicking on the Spinner the preselected Item will again be shown in the drop down and the right.setselection(0) is not executed fast enough so I still see the selected Item for about 0.5sec... Is there an other/easier way to perform this?

wasp256
  • 5,943
  • 12
  • 72
  • 119
  • If you don't want it to be selectable but just show when it is first displayed then you can use [android:hint](http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint) in your xml for the `TextView` that the `Spinner` uses. – codeMagic Aug 20 '13 at 19:30
  • Which TextView? I simply defined the Spinner as above in a GridLayout?? – wasp256 Aug 20 '13 at 19:49
  • I believe your question was already answered [here][1] [1]: http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one – Chris Hayes Aug 20 '13 at 21:43

1 Answers1

2

you can add android:prompt=" >>" in xml and in java set default position in spinner to be -1.

Aditya Rai
  • 139
  • 8
  • 1
    `android:prompt=">>"` gives me the error `Error: Gradle: String types not allowed` and how do I set the default position in java? – wasp256 Aug 20 '13 at 19:38
  • sorry, my fault , add String for prompt in strings.xml file and in java set spinner.setSelected(-1); – Aditya Rai Aug 20 '13 at 20:42