Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 5, 2016 4:44 AM
how to hide first item in an android spinner dropdown .
All replies (17)
Wednesday, February 17, 2016 2:53 PM β Answered
@Rish,
I just gone through the error you had mentioned in my sample. I had made some changes in my codes and created a new sample, which can solve that error.
SpinnerItem.axml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textStyle="italic"
android:fontFamily="sans-serif-medium" />
CustomAdapter.cs
using System;
using System.Collections.Generic;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.Widget;
namespace SpinnerTest
{
public class CustomAdapter : ArrayAdapter<String>
{
public CustomAdapter(Context context, int textViewResourceId, List<String> objects)
: base(context, textViewResourceId, objects)
{
}
public override bool IsEnabled (int position)
{
// Disable the first item from Spinner
// First item will be use for hint
return position == 0 ? false : true;
}
public override View GetDropDownView (int position, View convertView, ViewGroup parent)
{
View view = base.GetDropDownView (position, convertView, parent);
TextView tv = (TextView) view;
if(position == 0){
// Set the hint text color gray
tv.SetTextColor(Color.Gray);
}
else {
tv.SetTextColor(Color.Black);
}
}
}
}
MainActivity.cs
using System;
using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Widget;
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);
var mySpinner = FindViewById<Spinner> (Resource.Id.spinner);
var list = new List<String>();
list.Add ("[Select one]");
list.Add("string1");
list.Add("string2");
list.Add("string3");
var dataAdapter = new CustomAdapter(this, Resource.Layout.SpinnerItem, list);
dataAdapter.SetDropDownViewResource (Resource.Layout.SpinnerItem);
mySpinner.Adapter = dataAdapter;
mySpinner.ItemSelected += (s, e) => {
String selectedItemText= mySpinner.SelectedItem.ToString();
int position = mySpinner.SelectedItemPosition;
// If user change the default selection
// First item is disable and it is used for hint
if (position > 0)
// Notify the selected item text
Toast.MakeText (ApplicationContext, "Selected : " + selectedItemText, ToastLength.Short).Show ();
};
mySpinner.NothingSelected += (s, e) => {
};
}
}
}
Check this, and give a reply π
Friday, February 5, 2016 6:43 AM
@Rishi Naithani,
https://developer.xamarin.com/guides/android/user_interface/spinner/
refer this document.....................
Friday, February 5, 2016 9:55 AM
@Rish,
I suggest you links of similar post :
- https://forums.xamarin.com/discussion/12755/set-spinner-value-to-display-select-option-on-startup
- http://stackoverflow.com/a/15162795/3891036
Monday, February 8, 2016 10:46 AM
Hi @YkshLeo . I have gone through the links , but cudn't get the solution. Could you provide a sample code for it in Xamarin. ?
Tuesday, February 9, 2016 6:07 AM
@Rish,
Try this attached sample. π
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;
}
}
}
}
Wednesday, February 10, 2016 3:38 PM
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.
Wednesday, February 10, 2016 3:48 PM
@Rish,
Can you create a sample project and attach.
Thursday, February 11, 2016 6:31 AM
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.
Tuesday, February 16, 2016 6:06 AM
@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 .
Tuesday, February 16, 2016 6:16 AM
@Rish, Let me check it π
Wednesday, February 17, 2016 3:57 AM
Hi @YkshLeo were you able to find the reason behind the rotation issue .
Wednesday, February 17, 2016 6:40 AM
@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.
Wednesday, February 17, 2016 6:55 AM
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 . π
Wednesday, February 17, 2016 7:03 AM
@Rish, I just noted that, Let check that π
Thursday, February 18, 2016 7:05 AM
Thanks @YkshLeo . I ll check this and reply back π
Thursday, February 18, 2016 9:44 AM
The above code worked wonderfully well. π Thank You so much @YkshLeo for the effort .
Friday, August 12, 2016 9:56 PM
Thank You @YkshLeo I was looking so many hours for this!