Ruby prO
Ruby prO

Reputation: 119

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

Upvotes: 1

Views: 6421

Answers (4)

Dileep Patel
Dileep Patel

Reputation: 1988

@HumanOidRoBo you can do it by this code..

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Optional"
                android:textSize="20sp" />


            <Spinner
                android:id="@+id/mySpinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_marginLeft="5dp">

            </Spinner>
        </LinearLayout>

and in class add this on click event of SOS

sosButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    mySpinner.setVisibility(View.VISIBLE); // for Show 

    // or
    mySpinner.setVisibility(View.GONE);   // for Hide
}  
}); 

Upvotes: 1

Sardar Khan
Sardar Khan

Reputation: 841

//Hide

spinner.setVisibility(View.GONE);

//Show

spinner.setVisibility(View.VISIBLE);

Android: How to make a Spinner invisible and then visible again?

Upvotes: 1

Jaydroid
Jaydroid

Reputation: 334

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();
}

Upvotes: 2

Zarwan
Zarwan

Reputation: 5767

I have created a spinner in my app which i want to be invisible when someone press the sos button

You can set a listener on the button that will set the visibility of the spinner.

Ex.

sosButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mySpinner.setVisibility(View.GONE);
    }
});

it should be visible for the user to select one option in it how can i solve it?

I'm not sure what this means. I thought you wanted the spinner to be invisible?

Upvotes: 3

Related Questions