Removing margin from spinner dropdown
Asked Answered
M

4

20

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.

Marylnmarylou answered 22/5, 2014 at 21:52 Comment(4)
Did you even come up with a solution?Zamudio
@AnubianNoob Please post your layout to paste bin.Vapory
It's a pretty general question. I have a custom spinner with a drop down in a floating window attached to a service. Is my layout really important?Zamudio
@AnubianNoob it's not a general question because I can't seem to re-create the problem.Vapory
H
3

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?

Higgle answered 11/6, 2015 at 16:11 Comment(0)
B
2

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>
Binghi answered 13/10, 2015 at 6:56 Comment(0)
T
0

Try this, This should work

    <style name="DropDownStyle">

   <item name="android:layout_width">0dp</item>
Tropine answered 17/6, 2015 at 13:56 Comment(0)
I
-3

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.

Infralapsarian answered 17/6, 2015 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.