wasp256
wasp256

Reputation: 6242

android java set fix default value for spinner

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?

Upvotes: 2

Views: 3985

Answers (1)

Aditya Rai
Aditya Rai

Reputation: 139

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

Upvotes: 2

Related Questions