How to limit Spinner text single line?

MidusMidus USMember
edited August 2017 in Xamarin.Android

I will try to use
android:ellipsize="end" android:maxLines="1"

or
android:singleLine="true"

But textview in Spinner appear more 1 line.
This is my xml code
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/edit_text_style"
android:layout_weight="1">
<Spinner
android:id="@+id/barOldLocation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:popupBackground="@drawable/popup_style"
android:ellipsize="end"
android:maxLines="1"/>

Thanks for help.

Answers

  • There is one way to limit the text. You should use a layout with textview and assign attributes like max length, lines..., and then use this text view in your spinner adapter as r.layout.spinner_item_layout.

  • DimChrisDimChris USMember ✭✭✭

    Why don't you populate a list collection instead, and then select number of items you want to populate for your spinner

     public class MainActivity : AppCompatActivity
    {
        List<string> mItems = new List<string>();
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            var spinner = FindViewById<Spinner>(Resource.Id.spinner1);
            mItems.Add("A");
            mItems.Add("B");
            mItems.Add("C");
            mItems.Add("D");
            mItems.Add("E");
            mItems.Add("F");
            mItems.Add("G");
    
            spinner.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mItems.GetRange(0, 5));
    
        }
    }
    
Sign In or Register to comment.