3

I'm writing the application that has to display to a user a dynamically created table of EditText widgets.

My current code:

      /**
      * Create and display the table of EditTexts on the screen
      * @param x Number of columns
      * @param y Number of rows
      */
        private void createTable(int x, int y) {
            TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
            etGrid = new EditText[y][x];
            for (int i = 0; i < y; i++) {
                TableRow row = new TableRow(this);
                for (int j = 0; j < x; j++) {
                    EditText et = new EditText(this);

                    et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL 
                              | InputType.TYPE_NUMBER_FLAG_SIGNED);

                    et.setSingleLine();
                    et.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth()/x);
                    etGrid[i][j] = et;
                    row.addView(et);
                }
                table.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            }

        }

The problem is in this line, which doesn't work at all:

et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL 
                                  | InputType.TYPE_NUMBER_FLAG_SIGNED);

On my Kindle Fire I can either restrict to decimal values with InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL or signed with InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED, but not to signed decimal. What is the problem with the code?

bvk256
  • 1,837
  • 3
  • 20
  • 38
  • 2
    I don't really see any problem with your code. You could try to use .setRawInputType(); instead but if that also doesn't work its probably something Amazone screwed up with the Kindle – Stefan de Bruijn Jan 09 '13 at 15:57

3 Answers3

11

Try this:

et.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);

Reference : setRawInputType

EDIT:

I tried this and it works for me.

Just be sure with this :

You have to remove this attribute android:inputType from your et edittext from the xml layout file. Otherwise setRawInputType changes will not be reflected.

Thanks.

Community
  • 1
  • 1
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • This generally works, but minus and dot get inserted as unknown characters into an EditText. – bvk256 Jan 09 '13 at 16:47
  • @bvk256 please see the edit with the solution I have posted. This works for me perfectly. – Pratik Sharma Jan 10 '13 at 10:31
  • There is no definition of any EditText in the XML layout file since all of them are created dynamically during the runtime. – bvk256 Jan 11 '13 at 15:50
11

It is extremely important to ALWAYS have InputType.TYPE_CLASS_NUMBER before the other options: InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL, or else the keypad won't be numeric!

Robert
  • 5,278
  • 43
  • 65
  • 115
crina
  • 335
  • 4
  • 7
0

This indeed turned out to be a bug in Amazon's Android. Since I'm writing the application for myself there is a little workaround that perfectly suited me:

  1. Set InputType.TYPE_CLASS_PHONE
  2. Implement a custom key filter to enforce the correct format. I've used this one.
Community
  • 1
  • 1
bvk256
  • 1,837
  • 3
  • 20
  • 38