BlacKCaT27 / CurrencyEditText

A module designed to encapsulate the use of an Android EditText field for gathering currency information from a user. Supports all ISO-3166 compliant locales/currencies.
Apache License 2.0
339 stars 79 forks source link

force curser to right, when currecyEditText is focused #32

Closed d34n0s closed 7 years ago

d34n0s commented 7 years ago

Hi, nice library, thank's for sharing it. Do you know if it's possible to force the curser to the furthest right when the currencyEditText first gets focus?

As it is at the moment when the user clicks in the editText the curser just appears wherever they clicked, which means the first number they enter will appear at that position, then subsequent numbers will appear on the right. It makes the user experience a little awkward.

Cheers,

BlacKCaT27 commented 7 years ago

Nice catch. I'll take a look tonight. Should be a simple fix.

On Jul 17, 2017 6:14 PM, "d34n0s" notifications@github.com wrote:

Hi, nice library, thank's for sharing it. Do you know if it's possible to force the curser to the furthest right when the currencyEditText first gets focus?

As it is at the moment when the user clicks in the editText the curser just appears wherever they clicked, which means the first number they enter will appear at that position, then subsequent numbers will appear on the right. It makes the user experience a little awkward.

Cheers,

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/BlacKCaT27/CurrencyEditText/issues/32, or mute the thread https://github.com/notifications/unsubscribe-auth/AE_54W9M9z-7GVuTQOCYVFtYbhlF4Dvnks5sO9zKgaJpZM4Oams7 .

d34n0s commented 7 years ago

i added the following to the CurrencyEditText class to solve the issue. Thanks, and please feel free to close this issue :)

public void onSelectionChanged(int start, int end) {
    CharSequence text = getText();
    if (text != null) {
        if (start != text.length() || end != text.length()) {
            setSelection(text.length(), text.length());
            return;
        }
    }
    super.onSelectionChanged(start, end);
}
BlacKCaT27 commented 7 years ago

After trying this out as the library currently stands, I'm not sure I actually agree it's the correct behavior to ALWAYS force the cursor to the end. If you're editing an already populated field and just need to change, say, the 10s digit, it's easier to click in front of that, backspace once, enter in the new number, and be done, then to try to do so, have the cursor thrown to the right, hit backspace 3 times, enter the digit you wanted, reenter the digits that you had to delete (which you may have now forgotten), just to get to the same state.

I'm going to close this issue since you've found a workaround for your use, but I'm not going to add this particular functionality to CET.

d34n0s commented 7 years ago

Yeah no problem at all. Thanks

What happens if the user deletes two characters in the middle of the string though, and then tries to replace both..... It get's a bit messy, as the first character will appear in the correct spot, then the second at far right.

It's a hard one to solve for all scenarios. I wish Android just had a currency control by default ;)

I'll stick to my forcing right I think just for consistent behaviour.

Thanks again for the lib :D

Great work!

P.S. I also modified it slightly so the user can toggle the value to and from a negative number at any time using the '-' key.

BlacKCaT27 commented 7 years ago

"P.S. I also modified it slightly so the user can toggle the value to and from a negative number at any time using the '-' key.".

Could you post that for me if you don't mind? That I very much would like to add, I just hadn't gotten around to revamping negative value support yet.

On Tue, Jul 18, 2017 at 8:24 PM, d34n0s notifications@github.com wrote:

Yeah no problem at all. Thanks

What happens if the user deletes two characters in the middle of the string though, and then tries to replace both..... It get's a bit messy, as the first character will appear in the correct spot, then the second at far right.

It's a hard one to solve for all scenarios. I wish Android just had a currency control by default ;)

I'll stick to my forcing right I think just for consistent behaviour.

Thanks again for the lib :D

Great work!

P.S. I also modified it slightly so the user can toggle the value to and from a negative number at any time using the '-' key.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/BlacKCaT27/CurrencyEditText/issues/32#issuecomment-316236502, or mute the thread https://github.com/notifications/unsubscribe-auth/AE_54YqMDbL9CCuxQ0m1Okj0fSJSVPZSks5sPUyogaJpZM4Oams7 .

d34n0s commented 7 years ago

This might not be the best way. But it's how I managed to get it working :)

  1. first i had to create a KeyListener

