Example usage for android.text Editable length

List of usage examples for android.text Editable length

Introduction

In this page you can find the example usage for android.text Editable length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:br.ufrgs.ufrgsmapas.libs.SearchBox.java

private void openSearch(Boolean openKeyboard) {
    this.materialMenu.animateState(IconState.ARROW);
    this.logo.setVisibility(View.GONE);
    this.drawerLogo.setVisibility(View.GONE);
    this.search.setVisibility(View.VISIBLE);
    search.requestFocus();/*from ww  w.java 2s . c o  m*/
    this.results.setVisibility(View.VISIBLE);
    animate = true;
    results.setAdapter(new SearchAdapter(context, resultList));
    search.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() > 0) {
                micStateChanged(false);
                mic.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_clear));
                updateResults();
            } else {
                micStateChanged(true);
                mic.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_mic));
                if (initialResults != null) {
                    setInitialResults();
                } else {
                    updateResults();
                }
            }

            if (listener != null)
                listener.onSearchTermChanged();
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

    });
    results.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            SearchResult result = resultList.get(arg2);
            search(result);

        }

    });
    if (initialResults != null) {
        setInitialResults();
    } else {
        updateResults();
    }

    if (listener != null)
        listener.onSearchOpened();
    if (getSearchText().length() > 0) {
        micStateChanged(false);
        mic.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_clear));
    }
    if (openKeyboard) {
        InputMethodManager inputMethodManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(getApplicationWindowToken(),
                InputMethodManager.SHOW_FORCED, 0);
    }
}

From source file:com.forrestguice.suntimeswidget.settings.ColorChooser.java

@Override
public void afterTextChanged(Editable editable) {
    if (isRunning || isRemoving)
        return;//from   ww w .j a  va2 s.c  om
    isRunning = true;

    String text = editable.toString(); // should consist of [#][0-9][a-f]
    for (int j = text.length() - 1; j >= 0; j--) {
        if (!inputSet.contains(text.charAt(j))) {
            editable.delete(j, j + 1);
        }
    }

    text = editable.toString(); // should start with a #
    int i = text.indexOf('#');
    if (i != -1) {
        editable.delete(i, i + 1);
    }
    editable.insert(0, "#");

    if (editable.length() > 8) // should be no longer than 8
    {
        editable.delete(9, editable.length());
    }

    text = editable.toString();
    String toCaps = text.toUpperCase(Locale.US);
    editable.clear();
    editable.append(toCaps);

    isRunning = false;
}

From source file:com.pitchedapps.primenumbercalculator.Calculator.java

License:asdf

private void onDelete() {
    // Delete works like backspace; remove the last character from the expression.
    final Editable inputText = mInputEditText.getEditableText();
    final int inputLength = inputText.length();
    if (inputLength > 0) {
        inputText.delete(inputLength - 1, inputLength);
    }//www  .j ava 2s .c om
}

From source file:org.eyeseetea.malariacare.network.CustomParser.java

@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
    if (tag.equalsIgnoreCase("ul")) {
        if (opening) {
            lists.push(tag);//from  w ww  .j  av a 2s .  c  om
        } else {
            lists.pop();
        }
    } else if (tag.equalsIgnoreCase("ol")) {
        if (opening) {
            lists.push(tag);
            olNextIndex.push(Integer.valueOf(1)).toString();//TODO: add support for lists starting other index than 1
        } else {
            lists.pop();
            olNextIndex.pop().toString();
        }
    } else if (tag.equalsIgnoreCase("li")) {
        if (opening) {
            if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                output.append("\n");
            }
            String parentList = lists.peek();
            if (parentList.equalsIgnoreCase("ol")) {
                start(output, new Ol());
                output.append(olNextIndex.peek().toString() + ". ");
                olNextIndex.push(Integer.valueOf(olNextIndex.pop().intValue() + 1));
            } else if (parentList.equalsIgnoreCase("ul")) {
                start(output, new Ul());
            }
        } else {
            if (lists.peek().equalsIgnoreCase("ul")) {
                if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                    output.append("\n");
                }
                // Nested BulletSpans increases distance between bullet and text, so we must prevent it.
                int bulletMargin = indent;
                if (lists.size() > 1) {
                    bulletMargin = indent - bullet.getLeadingMargin(true);
                    if (lists.size() > 2) {
                        // This get's more complicated when we add a LeadingMarginSpan into the same line:
                        // we have also counter it's effect to BulletSpan
                        bulletMargin -= (lists.size() - 2) * listItemIndent;
                    }
                }
                BulletSpan newBullet = new BulletSpan(bulletMargin);
                end(output, Ul.class, new LeadingMarginSpan.Standard(listItemIndent * (lists.size() - 1)),
                        newBullet);
            } else if (lists.peek().equalsIgnoreCase("ol")) {
                if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                    output.append("\n");
                }
                int numberMargin = listItemIndent * (lists.size() - 1);
                if (lists.size() > 2) {
                    // Same as in ordered lists: counter the effect of nested Spans
                    numberMargin -= (lists.size() - 2) * listItemIndent;
                }
                end(output, Ol.class, new LeadingMarginSpan.Standard(numberMargin));
            }
        }
    } else {
        if (opening)
            Log.d("TagHandler", "Found an unsupported tag " + tag);
    }
}

