Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android spinner remove dropdown vertical extra space / padding

Tags:

java

android

xml

I am new to android app developing as i was creating spinner i noticed a extra space / padding vertically to drop down list of the spinner at the start and end of the drop down.

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 background of all the views to different color so that i can identify the source of the extra space / padding. But the the extra space / padding has white background which none of the view has.

Note this is not because of the spinnerMode="dialog" option. this behavior is also happens when spinnerMode="dropdown". How can i remove this space ? or i am doing something wrong ?

like image 808
HeiseN Avatar asked Jan 22 '17 10:01

HeiseN


1 Answers

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;
}
like image 102
edwin Avatar answered Oct 22 '22 22:10

edwin