how to hide first item in an android spinner?

RishRish USMember
edited February 2016 in Xamarin.Android

how to hide first item in an android spinner dropdown .

Best Answer

Answers

  • Vikram_BVikram_B USMember ✭✭

    @Rishi Naithani,

    https://developer.xamarin.com/guides/android/user_interface/spinner/

    refer this document.....................

  • RishRish USMember

    Hi @YkshLeo . I have gone through the links , but cudn't get the solution. Could you provide a sample code for it in Xamarin. ?

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @Rish,

    Try this attached sample. :smile:


    Sample Code

    MainActivity.cs

        using Android.App;
        using Android.OS;
        using Android.Widget;
        using System.Collections.Generic;
        using System;
    
        namespace SpinnerTest
        {
            [Activity (Label = "SpinnerTest", MainLauncher = true, Icon = "@mipmap/icon")]
            public class MainActivity : Activity
            {
                protected override void OnCreate (Bundle savedInstanceState)
                {
                    base.OnCreate (savedInstanceState);
    
                    SetContentView (Resource.Layout.Main);
    
                    Spinner mySpinner = FindViewById<Spinner> (Resource.Id.spinner);
    
                    var list = new List<String>();
                    list.Add("string1");
                    list.Add("string2");
                    list.Add("string3");
                    list.Add("[Select one]");
    
                    int listsize = list.Count - 1;
    
                    var dataAdapter = new CustomAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, list, listsize);
                    dataAdapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
                    mySpinner.Adapter = dataAdapter;
                    mySpinner.SetSelection (listsize);
    
                }
            }
        }
    

    CustomAdapter.cs

        using Android.Widget;
        using System;
        using Android.Content;
        using System.Collections.Generic;
    
    
        namespace SpinnerTest
        {       
            public class CustomAdapter : ArrayAdapter<String>
            {
                 int _count;
    
                public CustomAdapter(Context context, int textViewResourceId, List<String> objects, int listCount)
                    : base(context, textViewResourceId, objects)
                {
                    _count = listCount;
                }
    
                public override int Count {
                    get {
                        return _count;
                    }
                }
            }
        }
    
  • RishRish USMember

    Thanks @YkshLeo for the sample code . It works wonderfully well when i run it in a new project.
    But In my case , the spinner is invisible when I launch the app .
    Then , On rotation The spinner becomes visible ,
    but spinner text shows to be "string 3" instead of "[Select one]" .

    Could be because of the theme I am using i.e. Theme.Appcompat.? Or is it something else.

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @Rish,

    Can you create a sample project and attach.

  • RishRish USMember

    Hi @YkshLeo . Everything seems to be working fine now . except on rotation , instead of "select one", it shows the second last item of the list . note: all this code comes inside a fragment.

  • RishRish USMember

    @YkshLeo The SpinnerSample example you provided works great at startup.
    But ,on rotation (or whenever the activity is recreated ) it doesn't show up "Select One" .
    Can you pls check it and tell the reason behind it ?
    Thanks .

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @Rish, Let me check it :smile:

  • RishRish USMember

    Hi @YkshLeo were you able to find the reason behind the rotation issue .

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @Rish, When you navigate to back from page2 to page1, the spinner in the page1 will show the previously selected data. It's because that page is not refreshed when it is navigated back. If you want the [Select one] in the spinner then I suggest you to give some code to refresh page1 when navigated from back from page2.

  • RishRish USMember
    edited February 2016

    I m sorry @ykshleo .. but i cudn't understand.
    The detailed problem is like this:

    The list is like this:
    var list = new List();
    list.Add("string1");
    list.Add("string2");
    list.Add("string3");
    list.Add("[Select one]");

    At first, page1 shows the correct value "[Select One]" as the spinner title .
    then On rotating page 1, spinner title changes from "[Select One]" to "string 3" by itself.
    It should show "[Select One ]" because the spinner is (not clicked) or untouched . :smile:

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @Rish, I just noted that, Let check that :smile:

  • RishRish USMember

    Thanks @YkshLeo . I ll check this and reply back :smile:

  • RishRish USMember

    The above code worked wonderfully well. :smiley:
    Thank You so much @YkshLeo for the effort .

  • DimChrisDimChris USMember ✭✭✭

    Thank You @YkshLeo I was looking so many hours for this!

Sign In or Register to comment.