Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to set the maximum size of a Spinner?

Here is my layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:layout_width="fill_parent" android:id="@+id/LinearLayout01"
         android:layout_height="wrap_content">

    <Spinner android:text="@+id/AutoCompleteTextView01"
                       android:id="@+id/Spinner01"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:width="130dp"></Spinner>
    <EditText android:layout_height="wrap_content"
              android:layout_width="wrap_content"
             android:id="@+id/Chapter" android:width="30dp"></EditText>
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/TextView01" android:text=":"></TextView>
    <EditText android:layout_height="wrap_content"
              android:layout_width="wrap_content" android:id="@+id/Verse"
              android:width="40dp"></EditText>

</LinearLayout>

I inflate this layout as an AlertDialog's view. But when I pick a large element, the text fields get pushed out to the right. Is there any way to set the maximum size of the spinner so after choosing an element, it shortens the choice with an ellipsis ("...") or something?

like image 492
Epaga Avatar asked Mar 05 '10 22:03

Epaga


2 Answers

No need to use a custom adapter. Use the android.R.layout.simple_spinner_item and then change it from ellipse marquee to end ellipse. Then set your new change layout file as layout id or use this layout file

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="10dp"
    android:ellipsize="end" />
like image 109
Indrajit Avatar answered Nov 13 '22 18:11

Indrajit


The size of a Spinner set have android:layout_width="wrap_content" is determined by its content. If you want things ellipsized, that's up to you in your SpinnerAdapter, when you create your row Views.

Or, hard-wire an android:layout_width that is fixed in size.

Or, instead of using LinearLayout, use RelativeLayout and structure your rules such that the Spinner takes up the remaining space on the row, by anchoring it to the left side of the screen and to the left side of the first EditText. Though I'm not 100% certain what then happens if the content is too big -- it probably just gets truncated.

like image 4
CommonsWare Avatar answered Nov 13 '22 16:11

CommonsWare