In my Xamarin Android application user needs to select an item from a spinner (dropdown menu). After selecting any item from the spinner the accessibility focus shifts to the start of the screen instead of the next focusable element, which is not the expected behavior. So trying to find a way to set focus to the button programmatically but it seems there is no way in Xamarin Android.
I have tried
Button1.RequestFocus();
Button1.RequestFocusFromTouch();
Button1.SendAccessibilityEvent(EventTypes.ViewAccessibilityFocused);
But nothing works!!
The below code example is related to Xamarin iOS:
UIAccessibility.PostNotification(UIAccessibilityPostNotification.ScreenChanged, myFirstElement);
This method which is available in Xamarin iOS works fine so need a similar way for Xamarin Android to focus programmatically.
Here, I am posting sample code.
My .axml file code:
<?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="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp" android:focusable="true" android:focusableInTouchMode="true"/>
<Button
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:focusable="true"
android:focusableInTouchMode="true"/>
</LinearLayout> </LinearLayout>
My Activity file
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
this.SetContentView (Resource.Layout.Main);
Spinner spinner = this.FindViewById<Spinner>(Resource.Id.spinner);
Button btnAdd = this.FindViewById<Button>(Resource.Id.btnAdd);
var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
btnAdd.Click += (s, e) =>
{
string toasts = string.Format("Add button got focus");
Toast.MakeText(this, toasts, ToastLength.Long).Show();
};
}
private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string toast = string.Format("The planet is {0}", spinner.GetItemAtPosition(e.Position));
Toast.MakeText(this, toast, ToastLength.Long).Show();
//btnAdd.RequestFocus();
btnAdd.RequestFocusFromTouch();
btnAdd.RequestFocus();
if (btnAdd.IsFocused)
{
string toasts = string.Format("Add button got focus");
Toast.MakeText(this, toasts, ToastLength.Long).Show();
}
skillSeleted = "selected";
}
protected override void OnResume()
{
base.OnResume();
if (skillSeleted == "selected")
{
btnAdd.RequestFocusFromTouch();
btnAdd.RequestFocus();
}
}
//Add this to String file under values folder:
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
Please check this scenario in Samsung Mobile with Marshmallow android version.
Thanks in advance.