Search code examples
androidandroid-spinnervisible

Hide spinner in Android and display with the click of a button


I have created a spinner in my app which i want to be invisible when someone press the sos button then it should be visible for the user to select one option in it how can i solve it? this is the result i have created


Solution

  • You can use the below code to hide and show the Spinner

    //hide
    spinner.setVisibility(View.GONE);
    
    //show
    spinner.setVisibility(View.VISIBLE);
    

    Also,you can use the below code snippet to get the item selected by user;

    spinner.setOnItemSelectedListener(this);
    
    ...
    
    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        Toast.LENGTH_SHORT).show();
    }