2

I've followed this tutorial Android Widgets: Custom Spinner in Android and made a custom Spinner that has an image and a text view as items, but for some reason there is no space between the items.

At the moment it looks like that when it's not clicked (which is fine):

Custom spinner not clicked

And when I click it, it looks like that:

Custom Spinner clicked

While the default one has plenty of space between the items:

Default spinner clicked

This is the xml code for the spinner:

<Spinner
    android:id="@+id/languageSpinner"
    style="@android:style/Widget.Holo.Light.Spinner"
    android:layout_width="110dp"
    android:layout_height="40dp"
    android:layout_marginStart="30dp"
    android:prompt="@string/select"
    android:spinnerMode="dropdown"/>

And this is the xml for the custom row layout:

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center_vertical"
        android:textColor="@color/black"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="2dp"/>
</LinearLayout>

Does anybody have an idea what I do wrong?

EDIT: After some of the suggestions, the space between the items is added but then the item that is selected in the spinner cannot be seen properly, I am attaching an image:

Custom spinner with padding 10 on the layout

Apostrofix
  • 2,140
  • 8
  • 44
  • 71

4 Answers4

2

custom row layout:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/language"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_gravity="center_vertical"
            android:textColor="@color/black"
            android:layout_marginStart="10dp"
            android:layout_marginBottom="2dp"/>
    </LinearLayout>
    </LinearLayout>
Bhushan
  • 424
  • 5
  • 15
1

You only need add the attribute lineSpacingExtra in your textview.

Example:

<TextView
        android:id="@+id/language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center_vertical"
        android:textColor="@color/black"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="2dp"
        android:lineSpacingExtra="3dp/>

--> android:lineSpacingExtra="3dp

dpulgarin
  • 556
  • 5
  • 17
0

try this way

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="15dip"/>


    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginBottom="10dip"
        android:layout_marginTop="10dip"
        android:textColor="#000000" />


</LinearLayout>
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

just select this layout simple_list_item_1

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data); //add this layout
spinner_state.setAdapter(adapter);

that's all... enjoy your coding...

Mahendren Mahisha
  • 1,072
  • 11
  • 9