Prevent spinner dropdown to show
Asked Answered
B

3

11

I am using a spinner whose adapter is dynamically populated.

  • When there are multiple items, the spinner behavior is the standard one. On a click, the dropdown is showed, allowing the user to select an item
  • When there is only one item, I want to prevent the dropdown to appear and catch the click event to perform an action.

I can't find a solution to prevent the default behavior (i.e. showing the dropdown for only one item on a click). Any ideas on how to do this? Thanks

Boatsman answered 21/8, 2012 at 20:4 Comment(0)
L
11

hm... try to use setClickable(fasle) or setEnabled(false) if only one item in spinner.

Try this

public class SpinnerActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        List<String> list = new ArrayList<String>();
        list.add("list 1");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

        if (list.size() < 2) {
            spinner.setClickable(false);
            spinner.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        Toast.makeText(SpinnerActivity.this, "Catch it!", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }
            });
        }


    }
}
Leverrier answered 21/8, 2012 at 20:9 Comment(2)
this definitely solves the first part of the problem (prevent spinner dropdown to show), but since I can't have a click listener on a spinner and since the spinner is disabled or non clickable, I can't catch the click event :(Boatsman
There is a problem with this, in that some versions or vendors show the items on ACTION_DOWN. This will work on some devices but not on others I think.Draughtsman
S
8

To prevent spinner from showing drop-down list, I only return true in onTouch(...), like this:

    spinner.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return true;
        }
    });
Snuggle answered 4/8, 2017 at 8:14 Comment(0)
E
0

in kotlin I did it that way

  <string-array name="coins_array">
    <item>USD</item>
    <item>USDT</item>       
  </string-array>

 
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.main_activity.*


class MainActivity: AppCompatActivity(), AdapterView.OnItemSelectedListener{
    private var touchEvent = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        ArrayAdapter.createFromResource(this, R.array.coins_array, android.R.layout.simple_spinner_item
        ).also { adapter ->
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            coin_choice.adapter = adapter
        }
        coin_choice.onItemSelectedListener = this

    }

    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        if(touchEvent){
            val coin: String = parent.getItemAtPosition(position).toString()
            Toast.makeText(this, coin, Toast.LENGTH_SHORT).show()
        }
        touchEvent = true
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
    }
}
Exactitude answered 13/12, 2020 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.