Sudhansu
Sudhansu

Reputation: 870

How to set spinner window position on below of a spinner

Is there any method to set default spinner position , I just want to show my spinner window below the spinner title but normally when we click on spinner ,the window popup on above the title ,so is there any way to show the popup below the title.

Upvotes: 1

Views: 8197

Answers (4)

Yasser AKBBACH
Yasser AKBBACH

Reputation: 1028

Here's Rami El-bouhi's code to the Kotlin way:

spinner.post {
  dropDownVerticalOffset += height
}

Upvotes: 0

Ankur Bavishi
Ankur Bavishi

Reputation: 963

For this spinner, you may use like this

android:dropDownVerticalOffset="40dp"

 <android.support.v7.widget.AppCompatSpinner
      android:spinnerMode="dropdown"
      android:dropDownVerticalOffset="40dp"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"/>

Now Drop down will show as per your requirement.

Upvotes: 10

Rami El-bouhi
Rami El-bouhi

Reputation: 441

I preferable use this approach and not dealing with ViewTreeObserver

mSortingSpinner.post(new Runnable() {
            @Override
            public void run() {
                mSortingSpinner.setDropDownVerticalOffset(mSortingSpinner.getDropDownVerticalOffset() + mSortingSpinner.getHeight());
            }
        });

Upvotes: 3

Petr Daňa
Petr Daňa

Reputation: 1293

For dropdown Spinner mode you can use this:

mSortingSpinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mSortingSpinner.setDropDownVerticalOffset(
                mSortingSpinner.getDropDownVerticalOffset() + mSortingSpinner.getHeight());
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            mSortingSpinner.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            mSortingSpinner.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
});

This set vertical offset of dropdown by spinner height.

Upvotes: 1

Related Questions