Sandip Jadhav
Sandip Jadhav

Reputation: 7155

How to hide spinner dropdown android

I want to hide spinner prompt popup on outside click. If prompt popup is open and user press home key activity will minimize so when user again open application that prompt popup should disappear.
Is there any way to achieve this. Thank You

Edit:-- Prompt popup is not customized. So I can't hide them in onPause or onResume methods.

Upvotes: 14

Views: 15607

Answers (4)

Vikas Kumbhar
Vikas Kumbhar

Reputation: 121

spinner.clearFocus();

this is simple line to close spinner programitically

Upvotes: -3

Oliver Jonas
Oliver Jonas

Reputation: 1248

Based on Andro's answer, you may prefer reflection to be able to call the protected method onDetachedFromWindow. Then you don't have to subclass Spinner, adapt the layout, etc.

/**
 * Hides a spinner's drop down.
 */
public static void hideSpinnerDropDown(Spinner spinner) {
    try {
        Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
        method.setAccessible(true);
        method.invoke(spinner);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 23

Andro Selva
Andro Selva

Reputation: 54322

Well its a little complicated than I thought.

I am adding the step by step details here. Try to follow it. I was able to achieve this in api level 10.

And this solution assumes that you are supposed to close the prompt dialog programatically when the user clicks on Home Button or If you had to move to next activity without user interaction

The first step is to create a Custom Spinner by extending Spinner Class. Let's say, I have created a class called CustomSpinner in the package com.bts.sampleapp

My CustomSpinner class looks like this,

package com.bts.sampleapp;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Spinner;

    public class CustomSpinner extends Spinner{
        Context context=null;

        public CustomSpinner(Context context) {
            super(context);
            this.context=context;
        }

        public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public CustomSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        public void onDetachedFromWindow() {
            super.onDetachedFromWindow();
        }
    }

Now in your Xml file, replace Spinner element by this custom spinner,

        <com.bts.sampleapp.CustomSpinner
        android:id="@+id/spin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

The next step is to initialize and set adapter to this spinner in your Activity class,

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CustomSpinner spin=null;
    spin=(CustomSpinner)findViewById(R.id.spin);
    spin.setAdapter(spinnerAdapter); //you can set your adapter here.
}

The final step is to close the dialog when the user clicks on HomeButton or When the Activity moves to background. To do this, we override the onPause() like this,

@Override
    protected void onPause() {
        Log.i("Life Cycle", "onPause");
        spin.onDetachedFromWindow();
        super.onPause();
    }

Now within the onPause() call the method spin.onDetachedFromWindow(); which does the job of closing the prompt dialog for you.

Also calling spin.onDetachedFromWindow(); from anywhere within the Acitivity should close the Spinner prompt dialog if it is open.

Upvotes: 21

boiledwater
boiledwater

Reputation: 10482

  • You can popup Activity as Dialog Theme.
  • Override onPause method.

    protected void onPause (){ super.onPause (); this.finish(); }

Upvotes: -1

Related Questions