Farooq Khan Farooq Khan - 4 months ago 16
Android Question

How to add styling to the android spinner

Here is my code for spinner_style.xml




<item><layer-list>
<item><shape>
<gradient android:angle="90" android:endColor="#B3BBCC" android:startColor="#E8EBEF" android:type="linear" />

<stroke android:width="1dp" android:color="#000000" />

<corners android:radius="4dp" />

<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape></item>
<item ><bitmap android:gravity="bottom|right" android:src="@drawable/spinner_arrow" />
</item>
</layer-list></item>




style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/spinner_style</item>
<item name="android:layout_margin">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>




And in my activity layout I am using spinner as:

<Spinner
android:id="@+id/spinner"
style="@style/spinner_style" />


But I am not getting the required design. Here is the layout image for the given code. The spinner is covering the whole screen instead of height as wrap_ content:

layout image

Answer

I need to add change

<item name="android:layout_height">wrap_content</item>

to

<item name="android:layout_height">30dp</item>

in style.xml. Though I still don't know why wrap content is not working here.