From source file:com.csipsimple.ui.dialpad.DialerFragment.java

/**
 * Set the value of the text field and put caret at the end
 * //from   w  w w . j  av a2 s.  c o  m
 * @param value the new text to see in the text field
 */
public void setTextFieldValue(CharSequence value) {
    if (digits == null) {
        initText = value.toString();
        return;
    }
    digits.setText(value);
    // make sure we keep the caret at the end of the text view
    Editable spannable = digits.getText();
    Selection.setSelection(spannable, spannable.length());
}

From source file:org.thoughtcrime.securesms.GroupCreateActivity.java

private void initializeResources() {
    groupRecipient = RecipientFactory.getRecipientForId(this,
            getIntent().getLongExtra(GROUP_RECIPIENT_EXTRA, -1), true);
    groupThread = getIntent().getLongExtra(GROUP_THREAD_EXTRA, -1);
    if (groupRecipient != null) {
        final String encodedGroupId = groupRecipient.getNumber();
        if (encodedGroupId != null) {
            try {
                groupId = GroupUtil.getDecodedId(encodedGroupId);
            } catch (IOException ioe) {
                Log.w(TAG, "Couldn't decode the encoded groupId passed in via intent", ioe);
                groupId = null;/* w  ww  .j  a v a  2 s .  c om*/
            }
            if (groupId != null) {
                new FillExistingGroupInfoAsyncTask().execute();
            }
        }
    }

    lv = (ListView) findViewById(R.id.selected_contacts_list);
    avatar = (ImageView) findViewById(R.id.avatar);
    groupName = (EditText) findViewById(R.id.group_name);
    creatingText = (TextView) findViewById(R.id.creating_group_text);
    recipientsPanel = (PushRecipientsPanel) findViewById(R.id.recipients);

    groupName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            final int prefixResId = (groupId != null) ? R.string.GroupCreateActivity_actionbar_update_title
                    : R.string.GroupCreateActivity_actionbar_title;
            if (editable.length() > 0) {
                getSupportActionBar().setTitle(getString(prefixResId) + ": " + editable.toString());
            } else {
                getSupportActionBar().setTitle(prefixResId);
            }
        }
    });

    SelectedRecipientsAdapter adapter = new SelectedRecipientsAdapter(this, android.R.id.text1,
            new ArrayList<SelectedRecipientsAdapter.RecipientWrapper>());
    adapter.setOnRecipientDeletedListener(new SelectedRecipientsAdapter.OnRecipientDeletedListener() {
        @Override
        public void onRecipientDeleted(Recipient recipient) {
            removeSelectedContact(recipient);
        }
    });
    lv.setAdapter(adapter);

    recipientsPanel.setPanelChangeListener(new PushRecipientsPanel.RecipientsPanelChangedListener() {
        @Override
        public void onRecipientsPanelUpdate(Recipients recipients) {
            Log.i(TAG, "onRecipientsPanelUpdate received.");
            if (recipients != null) {
                addAllSelectedContacts(recipients.getRecipientsList());
                syncAdapterWithSelectedContacts();
            }
        }
    });
    (findViewById(R.id.contacts_button)).setOnClickListener(new AddRecipientButtonListener());

    avatar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Crop.pickImage(GroupCreateActivity.this);
        }
    });

    ((RecipientsEditor) findViewById(R.id.recipients_text)).setHint(R.string.recipients_panel__add_member);
}

From source file:org.mozilla.focus.widget.InlineAutocompleteEditText.java

/**
 * Remove any autocomplete text//from  w  w  w . ja va2s.c  o m
 *
 * @param text Current text content that may include autocomplete text
 */
private boolean removeAutocomplete(final Editable text) {
    final int start = text.getSpanStart(AUTOCOMPLETE_SPAN);
    if (start < 0) {
        // No autocomplete text
        return false;
    }

    beginSettingAutocomplete();

    // When we call delete() here, the autocomplete spans we set are removed as well.
    text.delete(start, text.length());

    // Keep mAutoCompletePrefixLength the same because the prefix has not changed.
    // Clear mAutoCompleteResult to make sure we get fresh autocomplete text next time.
    mAutoCompleteResult = "";

    // Reshow the cursor.
    setCursorVisible(true);

    endSettingAutocomplete();
    return true;
}

