0

I have successfully implemented Spinners in my project using the Spinner class which exists in this link:

How to make an Android Spinner with initial text "Select One"

Also, I customized the layout of each item and named it as spinner_entries_style.xml ..

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="2dp"
        android:background="#640c1c"
        android:maxEms="10"
        android:padding="10dp"
        android:singleLine="false"
        android:textColor="#d7a801"
        android:textSize="18sp" />

</LinearLayout>

Also, These are the Adapter classes I used in my code..

class LoanTypeAdapter extends ArrayAdapter<String> {

        Context context;
        String[] items;

        public LoanTypeAdapter(Context context, String[] items) {

            super(context, R.layout.spinner_entries_style, R.id.textView, items);
            this.context = context;
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = convertView;
            ViewHolder holder = null;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(android.R.layout.simple_spinner_item,
                        parent, false);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.textView.setText(items[position]);
            holder.textView.setTextColor(Color.parseColor("#d7a801"));
            holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            holder.textView.setSingleLine(true);
            return holder.textView;
            // ------------------------------------------

        }

        class ViewHolder {
            final TextView textView;

            ViewHolder(View view) {
                textView = (TextView) view;
            }
        }

    }

And this..

class LoanProgramAdapter extends ArrayAdapter<String> {

        Context context;
        String[] items;

        public LoanProgramAdapter(Context context, String[] items) {

            super(context, R.layout.spinner_entries_style, R.id.textView, items);
            this.context = context;
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder holder = null;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(android.R.layout.simple_spinner_item,
                        parent, false);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.textView.setText(items[position]);
            holder.textView.setTextColor(Color.parseColor("#d7a801"));
            holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            holder.textView.setSingleLine(true);
            return holder.textView;

        }

        class ViewHolder {
            final TextView textView;

            ViewHolder(View view) {
                textView = (TextView) view;
            }
        }

    }

But, there is something strange I encountered after building the spinners. When running the app in Android versions (4.2 or less), there is a white space below the dropdown list.

Here are the screenshots of what happens..

http://www.mediafire.com/view/cqj51t8e3aju18m/01.png

http://www.mediafire.com/view/ure788yetrt00v3/02.png

That is not just on the emulator, but also in the real devices having Android 4.2 or less. Also, this white space seems a bit bigger in some devices.

Is there any idea to hide or kill these white area? Any solution for this problem?

Community
  • 1
  • 1

2 Answers2

1

I found the solution of this problem after reading this post.

How to wrap lengthy text in a spinner

In my layout called spinner_entries_style.xml, I should specify the height of text view like that:

android:layout_height="?android:attr/listPreferredItemHeight"

And it will be like that:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_marginBottom="2dp"
        android:background="#640c1c"
        android:maxEms="10"
        android:padding="10dp"
        android:singleLine="false"
        android:textColor="#d7a801"
        android:textSize="18sp" />

</LinearLayout>

That solved my problem successfully..

Community
  • 1
  • 1
0

Remove this line from spinner_entries_style.xml.

android:layout_marginBottom="2dp"
kelvincer
  • 5,929
  • 6
  • 27
  • 32