Create android spinner dynamically in Xamarin - xamarin.android

I am creating a simple application to get familiar with Xamarin. I want to create and populate a spinner and display its options dynamically. I have seen the documentation here but it is not created programmatically. Any help will be appreciated
var levels = new List<String>() { "Easy", "Medium", "Hard", "Multiplayer" };
var adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleSpinnerItem, levels);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
var spinner = FindViewById<Spinner>(Resource.Id.spnrGameLevel);
spinner.Adapter = adapter;
spinner.ItemSelected += (sender, e) =>
{
var s = sender as Spinner;
Toast.MakeText(this, "My favorite is " + s.GetItemAtPosition(e.Position), ToastLength.Short).Show();
};
My axml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose your game level" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spnrGameLevel" />
</LinearLayout>

To dynamically create items for Spinner, you need to have an adapter.
The simplest adapter would be ArrayAdapter<T>.
Here is the example.
var items = new List<string>() {"one", "two", "three"};
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);
After setting up the adapter, find the Spinner in your view and set adapter to it.
var spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.Adapter = adapter;

Related

Oxyplot graph takes up whole screen for display

I just started using using oxyplot library for my project. I successfully added a donut chart in my project but the problem with this is that it takes up the whole screen to show the graph. Is tried so many way to solve this but none of them worked.
Here is my axml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
local:layout_behavior="#string/appbar_scrolling_view_behavior" >
<OxyPlot.Xamarin.Android.PlotView
android:id="#+id/plot_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Model DonutChart"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
local:MvxBind="Text TestText1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
local:MvxBind="Text TestText2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
local:MvxBind="Text TestText3" />
</LinearLayout>
Here is the graph method:
private void GeneratePlotModel()
{
var plotmodel = new PlotModel();
var seriesP1 = new PieSeries
{
Stroke = OxyColors.Transparent,
StrokeThickness = 2.0,
InsideLabelPosition = 0.0,
AngleSpan = 360,
StartAngle = 0,
InnerDiameter = 0.7,
OutsideLabelFormat = null,
InsideLabelFormat = null,
TickHorizontalLength = 0.0,
TickRadialLength = 0.0
};
seriesP1.Slices.Add(new PieSlice("Test 1", test1) { IsExploded = false, Fill = OxyColors.Blue });
seriesP1.Slices.Add(new PieSlice("Test 2", test2) { IsExploded = false, Fill = OxyColors.Green });
seriesP1.Slices.Add(new PieSlice("Test 3", test3) { IsExploded = false, Fill = OxyColors.Red });
plotmodel.Series.Add(seriesP1);
DonutChart = plotmodel;
}
Here is my plotmodel property:
private PlotModel _donutChart ;
public PlotModel DonutChart
{
get => _donutChart ;
set
{
if (_donutChart == value)
return;
_donutChart = value;
RaisePropertyChanged(nameof(DonutChart ));
}
}
It takes up the whole screen to show the graph because you set
android:layout_width="match_parent"
android:layout_height="match_parent"
And you can make it only takes patial space by adding
android:layout_weight="1"
So the layout is like this:
<OxyPlot.Xamarin.Android.PlotView
android:id="#+id/plot_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
local:MvxBind="Model DonutChart"/>

List sqlite database Table in xamarin.android

I Want To List All sqlite database table,
When i tries to List the table contents It Only Shows the First columns of table.
AXML Page:;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:clickable="true"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:id="#+id/listView1" />
cs Page:
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "BookmarkSqlite.db3");
var db = new SQLiteConnection(dpPath);
var data = db.Table<BookmarkSqliteTable>();
foreach (var listing in data)
{ var from = new string[]{ listing.SiteName + "----" + listing.SiteUrl };
//var from = fromm.ToList();
ListView listtable = (ListView)FindViewById(Resource.Id.listView1);
listtable.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, from);
}
}

How to add text color to ListView