From source file:android.support.text.emoji.EmojiProcessor.java

/**
 * Handles deleteSurroundingText commands from {@link InputConnection} and tries to delete an
 * {@link EmojiSpan} from an {@link Editable}. Returns {@code true} if an {@link EmojiSpan} is
 * deleted./*  ww w  .ja va2 s  .  com*/
 * <p/>
 * If there is a selection where selection start is not equal to selection end, does not
 * delete.
 *
 * @param inputConnection InputConnection instance
 * @param editable TextView.Editable instance
 * @param beforeLength the number of characters before the cursor to be deleted
 * @param afterLength the number of characters after the cursor to be deleted
 * @param inCodePoints {@code true} if length parameters are in codepoints
 *
 * @return {@code true} if an {@link EmojiSpan} is deleted
 */
static boolean handleDeleteSurroundingText(@NonNull final InputConnection inputConnection,
        @NonNull final Editable editable, @IntRange(from = 0) final int beforeLength,
        @IntRange(from = 0) final int afterLength, final boolean inCodePoints) {
    //noinspection ConstantConditions
    if (editable == null || inputConnection == null) {
        return false;
    }

    if (beforeLength < 0 || afterLength < 0) {
        return false;
    }

    final int selectionStart = Selection.getSelectionStart(editable);
    final int selectionEnd = Selection.getSelectionEnd(editable);

    if (hasInvalidSelection(selectionStart, selectionEnd)) {
        return false;
    }

    int start;
    int end;
    if (inCodePoints) {
        // go backwards in terms of codepoints
        start = CodepointIndexFinder.findIndexBackward(editable, selectionStart, Math.max(beforeLength, 0));
        end = CodepointIndexFinder.findIndexForward(editable, selectionEnd, Math.max(afterLength, 0));

        if (start == CodepointIndexFinder.INVALID_INDEX || end == CodepointIndexFinder.INVALID_INDEX) {
            return false;
        }
    } else {
        start = Math.max(selectionStart - beforeLength, 0);
        end = Math.min(selectionEnd + afterLength, editable.length());
    }

    final EmojiSpan[] spans = editable.getSpans(start, end, EmojiSpan.class);
    if (spans != null && spans.length > 0) {
        final int length = spans.length;
        for (int index = 0; index < length; index++) {
            final EmojiSpan span = spans[index];
            int spanStart = editable.getSpanStart(span);
            int spanEnd = editable.getSpanEnd(span);
            start = Math.min(spanStart, start);
            end = Math.max(spanEnd, end);
        }

        start = Math.max(start, 0);
        end = Math.min(end, editable.length());

        inputConnection.beginBatchEdit();
        editable.delete(start, end);
        inputConnection.endBatchEdit();
        return true;
    }

    return false;
}

From source file:dev.drsoran.moloko.fragments.ChangeTagsFragment.java

private void onListItemClick(ListView l, View v, int position, long id) {
    final ChangeTag tag = (ChangeTag) l.getAdapter().getItem(position);
    final Editable tagsEdit = editView.getEditableText();

    if (tag.isAvailable) {
        if (TextUtils.isEmpty(tagsEdit)) {
            tagsEdit.append(tag.tag);//from w w  w.  j a  va  2  s. com
        } else {
            final int trimmedLength = TextUtils.getTrimmedLength(tagsEdit);
            if (tagsEdit.charAt(trimmedLength - 1) == ',') {
                tagsEdit.append(tag.tag);
            } else {
                tagsEdit.append(", " + tag.tag);
            }
        }
    } else {
        // Cut the removed tag including any trailing ,
        String content = UIUtils.getTrimmedText(editView);
        content = content.replaceAll(tag.tag + "\\,*\\s*", Strings.EMPTY_STRING);

        editView.setText(content);
    }

    Selection.setSelection(tagsEdit, tagsEdit.length());
    updateTagList();
}

From source file:org.mariotaku.twidere.fragment.support.MessagesConversationFragment.java

private void setupEditText() {
    EditTextEnterHandler.attach(mEditText, new EnterListener() {
        @Override/*from   w  ww.  ja  v  a2s  . c  o m*/
        public void onHitEnter() {
            sendDirectMessage();
        }
    }, mPreferences.getBoolean(KEY_QUICK_SEND, false));
    mEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(final Editable s) {
            Utils.removeLineBreaks(s);
            mTextChanged = s.length() == 0;
        }

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

        }

        @Override
        public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
            if (mSendButton == null || s == null)
                return;
            mSendButton.setEnabled(mValidator.isValidTweet(s.toString()));
        }
    });
}