user1746708
user1746708

Reputation: 691

Using spinner effectively

How can i have my spinner set on a standard size? Meaning it won't be smaller or bigger depending on the string that it currently shows.I want it to have a fixed size,and if it contains a bigger word ,than its size,that word must be truncated(only showing what can fit the spinner).Thank you for your time.

Upvotes: 0

Views: 143

Answers (2)

Siddhesh
Siddhesh

Reputation: 1380

To give width according to multiple screens you can use dimensions and set width. suppose you want to leave area of spinner button so that text should not ovelay it you can set padding instead of trimming text and also can set marqee effect so that big text will be shown properly.

EDIT :

             <Spinner
                android:id="@+id/spntipode"
                android:layout_width="@dimen/spin_size"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lbltipode"
                android:layout_marginLeft="10dip"
                android:layout_marginTop="5dip" 
                android:paddingLeft="5dip"
                android:paddingRight="30dip"
                android:background="@drawable/spinner_selector"
                />

          public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ((TextView)v).setEllipsize(TruncateAt.MARQUEE);
                return v;
            }

            public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
                View v =super.getDropDownView(position, convertView, parent);
                ((TextView)v).setEllipsize(TruncateAt.MARQUEE);
                return v;
            }

Upvotes: 1

IAmGroot
IAmGroot

Reputation: 13855

Just truncate the string array that you pass to your spinner when you create it.

Pseudo for it:

for each string,

   if string > 20 characters, 

   trim to 20 characters (or 17 + ...)

   add string to array.

add array to adapter of spinner.

But I must point out that a spinner is not a dropdown. The android api will generally show the list in a scrollable "popup" in the middle on screen. And works well as is. If you have huge strings, then maybe this is an issue. Although I seem to think, it truncates for you.. Try it out.

Upvotes: 1

Related Questions