How to add text color to ListView ?
The background is white for Layout and I need the text color to be darkgray or black.
--UI
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ListView1" />
</LinearLayout>
-- code :
listV= FindViewById<ListView>(Resource.Id.ListView1);
itemlist = new List<string>();
itemlist.Add("item1");
itemlist.Add("item2");
itemlist.Add("item3");
itemlist.Add("item4");
itemlist.Add("item5");
itemlist.Add("item6");
itemlist.Add("item7");
itemlist.Add("item8");
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemlist);
listV.Adapter = adapter;
Thanks
Create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.
e.g. mytextview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tview"
android:textColor="#color/yourColor"
android:padding="5sp"
android:layout_width="fill_parent"
android:background="#drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
and then use it with your ArrayAdapter as usual like
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, R.layout.mytextview, itemlist);

Why isn't my ListView ItemClick event being triggered?

Why doesn't my ItemClick event trigger?
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id);
NOTE:
The listview is populated.
Items receive focus.
However, I just cannot get the itemclick event to trigger.
I have the following initialization code for my ListView:
_specialtyListView = FindViewById<ListView>(Resource.Id.SpecialtyListView);
_specialtyListView.ChoiceMode = ChoiceMode.Single;
_viewModel.LoadSpecialties();
_specialtyListView.Adapter = new SpecialtiesAdapter(this, new List<string>(_viewModel.Specialties));
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id);
I then have the following ListViewItem layout:
<ListView
android:id="#+id/SpecialtyListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="2dp" />
Here's the Layout for my ListItem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/ProviderSpecialty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
I suggest you switch to RecyclerView as it is has better memory handling; you can make updates to single items instead of being forced to update them all.
Anyways, I can't reproduce your issue locally. I did the following:
var listView = FindViewById<ListView>(Resource.Id.listview);
listView.ChoiceMode = ChoiceMode.Single;
listView.ItemClick += (s, e) =>
{
System.Diagnostics.Debug.WriteLine($"Clicked {e.Position}");
};
var items = new string[] { "Havarti", "Brie", "Cheddar", "Muenster", "Swiss" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
listView.Adapter = adapter;
I used the exact same layout as you posted. The ItemClick handler is hit consistently.
There's a bug where the breakpoint will NOT be triggered when the code is NOT wrapped in between braces:
listview.ItemClick += (s, e) => selectedItem = listview.GetItemAtPosition(e.Position).ToString();
To observe the breakpoint, I had to format the code as the following:
listview.ItemClick += (s, e) =>
{
selectedItem = listview.GetItemAtPosition(e.Position).ToString();
};

How to use the native control of Stepper in Xamarin Android

I use a Xamarin.Forms layout, and it has a Stepper control.
On Android, it looks like this:
Now it seems that I have to create custom cells on each platform as the UWP performance is way-way-way below the normal scrolling speed.
According to this page, the Stepper is rendered via a custom view in Android:
I created an Android layout, and I intend to use the StepperRenderer, but I don't know how to inflate the custom view inside my custom viewcell:
<LinearLayout
android:id="#+id/Layout1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F3300"
android:textSize="20dip"
android:textStyle="italic" />
<!-- NumberPicker is not applicable-->
<NumberPicker
android:id="#+id/Stepper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--StepperRenderer
android:id="#+id/Stepper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /-->
<Spinner
android:id="#+id/Picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
protected override Android.Views.View GetCellCore(Xamarin.Forms.Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
{
var x = (MyViewCell)item;
var view = convertView;
if (view == null)
{
// no view to re-use, create new
view = (context as Activity).LayoutInflater.Inflate(Resource.Layout.MyViewCellLayout, null);
}
view.FindViewById<TextView>(Resource.Id.Text).Text = x.Text;
view.FindViewById<NumberPicker>(Resource.Id.Stepper).Value = x.Value;
return view;
}
Any idea how I could use that view in my viewCell?
There is similar thread related to this:
https://forums.xamarin.com/discussion/26173/custom-renderer-for-stepper
Hope this helps.

Resources