Bug 30483 - Converters cause cursor position to reset when text changed, in a EditText
Summary: Converters cause cursor position to reset when text changed, in a EditText
Status: RESOLVED UPSTREAM
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-05-26 19:57 UTC by Josh Wallace
Modified: 2015-05-28 21:12 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED UPSTREAM

Description Josh Wallace 2015-05-26 19:57:30 UTC
When using a value converter in an EditText the cursor location resets to the beginning of the field when the text changes.

In our example we are using a converter which changes the format of a phone number to XX XXXX XXXX.

When a number is entered, the cursor moves to the left, causing a number such as 0734553864, to be entered as 4683554370. This also adversely affects the value converters conversion.

I am currently seeing it in a Xamarin.Android application, using MVVM Cross and binding from the axml to the view model, however I have also seen this in a Xamarin.Forms project binding from Xaml to view model and from cs UI to the view model.
Comment 1 Atsushi Eno 2015-05-27 05:05:55 UTC
We don't control over any Android widget behavior in the API bindings.

jonp: do you think it is because of CultureInfo?
Comment 2 Jonathan Pryor 2015-05-27 11:49:44 UTC
@Josh: What is your "value converter"? How is it updating the text?

Is it setting the EditText.Text property?

I imagine you're hitting Android's normal behavior in which calling EditText.setText() (i.e. the EditText.Text property setter) places the cursor at the beginning of the textbox:

https://www.google.com/webhp#q=edittext+setText+cursor+location
http://stackoverflow.com/questions/6217378/place-cursor-at-the-end-of-text-in-edittext
Comment 3 Josh Wallace 2015-05-27 19:41:10 UTC
Hi Jonathan

our value converter is this

public class FnnConverter : MvxValueConverter<string, string>
	{
		protected override string Convert (string value, Type targetType, object parameter, CultureInfo culture)
		{
			var numbers = Regex.Replace (value.ToString (), @"\D", "");

			if (numbers.Length <= 2)
				return numbers;
			if (numbers.Length <= 6)
				return string.Format ("{0} {1}", numbers.Substring (0, 2), numbers.Substring (2));

			return string.Format ("{0} {1} {2}", numbers.Substring (0, 2), numbers.Substring (2, 4), numbers.Substring (6));
		}

		protected override string ConvertBack (string value, Type targetType, object parameter, CultureInfo culture)
		{
			return Regex.Replace (value.ToString (), @"\D", "");
		}

	}

which we are referencing using MVX binding in our axml like this

    <EditText
        android:id="@+id/home_et_fnn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_et_height"
        android:background="@drawable/bg_home_et"
        android:inputType="numberDecimal"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:maxLength="12"
        android:paddingLeft="@dimen/activity_margin_fnn"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:paddingStart="@dimen/activity_margin_large"
        android:textColorHint="@android:color/black"
        android:imeOptions="actionDone"
        android:digits="0123456789 "
        local:MvxBind="Text Fnn(Fnn), Mode=TwoWay; Hint FnnHint;  Click ShowPreviousFnnCommand" />

This is using Tibet binding of MVVM Cross as outlined here

https://github.com/MvvmCross/MvvmCross/wiki/Databinding#tibet

and inspired from the value converter outlined here 

http://gregshackles.com/auto-formatting-text-inputs-with-mvvmcross-and-value-converters/

Please let me know if we are still doing something wrong.

Josh
Comment 4 Atsushi Eno 2015-05-28 05:39:48 UTC
As Jonathan commented, it is mostly all about how EditText.Text is set by MvvmCross. Maybe with a value converter it needs to retrieve the entire text value in the EditText and set it? MvvmCross developers would know about that.
Comment 5 Josh Wallace 2015-05-28 21:12:02 UTC
I have seen this happen also with MVVM light with a Xamarin.Forms project. Have you guys successfully used this kind of model? I would think it quite common.