16

How do I specify for a spinner in Android an extra padding on top/bottom of the first/last item? See sample of Goolge below (8 dp spacing). Google Simple Menu

multiholle
  • 3,050
  • 8
  • 41
  • 60
  • Try these links [1st link](http://stackoverflow.com/questions/17407626/custom-layout-for-spinner-item) [2nd link](http://stackoverflow.com/questions/8946893/android-custom-spinner-layout) [Sample Program](http://mrbool.com/how-to-customize-spinner-in-android/28286) Hope this Help full –  Nov 10 '15 at 07:14

6 Answers6

4

You need to create a custom adapter for you Spinner using your custom layout. Thus, you just need to provide a layout with the proper margin/paddins.

Spinner spinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.data, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);

You can find a valid example here.

Community
  • 1
  • 1
bonnyz
  • 13,458
  • 5
  • 46
  • 70
4

Spinner does not support multiple view types.Issue.

I suggested you using dialog for showing your list.

Edited

according to your comment resolved your problem with this question

Community
  • 1
  • 1
Mohammad Hossein Gerami
  • 1,360
  • 1
  • 10
  • 26
  • The only solution seems to be this one. I found it following your link. http://stackoverflow.com/a/16400899/519790 – multiholle Nov 05 '15 at 12:57
1

Just create a separate layout for the items you want to display in the spinner.

Something like this

spinner_text_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="@string/sample_list_item"
android:textColor="@android:color/black"
android:textSize="20sp" />

Then in your activity/fragment, you can create an adapter (an array adapter to be simple in this case) and pass this layout to that adapter reference. Finally set that adapter to the spinner and you can get the desired result.

Sample code.

private void setupSpinner() {
    String[] sampleSpinnerItems = {"One", "Two", "Three", "Four", "Five"};
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            R.layout.spinner_text_view, sampleSpinnerItems);
    sampleSpinner.setAdapter(spinnerAdapter);
}    

Hope this helps.

Note : spinner_text_view is the layout file you have created earlier.

Itapu Vinay
  • 687
  • 2
  • 9
  • 18
1

You need custom adapter for this and custom layouts . You can use 2 layouts . Supposing you have n elements to show , in your spinner you ll have n+2. iF the position is 0 (the first element) or the last element you will show the empty layout otherwise you will show the other layout . The empty layout you will use for creating this space

Raluca Lucaci
  • 2,058
  • 3
  • 20
  • 37
1

You can accomplish this using custom Adapter for Spinner. It will look like this:

class YourAdapter extends BaseArrayAdapter<YourObject> {

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.your_item, parent, false);
            DropDownViewHolder holder = new DropDownViewHolder();
            holder.root = convertView.findViewById(R.id.drop_down_root);
            // other assignments etc.
            convertView.setTag(holder);
        }
        DropDownViewHolder holder = (DropDownViewHolder) convertView.getTag();
        int bottomPadding = 0;
        int topPadding = 0;
        if (position == 0) {
            topPadding = getContext().getResources().getDimensionPixelSize(R.dimen.margin_8);
        } else if (position == (getCount() - 1)) {
            bottomPadding = getContext().getResources().getDimensionPixelSize(R.dimen.margin_8);
        }

        holder.root.setPadding(0, topPadding, 0, bottomPadding);
        // other UI related logic etc.
        return convertView;
    }

    // ...

    public static class DropDownViewHolder {
        View root;
        // other views
    }
}

Actually logic with setting extra padding can be changed to changing some stub view visibility to VISIBLE or inflating some other layout depending on position, but the solution with padding seems more natural to me.

Nikita Barishok
  • 1,312
  • 1
  • 12
  • 15
0

You don't actually need a custom adapter for this. The following solution works perfectly.

Create a drawable bg_spinner.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="3dp" />
    <padding
        android:bottom="8dp"
        android:top="8dp" />
    <solid android:color="yourPopupColor" />
</shape>

Then set android:popupBackground="@drawable/bg_spinner_popup" property for your Spinner in XML.

This works like a charm without anything being cut off etc.