How to get started easily with Xamarin Android.ComboBox control?
This article provides an introduction how to get started easily with SfComboBox in Xamarin Android.
The following steps explains to create a new SfComboBox sample in Android.
1. Choose File > New > Project... or press the Create new project... button.
2. Search for "Android" or choose Mobile from the Project type menu. Select the Android App (Xamarin) project type.
3. Enter a project name (For e.g. ComboBoxSample) and click Create option.
4. Click the Blank APP project type and click OK.
5. Now, the project is creating and restoring the NuGet packages, wait until the NuGet packages are restored (a "Restore completed" message will appear in the status bar).
6. After that, right-click the solution explorer to select Manage Nuget Packages for Solution.
7. Browse Syncfusion.Xamarin.SfComboBox.Android in the Nuget Solution Window.
8. Install the Selected package.
9. If you prefer to manually refer the assemblies instead of NuGet, then refer the following mentioned assembly.
Project | Required assemblies |
Xamarin.Android | Syncfusion.SfComboBox.Android.dll |
10. Add the namespace using the Syncfusion.Android.ComboBox.
11. Then, add the SfComboBox control with a required optimal name using the included namespace.
// Set the layout to display the control LinearLayout linearLayout = new LinearLayout(this); linearLayout.LayoutParameters = new ViewGroup.LayoutParams(500, ViewGroup.LayoutParams.MatchParent); linearLayout.SetPadding(10, 0, 10, 0); linearLayout.SetBackgroundColor(Android.Graphics.Color.White);
// Add the Combobox Control SfComboBox comboBox = new SfComboBox(this); comboBox.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, 50); //Add the control in layout to display linearLayout.AddView(comboBox);
SetContentView(linearLayout); |
12. After adding SfComboBox, the following screenshot depicts a view of the control.
13. Adding Items into the ComboboxSource.
A list of string with resolution list are created and added to the combo box source. This list is populated as suggestion list by setting the ComboBoxSource property based on text entry.
You can set the suggestion list to the SfComboBox using the ComboBoxSource property and also you can customize the drop-down height using the MaximumDropDownHeight property. Add the ComboBoxSource for the SfComboBox as follows.
using Android.App; using Android.OS; using Android.Support.V7.App; using Android.Runtime; using Android.Widget; using System; using Syncfusion.Android.ComboBox; using Android.Views; using System.Collections.Generic;
namespace ComboBoxSample { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set the layout to display the control LinearLayout linearLayout = new LinearLayout(this); linearLayout.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent); linearLayout.SetPadding(10, 0, 30, 0); linearLayout.SetBackgroundColor(Android.Graphics.Color.White);
// Add the Combobox Control SfComboBox comboBox = new SfComboBox(this); comboBox.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, 50);
//Add the items List<String> resolutionList = new List<String>(); resolutionList.Add("1920 x 1080"); resolutionList.Add("1680 x 1050"); resolutionList.Add("1600 x 900"); resolutionList.Add("1440 x 900"); resolutionList.Add("1400 x 1050"); resolutionList.Add("1366 x 768"); resolutionList.Add("1360 x 768");
//Add the ComboBox source ArrayAdapter<String> resolutionListDataAdapters = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, resolutionList); comboBox.ComboBoxSource = resolutionListDataAdapters;
//Set MaximumDropDownHeight comboBox.MaximumDropDownHeight = 150;
//Add the control in layout to display linearLayout.AddView(comboBox); SetContentView(linearLayout); }
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } } } |
Get the sample from this link.