0

I have write this code for dropdown in android xml

    <Spinner
        android:id="@+id/gender"
        android:layout_height="31dp"
        android:layout_width="150dp"
        android:entries="@array/gender"
        android:prompt="@string/gender_select"
        android:background="@drawable/textbox_bg_image">
    </Spinner>

Screen is looks like this type

enter image description here

it's dropdown height or width i can define but i click on it a list come out for options 'male' and 'female' ..

i can't cantrol this selection option list.. can we control drop down background or height and width....

8
  • Make your spinner height and width to wrap_cotent. Commented Mar 13, 2014 at 12:40
  • 1
    you can also use custom spinner in which you can set height and width manually. Commented Mar 13, 2014 at 12:41
  • @PiyushGupta ...is there no option for control height & width in this dropdown Commented Mar 13, 2014 at 12:50
  • 2
    If you are using by default Spinner UI item adapter then you can't. You have to use Custom spinner. Commented Mar 13, 2014 at 12:52
  • 1
    @KuldeepChoudhary Nice. I hope you will be solved using by this one!!! Commented Mar 13, 2014 at 12:57

2 Answers 2

1

Create a custom spinner Adapter and then use the getDropDownView() method.

eg:

adapter = new ArrayAdapter<String>(ActivityName.this,
            R.layout.custom_spinner, gender_arraylist) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);


            return v;
        }

        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);
            //change height and width or text size and colour here

            return v;
        }
    };
Sign up to request clarification or add additional context in comments.

1 Comment

thanks sir....in my question i wanted to know "can we custimize default drop down spinner" ,as much i know we can't do this....so i have tried your custom spinner ..it's working fine for me........
1

//it will help you to achieve this

genderAdapter = new ArrayAdapter<String>(context,
            R.layout.my_spinner_style, subjectList) {
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);

            Typeface externalFont = Typeface.createFromAsset(getAssets(),
                    "ArchitectsDaughter.ttf");
            ((TextView) v).setTypeface(externalFont);

            return v;
        }

        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);

            Typeface externalFont = Typeface.createFromAsset(getAssets(),
                    "ArchitectsDaughter.ttf");
            ((TextView) v).setTypeface(externalFont);
            v.setBackgroundColor(Color.GRAY);
            ((TextView) v).setTextColor(Color.parseColor("#FFFFFF"));
            return v;
        }

//above code will help to give background color for dropdown spinner and to customize the font . width of drop down depends on width of spinner.Below is the my_spinner_style.xml for customizing text

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#ffffff" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.