In my app, I have an EditText object. When the object is touched, I want to select all the text if no text is selected, and deselect it if any text is selected. Seems easy enough, but not so much. I’m doing this in C# and Xamarin.
Here is my EditText object:
<EditText
android:text=""
android:textIsSelectable="true"
android:textColor="@android:color/holo_blue_dark"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginLeft="110dp"
android:layout_marginTop="7dp"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:id="@+id/txtCS" />
Here is my code:
EditText? txtCS = (EditText?)FindViewById(Resource.Id.txtCS);
if (txtCS != null)
{
txtCS.Click += (sender, e) =>
{
if (!txtCS.HasSelection)
{
txtCS.SelectAll();
}
else
{
txtCS.SetSelection(0);
}
};
}
The problem is that txtCS.HasSelection is always false, even when I can see on the view that the contents of the EditText object are selected. I’ve also tried combinations of txtCS.SelectionStart and txtCS.SelectionEnd, can’t get those to work either.
I’m pretty new to the Android world, coming from the Windows world. What am I missing?
Thanks.
Don
2