63

I have a layout which has three fields for the entry of three map coordinates. So far so good. I'm using android:inputType="numberDecimal" in the layout. When entering the field the user gets the numeric keypad. Still good.

However, when a negative coordinate needs to be entered, there is no apparent way to do this.

23.2342 works fine. 232.3421 works fine. -11.23423 can not be entered - there is no way to enter the leading negative sign, or even wrap the coordinate in ().

I'm sure I can go the route of changing this to straight text inputType, and then use a regular expression to validate that what was entered is in fact a numeric coordinate, handle error messaging back to the user, etc. But I'd rather not go that route.

I have Googled and Stackoverflowed this question for a couple hours with no luck. Any suggestions?

EBM
  • 719
  • 1
  • 5
  • 9
  • 4
    I had the same problem, but Kevin's response solved my problem. I just needed the ability to have a numbers-only EditText with the ability to have negative numbers. Thanks! android:inputType="numberDecimal|numberSigned" – thePITman Aug 14 '11 at 15:54
  • The answer posted [here](http://stackoverflow.com/a/32397808/2437881) is the simplest solution that you are looking for to solve your problem, so take a look ;) – SerSánGal Sep 21 '15 at 16:09

8 Answers8

158

Unless I'm missing something this is all you need

android:inputType="numberDecimal|numberSigned"
Tim
  • 41,901
  • 18
  • 127
  • 145
Kevin Burandt
  • 1,970
  • 1
  • 14
  • 10
8

Please use this code

 et.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Archie
  • 161
  • 1
  • 8
5

You have to add numberSigned in the EditText tag of your layout (*.xml)

 <EditText android:inputType="numberDecimal|numberSigned"></EditText>

Good luck!

edwindh
  • 99
  • 1
  • 4
2

set InputType.TYPE_NUMBER_FLAG_SIGNED in EditText.setInputType

Pinki
  • 21,723
  • 16
  • 55
  • 88
  • Using .xml layout, and this (android:inputType="TYPE_NUMBER_FLAG_SIGNED" gives error: Error: String types not allowed (at 'inputType' with value 'TYPE_NUMBER_FLAG_SIGNED'). – EBM Feb 25 '11 at 02:07
2

I think that creating your own custom keylistener may be your best bet. I did something similar. I created a SignedDecimalKeyListener like this:

public class SignedDecimalKeyListener extends NumberKeyListener 
{
    private char[] mAccepted;
    private static SignedDecimalKeyListener sInstance;

    private static final char[] CHARACTERS = new char[] { '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' };

    @Override
    protected char[] getAcceptedChars()
    {
        return mAccepted;
    }

    private SignedDecimalKeyListener()
    {
        mAccepted = CHARACTERS;
    }

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

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

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
        CharSequence out = super.filter(source, start, end, dest, dstart, dend);

        if(out != null)
        {
            source = out;
            start = 0;
            end = out.length();
        }

        //Only allow a '-' to be the very first char
        //and don't allow '.' to be the first char
        if(dstart > 0 && source.equals("-") || dstart == 0 && source.equals("."))
        {
            SpannableStringBuilder stripped = null;

            stripped = new SpannableStringBuilder(source, start, end);
            stripped.delete(start, end);

            if(stripped != null)
                return stripped;
        }
        else if(source.equals("."))
        {
            for(int lo = dend-1; lo > 0; lo--)
            {
                char c = dest.charAt(lo);
                if(source.equals(String.valueOf(c)))
                {
                    SpannableStringBuilder stripped = null;

                    stripped = new SpannableStringBuilder(source, start, end);
                    stripped.delete(start, end);

                    if(stripped != null)
                        return stripped;
                }
            }
        }

        if(out != null)
            return out;
        else
            return null;
    }
}

And then use this custom keylistener with your edittext like this:

EditText myEditText = (EditText)findViewById(R.id.myedittext);
myEditText.setKeyListener(SignedDecimalKeyListener.getInstance());
VMcPherron
  • 584
  • 1
  • 6
  • 10
0

i m not sure about the input type but you can always create your own text filter and attach it on an editText field. that one is pretty simple. you can do the check on every character and only allow the numbers, dot or any custom character you like.

DArkO
  • 15,880
  • 12
  • 60
  • 88
-1

Ended up using Pattern and Matcher for each of the axes, leaving the actual text input format open.

Three axes fields, submit button triggers onSave. Fields are validated and either the insert occurs, or error message is raised to submitter about required format for the Axes fields. Proper format is 1-3 digits, optionally prefaced by '-', optionally followed by '.' and additional digits.

Try this code :

private View.OnClickListener onSave=new View.OnClickListener(){
    public void onClick(View v){
        Pattern xAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
        Matcher mForX = xAxis.matcher(xaxis.getText().toString());

        Pattern yAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
        Matcher mForY = yAxis.matcher(yaxis.getText().toString());

        Pattern zAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
        Matcher mForZ = zAxis.matcher(zaxis.getText().toString());

        //If Axis X and Axis Y and Axis Z are all valid entries, the proceed.
        if(mForX.find() && mForY.find() && mForZ.find()){
             //handle insert or update statement here
        }else{
             //give error message regarding correct Axis value format
        }
   }

}

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
EBM
  • 719
  • 1
  • 5
  • 9
-3

This is working for me.

etbox.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);