Martin
Martin

Reputation: 4836

Android InputType not honored

I have a listview with an EditText in each row. The EditText is supposed to collect numeric values so I tried to keep the alpha keyboard off and allow the user to only see the numeric keypad.

What happens, though, is that when the listview is populated and I click on any EditText in the list, the numeric keyboard appears - but is then quickly replaced by the full qwerty keyboard - and the user must switch back to numeric to input numbers.

I've tried various combinations programmatically, like so:

mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);

As well as multiple comnbinations of XML such as

android:inputType="phone"

or

android:digits="0123456789"

But, no matter the combination, they keyboard always rapidly switches back to qwerty and off the numeric keys.

I'm thinking it might be something with the TextWatcher. I use a textwatcher to indicate what has been typed. I remove it, add whatever text might be in that row's edittext and then add the textwatcher.

This is the guts of my custom adapter :

public View getView(final int position, View inView, ViewGroup parent) {
        ViewHolder holder = null;

        if (inView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inView = inflater.inflate(mLayoutResource, null);
            holder = new ViewHolder();
            holder.mPercent = (EditText) inView.findViewById(R.id.percent);
            holder.mMaterial = (TextView) inView.findViewById(R.id.name);
            inView.setTag(holder);
        } else {
            holder = (ViewHolder) inView.getTag();
        }

        mMaterial.moveToPosition(position);

        holder.mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);

// remove textwatcher on percentage
        holder.mPercent.removeTextChangedListener(percentWatcher);

        // insert the percentage for this row
        final String material = mMaterial.getString(mMaterial.getColumnIndex(OptionsProvider.OPTIONS_DATA));
        final EditText percent = holder.mPercent;

        percent.setTag(material);   // use material and position as keys into which of the many edittexts the textwatcher is watching
        percent.setId(position);

        holder.mMaterial.setText(material);

        // persist any text the user may have typed in the comment box
        if (percentages.containsKey(material)) {
            percent.setText(percentages.get(material));
        }
        else {
            percent.setText("");
        }

        // turn on textwatcher
        percentWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // save this text and the position in which is resides.
                // if the user scrolls before pressing send, only this position should
                // contain the text
                String text = percent.getText().toString().trim();
                if (text.length() > 0) {
                    if ((position == percent.getId() &&
                            (material.equals( percent.getTag().toString())))) {
                        percentages.put(material, text);
                        Log.d(TAG, "Percentage inserted "+material+"="+text);
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

            // force the EditText to stay with 3 digits (max of 100%)
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    int pct = Integer.parseInt(s.toString());
                    Log.d(TAG, " Material "+(String) percent.getTag()+ " = "+pct);
                }
            }
        };

        // add the textwatcher back to the edittext
        holder.mPercent.addTextChangedListener(percentWatcher);

Anyone know if removing/adding a textwatcher corrupts the InputType? Is this a path I should explore to figure out why the keyboard always goes back to qwerty?

Upvotes: 2

Views: 185

Answers (1)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

Might be that the ListView steals the focus. Try to add this your ListView

XML

android:descendantFocusability="afterDescendants"

Or in Java

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

Upvotes: 1

Related Questions