0

I have an android phone using googles keyboard. On any EditText field in any other app if I use the swipe method to enter text in, it adds a space after each word. However, I have written my own app and when I use the swipe method to enter text on my EditText field it does NOT add a space sothewordsbleedtogether. This is very annoying.

I have an AlertDialog with a linear view added. On that linear view there is a text EditText. Here is my code to create the EditText and add it to the view:

final EditText titleBox = new EditText(this);
titleBox.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT |
    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
    InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
titleBox.setHint("Title");
layout.addView(titleBox);

Any ideas why its not adding spaces in between my words?

This was marked as a possible duplicate, but that question was about not allowing the first character to be a space....Im asking about allowing a space after words that are entered via a keyboard swipe.

Update

Here is the entire method of similar page, its having the same issue, its slightly less complex then the initial page I was troubleshooting. This one doesn't even have a LinearLayout associated:

private void addBudget(final Budget budget) {
EditText taskEditText = new EditText(this);
        taskEditText.setId(R.id.add_budget_text);
             taskEditText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

String dialogTitle = "Add budget";
final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(dialogTitle)
                .setMessage("What do you want to call this budget?")
                .setView(taskEditText)
                .setPositiveButton("Save", new    DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // final String task = ;
                        SQLiteDatabase db = mHelper.getWritableDatabase();


                        Budget lBudget = new Budget();
                        if (budget != null) {
                            lBudget = budget;
                        }
                        EditText taskEditText = (EditText) findViewById(R.id.add_budget_text);
                        lBudget.title = String.valueOf(taskEditText.getText());

                        // Init custom budget object //new Budget(){{ title=task; id = budgetID;}}
                        int retId = mHelper.saveBudget(db, lBudget);

                        db.close();
                        int retRow = updateUI(retId);
                        mTaskListView.smoothScrollToPosition(retRow);

                    }
                })
                .setNegativeButton("Cancel", null)
                .create();
// Handle done on soft keyboard
        taskEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                int result = actionId & EditorInfo.IME_MASK_ACTION;
                if (result == EditorInfo.IME_ACTION_DONE) {
                    dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
                    return true;
                }
                return false;
            }
        });
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        dialog.show();

    }
Todd Horst
  • 853
  • 10
  • 22
  • what do you meant by swipe text in? – Andrew Indayang May 18 '16 at 00:28
  • i think you can check this out http://stackoverflow.com/a/19514175/5319409 hope it helps – Andrew Indayang May 18 '16 at 00:29
  • Possible duplicate of [Android EditText Space Validation](http://stackoverflow.com/questions/19511075/android-edittext-space-validation) – Andrew Indayang May 18 '16 at 00:36
  • By swipe i mean, you can either tap keys, or use a swiping motion to enter text into a field. Of course you can manually type a space key, but when swiping it takes care of the spaces in between the words for you. In my app swiping does not enter the space key. – Todd Horst May 18 '16 at 01:56
  • @AndrewIndayang this is not a duplicate. That linked question was trying to not allow spaces as the first character being entered. This is almost the opposite. I need to allow spaces to be added in between words that are entered using a swiping motion on the google keyboard. Every other app ive tried on my phone works, but i can get it to work in my own app. – Todd Horst May 18 '16 at 01:58

3 Answers3

1

I didnt know if you got solved, i just had the same problem today and found a way to solve it.

I saw a "extrange" performance of the swipe, sometimes it showed the "blankspace" and sometimes not.

The way i found to check if it was shown and if it didnt, add it, was this:

        editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            checkCancel();
            int compare = count-before;
            if(compare>1){
                String text = editText.getText().toString();
                String lastChar = (text.substring(text.length()-1,text.length()));
                if(!lastChar.equals(" ")){
                    String plus = text+" ";
                    editText.setText(plus);
                    editText.setSelection(editText.getText().length());
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }

        } );

You can see, onTextChanged can use the variables "before" and "count" and if the compare (difference between last word and current one) is more than 1, it's a word entered by Swipe. Then you can check if the "blankspace" is shown, and if not, just add it and perfom anything you want with it.

Hope it helps!

Mikiodelg
  • 11
  • 4
0

Could you try this? Add the filter into the editText. I used it for enter code on my app.

EditText et = (EditText) findViewById(R.id.edit_text);
et.setFilters(new InputFilter[] {
new InputFilter() {
    @Override
    public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int start, int end {
           if(charSequence.equals("")){
                return charSequence;
           }
           if(charSequence.toString().matches("[a-zA-Z ]+")){
                return charSequence;
           }
           return "";
        }
    }
});
  • Unfortunately, that didnt have any effect. I also tried to change the EditText to not be final, that didnt work either – Todd Horst May 18 '16 at 11:40
0

So I uninstalled the google keyboard and reinstalled and I changed the build to release. One of those two things fixed it.

Todd Horst
  • 853
  • 10
  • 22