-1

When I load data to the spinner from web service using Retrofit, It increase the height of the spinner and change text color. What is the problem? How can I fix it?

Red marked spinner is the standard size without loading any data.

enter image description here

Layout:

    <?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_monthly_target_ad"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nitolniloygroup.operating.view.activity.MonthlyTargetADActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@android:drawable/btn_dropdown"
            android:textColor="@android:color/holo_blue_bright"
            android:id="@+id/spinnerZone" />


        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@android:drawable/btn_dropdown"
            android:textColor="@android:color/holo_blue_bright"
            android:id="@+id/spinnerSubZone" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@android:drawable/btn_dropdown"
            android:textColor="@android:color/holo_blue_bright"
            android:id="@+id/spinnerBranch" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@android:drawable/btn_dropdown"
            android:textColor="@android:color/holo_blue_bright"
            android:id="@+id/spinnerFieldOfficer" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@android:drawable/btn_dropdown"
            android:textColor="@android:color/holo_blue_bright"
            android:id="@+id/spinnerFieldOfficeraaa" />

        <Button
            android:text="Search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/btn_default"
            android:onClick="onClickSearch"
            android:id="@+id/button4" />




    </LinearLayout>

</RelativeLayout>
durlove roy
  • 229
  • 4
  • 12

3 Answers3

2

because you make spinner height warp_content. to avoid that give spinner a specific height like 80dp

Ramzy Hassan
  • 926
  • 8
  • 21
  • Thanks. But I cant change the color, gravity and size. Whats the problem? It is not working. android:textColor="#000000" android:textSize="30sp" android:gravity="center" – durlove roy Feb 19 '17 at 10:22
  • @durloveroy you can implement your custom spinner adapter and also specify your spinner items as text view which allows you to specify item gravity and item text color and text size – Ramzy Hassan Feb 19 '17 at 10:31
  • see this tutorial @durloveroy http://abhiandroid.com/ui/custom-spinner-examples.html – Ramzy Hassan Feb 19 '17 at 10:36
1

You can set minimum height of Spinner:

android:minHeight="80dp"
aslamhossin
  • 1,217
  • 12
  • 22
  • Thanks. But I cant change the color, gravity and size. Whats the problem? It is not working. android:textColor="#000000" android:textSize="30sp" android:gravity="center" – durlove roy Feb 19 '17 at 10:21
  • If you want to use desire color then you have to implement your own Spinner Adapter. Then you can change style. – aslamhossin Feb 19 '17 at 10:24
0

To change spinner size: use padding to 0dp

 <Spinner
    android:id="@+id/spinnerSubZone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@android:drawable/btn_dropdown"
    android:minHeight="50dp"
    android:padding="0dp"  //add this
    android:textColor="@android:color/holo_blue_bright" />

To change text color: overwrite setOnItemSelectedListener method in your spinner object

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.RED); //change color
            ((TextView) parent.getChildAt(0)).setTextSize(10);  //change size
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

or u can use a custom adapter class see here

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62