`public class CurrencyKeyListener extends NumberKeyListener {

private static CurrencyKeyListener sInstance;

@Override
protected char[] getAcceptedChars() {
    return new char[] { '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' };
}

public static CurrencyKeyListener getInstance()
{
    if(sInstance != null)
        return sInstance;
    sInstance = new CurrencyKeyListener();
    return sInstance;
}

public int getInputType()
{
    return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED;
}

}`

  1. then make a couple of changes to the CurrencyEditText class.

a. in the init()

'this.setKeyListener(CurrencyKeyListener.getInstance());'

b. Add a method to change text colour (just a style preference, not really necessary)

` protected void setNegativeTextColour(boolean b){

    if(b) {
        this.setTextColor(getResources().getColor(android.R.color.holo_red_light));
    } else {
        this.setTextColor(getResources().getColor(R.color.darkFont));
    }

}`
  1. modify the CurrencyTextWatcher classes After Text change to add a few lines.

` newText = (editText.areNegativeValuesAllowed()) ? newText.replaceAll("[^0-9/-]", "") : newText.replaceAll("[^0-9]", "");

        //if newText already contains a '-' at the start and we receive another - at the end, remove the minus as the user wants to toggle
        if(newText.startsWith("-") && newText.endsWith("-")){
            newText = newText.replace("-","");
        }

        //move the incoming minus symbol from the back to the front
        if(newText.contains("-")){
            newText = newText.replace("-","");
            newText = newText.replace(newText, "-" + newText);
            editText.setNegativeTextColour(true);
        } else {
            editText.setNegativeTextColour(false);
        }

        if(!newText.equals("") && !newText.equals("-")){
            //Store a copy of the raw input to be retrieved later by getRawValue
            editText.setRawValue(Long.valueOf(newText));
        }`
BlacKCaT27 commented 7 years ago

makes total sense. That's a great idea. If you want to submit a PR I'll happily bring it in. Or if you'd rather I bake it in myself that's fine too. Just let me know.

On Tue, Jul 18, 2017 at 9:02 PM, d34n0s notifications@github.com wrote:

This might not be the best way. But it's how I managed to get it working :)

  1. first i had to create a KeyListener

`public class CurrencyKeyListener extends NumberKeyListener {

private static CurrencyKeyListener sInstance;

@Override protected char[] getAcceptedChars() { return new char[] { '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' }; }

public static CurrencyKeyListener getInstance() { if(sInstance != null) return sInstance; sInstance = new CurrencyKeyListener(); return sInstance; }

public int getInputType() { return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED; }

}`

  1. then make a couple of changes to the CurrencyEditText class.

a. in the init()

'this.setKeyListener(CurrencyKeyListener.getInstance());'

b. Add a method to change text colour (just a style preference, not really necessary)

` protected void setNegativeTextColour(boolean b){

if(b) {
    this.setTextColor(getResources().getColor(android.R.color.holo_red_light));
} else {
    this.setTextColor(getResources().getColor(R.color.darkFont));
}

}`

  1. modify the CurrencyTextWatcher classes After Text change to add a few lines.

` newText = (editText.areNegativeValuesAllowed()) ? newText.replaceAll("[^0-9/-]", "") : newText.replaceAll("[^0-9]", "");

    //if newText already contains a '-' at the start and we receive another - at the end, remove the minus as the user wants to toggle
    if(newText.startsWith("-") && newText.endsWith("-")){
        newText = newText.replace("-","");
    }

    //move the incoming minus symbol from the back to the front
    if(newText.contains("-")){
        newText = newText.replace("-","");
        newText = newText.replace(newText, "-" + newText);
        editText.setNegativeTextColour(true);
    } else {
        editText.setNegativeTextColour(false);
    }

    if(!newText.equals("") && !newText.equals("-")){
        //Store a copy of the raw input to be retrieved later by getRawValue
        editText.setRawValue(Long.valueOf(newText));
    }`

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/BlacKCaT27/CurrencyEditText/issues/32#issuecomment-316241986, or mute the thread https://github.com/notifications/unsubscribe-auth/AE_54REIx9Ih_uX095N-P_ZmmGLkrrtzks5sPVWwgaJpZM4Oams7 .

d34n0s commented 7 years ago

thanks josh, i don't have it cloned in git sorry. So can't submit PR.