Android spinner remove dropdown vertical extra space / padding

I am new to Android app development since I created spinner. I noticed extra space / padding vertically for the counter drop-down list at the beginning and end of the drop-down list.

MainActivity.java:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner sr = (Spinner) findViewById(R.id.spinner); String[] days = getResources().getStringArray(R.array.days); ArrayAdapter<String> ar = new ArrayAdapter<>(this, R.layout.single_row, days); sr.setAdapter(ar); } } 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#666666" tools:context="com.xxxxx.defaultspinner.MainActivity"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:spinnerMode="dialog" android:background="#898989"> </Spinner> </RelativeLayout> 

single_row.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="#FFFFFF" android:textSize="26sp" android:background="#214161"> </TextView> 

enter image description here

enter image description here

I set the background of all kinds to different colors so that I can identify the source of the extra space / addition. But the extra space / addition has a white background that does not have any kind.

The note is not due to the spinnerMode = " parameter. This behavior also occurs when spinnerMode =" dropdown " . How can I remove this space? Or am I doing something wrong?

+8
java android xml
source share
2 answers

try adding this to your TextView

 android:includeFontPadding="false" 

it will remove TextView extra top and bottom padding

0
source share

You just need to override the getDropDownView method in the adapter.

 @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { parent.setPadding(0, 0, 0, 0); return convertView; } 
0
source share

All Articles