android – Removing margin from spinner dropdown-ThrowExceptions

Exception or error:

I have a spinner like the image below;

enter image description here

How can I remove the gap on the left x between the spinner and its dropdown – preferably a solution that works on API8 as I am trying to keep my app requirements as low as possible.

I had assumed this would be layout_margin in the spinners style, but after reading this question it seems thats not possible.

In my theme I have;

<style name="AppTheme" parent="AppBaseTheme">        
        <item name="android:dropDownListViewStyle">@style/DropDownStyle</item>
    <item name="android:dropDownSelector">@style/DropDownStyle</item>                
</style>


<style name="DropDownTopStyle">

   <item name="android:clickable">true</item>
   <item name="android:background">@drawable/dropdowntop</item>

</style>

<style name="DropDownStyle">

   <item name="android:layout_width">fill_parent</item>
   <item name="android:layout_height">fill_parent</item> 
   <item name="android:layout_marginLeft">0dp</item> 
   <item name="android:layout_margin">0dp</item> 

   <item name="android:clickable">true</item>
   <item name="android:background">@drawable/dropdownback</item>
   <item name="android:popupBackground">@drawable/dropdownback</item>
   <item name="android:cacheColorHint">#FFF000</item>    
  </style>

Thanks,
Thomas

Additional;
I can see there might be a way to make a popup myself in code – if this is necessary, can I somehow get to the adapters popup view? (that is the list that it displays). Trying to recreate the whole adapters behavior from scratch seems a terrible way to go – but if I can get to that view and disable the normal popup behavior, then I could make my own popup without the annoying offset.

How to solve:

Seems you need to extend the Spinner View to make your own AlertDialog that shows the list of items, or in newer version (API 16+) you can use

android:dropDownHorizontalOffset="-8dp"

check the full details here: How to change the position of opened spinner?

###

Is what you want something like this?

enter image description here

If so, here is the code (works until API level 9) :

Activity:

public class MainActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spinnerListOptions = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapterListOptions = ArrayAdapter.createFromResource(this, R.array.string_array,
            R.layout.spinner_item_top);
        adapterListOptions.setDropDownViewResource(R.layout.spinner_item_dropdown);
        spinnerListOptions.setAdapter(adapterListOptions);
    }
}

spinner_item_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="29dp"
      android:textSize="12sp"
      android:gravity="center">
</TextView>

spinner_item_dropdown.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="29dp"
      android:textSize="12sp"
      android:gravity="center">
</TextView>

activity_main.xml:

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

   <Spinner
           android:id="@+id/spinner"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"/>

</RelativeLayout>

strings.xml:

<resources>
<string-array name="string_array">
    <item>item 1</item>
    <item>item 2</item>
    <item>item 3</item>
</string-array>
</resources>

###

Try this, This should work

    <style name="DropDownStyle">

   <item name="android:layout_width">0dp</item>

###

I dont know it is possible, because all view item like Spinner, EditText, etc... are small images in the android SDK. You should check, is there any default padding in the 9patched SDK image, if yes, I think you cant do it.
Also, don`t forget to check the base Spinner class in Android sourcode, maybe there are some trick.

Leave a Reply

Your email address will not be published. Required fields are marked *