19

i tried a lot to change the dropdown item height of spinner.. but i couldn't get a good solution.. plz help me guys..

here is a code loginactivityview.xml

       <Spinner
         android:id="@+id/spinnerFacility"
         android:layout_width="400dip"
         android:layout_height="50dip"
         android:layout_alignLeft="@+id/lpassword"
         android:layout_below="@+id/lpassword"
         android:layout_marginTop="32dip"            
         android:background="@drawable/location" 
         android:paddingLeft="10dip"               
         android:dropDownWidth="@style/dropDown"  
         android:minHeight="40dip"     
         android:typeface="monospace" />

loginrowspinner.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

  <TextView
    android:id="@+id/textViewRowFacility"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="left"
    android:paddingBottom="5dip"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:paddingTop="5dip"
    android:text="Facility"
    android:textColor="#000000"
    android:textSize="20dip" >
</TextView>

enter image description here

how to change the height of drop down any idea..

Rao's
  • 1,087
  • 8
  • 24
  • 45
  • Check following URL for the exact solution: http://stackoverflow.com/questions/8878260/how-to-re-size-the-height-of-the-items-in-spinner-with-multiple-choice – JIT1986 Oct 13 '12 at 06:09
  • you should mark John's post as answer. – M D P Jul 31 '14 at 13:25

11 Answers11

62

Just add this to your adapter.

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
John
  • 941
  • 8
  • 17
  • ...so clever solution :) – Davidm176 Apr 12 '17 at 17:11
  • 2
    simple_spinner_dropdown_item is the default android layout and should not be edited. Btw. changing anything inside has no effect for my spinners. Better to add android:minHeight="80dp" to your Spinner section in layout xml – Darksymphony Nov 25 '18 at 19:26
  • if you use this, you cant edit your layout... and if you give your layout id it also not work... this not solve error – Ucdemir Aug 31 '21 at 18:25
8

Maybe it can help..

ArrayAdapter<String> yourSpinnerAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_item, yourItem) {

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            convertView = super.getDropDownView(position, convertView,
                    parent);

            convertView.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams p = convertView.getLayoutParams();
            p.height = 100; // set the height
            convertView.setLayoutParams(p);

            return convertView;
        }
    };
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
dimasgung
  • 81
  • 1
  • 1
7

In loginrowspinner.xml, add android:minHeight="48dp" to TextView element.

 <TextView 
     ...
     android:id="@+id/textViewRowFacility"
     android:minHeight="48dp" />
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Jack W
  • 2,637
  • 1
  • 14
  • 7
4

Create your own textview in layout folder like this which will be populated in dropdown popup

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="18sp"
android:textAlignment="inherit" />

Note I have provided

android:layout_height="60dp"

and

android:paddingLeft="20dp"

and use this for your spinner, like this

Spinner dropdown = (Spinner)findViewById(R.id.sosMode);
String[] items = new String[]{"Date", "Travelling alone"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_dropdown_list, items);
dropdown.setAdapter(adapter);

this works for me :)

2

You can try using dps.

@Override   
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    TextView textview = (TextView)inflater.inflate(android.R.layout.simple_spinner_item, null);

    textview.setText(alOpcion.get(position).getOpcionName());

    DisplayMetrics metrics = parent.getResources().getDisplayMetrics();
    float dp = 40f;
    float fpixels = metrics.density * dp;
    int pixels = (int) (fpixels + 0.5f);

    textview.setHeight(pixels);

    return textview;
}
miniBill
  • 1,743
  • 17
  • 41
Luis Colomé
  • 101
  • 2
0

Have you tried something like android:layout_height="30dp" instead of fill_parent for either of those elements in loginrowspinner.xml?

es0329
  • 1,366
  • 1
  • 18
  • 35
0

add this in your adapter : convertView.setMinimumHeight(60); which works for me.

0

//This might also help:

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataForAdapter) {


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            v.setMinimumHeight((int) (40*cx.getResources().getDisplayMetrics().density));
            v.setBackgroundColor(Color.rgb(222, 222, 222));


            return v;
        }

    };
Marie Amida
  • 556
  • 1
  • 5
  • 14
  • Could you please explain '`40*cx.getResources().getDisplayMetrics().density`' ? – backslashN Sep 19 '17 at 07:30
  • @backslashN, when writing Java code for Android, you can't use dp units and have to specify in pixels instead (unlike XML, which accepts dp also). If you set 40px for all cases, the height will still look small on screens with a lot of pixels. That's why we use this code to take the current device density into account – kit Feb 15 '18 at 10:41
0

Override method in ArrayAdapter class

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View view = super.getDropDownView(position, convertView, parent);
    view.getLayoutParams().height = 50;
    return view;
}
Amit Yadav
  • 32,664
  • 6
  • 42
  • 57
  • It crash java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView" – Ucdemir Aug 31 '21 at 18:15
0

Got stuck in same problem.Found following solution to be working for me:

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        setSpinnerDropDownHeight()
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mBinding?.included?.spinn?.getViewTreeObserver()?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
                @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
                override fun onGlobalLayout() {
                    mBinding?.included?.spinn?.dropDownVerticalOffset = mBinding?.included?.spinn?.getDropDownVerticalOffset()!! + mBinding?.included?.spinn?.getHeight()!!
                    mBinding?.included?.spinn?.viewTreeObserver!!.removeOnGlobalLayoutListener(this)
                }
            })

            mBinding?.included?.spinn?.dropDownVerticalOffset = 10
        }
    }

Hope it helps someone!!!!

Thanks

Anu Bhalla
  • 422
  • 4
  • 11
0

As people say, best way is to add

    android:minHeight="48dp"

to .xml. But if you want to do it programatically use:

    mspinner.setMinimumHeight(48);

being mspinner the name of your spinner and 48 the new height of the option.

Aike
  • 1