Search code examples
androidspinnerandroid-relativelayoutandroid-spinner

Customize the spinner in Android is giving space issue


I have customize the background of the spinner. I have removed the background and showing a spinner down arrow beside that. But if the text are not properly aligned. The bigger length text is showing in complete area but for smaller text, there is a gap coming for text and arrow. I have used the below code:

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txtSelectVehins"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="30dp"
                android:text="Select Vehicle"
                android:textColor="#F8F1F1"
                android:textStyle="bold" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:layout_toRightOf="@id/txtSelectVehins" >

                <Spinner
                    android:id="@+id/sp_veh_insrenew"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:background="@null"
                    android:drawSelectorOnTop="true"
                    android:minHeight="0dp"
                    android:spinnerMode="dropdown"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@drawable/dropdownarrow" />
            </RelativeLayout>
        </RelativeLayout>

The whole alignment i want to put rightly aligned. I have attached the screen shot here:

enter image description here

Please help me to fix this.

Here is my adapter to the spinner:

 editStateAdapter = new ArrayAdapter<String>(EditStateCity.this, R.layout.spinner_item, Constants.stateList);
                  editStateAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
                  mSpEditState.setAdapter(editStateAdapter);

And here is the custom layout for the spinner text:

    <?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:textColor="#FFFFFF" 
    android:textStyle="bold"  
    android:paddingRight="20dip" 
    />

Thanks.


Solution

  • The views in the spinner are obtained from the adapter, so if you want to change anything on it, including the text alignment, you will need to edit the layout used as a dropdown resource.

    So take your R.layout.spinner_item and R.layout.simple_spinner_dropdown_item resources and update them so the TextView they contain has its text aligned to the right.

    <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textSize="13sp"
        android:textColor="#FFFFFF" 
        android:textStyle="bold"  
        android:paddingRight="20dip"
        android:gravity="right" />