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:com.todoroo.astrid.actfm.CommentsFragment.java

protected void setUpUpdateList() {
    final ImageButton commentButton = (ImageButton) getView().findViewById(R.id.commentButton);
    addCommentField = (EditText) getView().findViewById(R.id.commentField);
    addCommentField.setOnEditorActionListener(new OnEditorActionListener() {
        @Override/*from ww  w.j  av  a 2  s . c om*/
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_NULL && addCommentField.getText().length() > 0) {
                addComment();
                return true;
            }
            return false;
        }
    });
    addCommentField.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            commentButton.setVisibility((s.length() > 0) ? View.VISIBLE : View.GONE);
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //
        }
    });
    commentButton.setVisibility(TextUtils.isEmpty(addCommentField.getText()) ? View.GONE : View.VISIBLE);
    commentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addComment();
        }
    });

    final ClearImageCallback clearImage = new ClearImageCallback() {
        @Override
        public void clearImage() {
            picture = null;
            resetPictureButton();
        }
    };
    pictureButton = (ImageButton) getView().findViewById(R.id.picture);
    pictureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (picture != null) {
                ActFmCameraModule.showPictureLauncher(CommentsFragment.this, clearImage);
            } else {
                ActFmCameraModule.showPictureLauncher(CommentsFragment.this, null);
            }
        }
    });

    refreshUpdatesList();
}

From source file:org.adaway.ui.hosts.HostsSourcesFragment.java

/**
 * Add a hosts source./*from w  ww  . jav  a  2 s.c  o m*/
 */
private void addSource() {
    // Create dialog builder
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setTitle(R.string.hosts_add_dialog_title);
    builder.setCancelable(true);
    // Create dialog view
    LayoutInflater factory = LayoutInflater.from(mActivity);
    View view = factory.inflate(R.layout.hosts_sources_dialog, null);
    builder.setView(view);
    // Move cursor to end of EditText
    EditText inputEditText = view.findViewById(R.id.hosts_add_dialog_url);
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());
    // Setup buttons
    builder.setPositiveButton(R.string.button_add, (dialog, which) -> {
        String url = inputEditText.getText().toString();
        if (HostsSource.isValidUrl(url)) {
            // Insert hosts source into database
            this.mViewModel.addSourceFromUrl(url);
        }
        dialog.dismiss();
    });
    builder.setNegativeButton(R.string.button_cancel, (dialog, which) -> dialog.dismiss());
    // Display dialog
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    // Set button validation behavior
    inputEditText.addTextChangedListener(new AlertDialogValidator(alertDialog, HostsSource::isValidUrl, false));
}

From source file:org.miaowo.miaowo.util.Html.java

private static void startBlockElement(Editable text, Attributes attributes, int margin) {
    final int len = text.length();
    if (margin > 0) {
        appendNewlines(text, margin);/*from  ww w  .jav a 2s .c  o  m*/
        text.setSpan(new Newline(margin), len, len, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    }

    String style = attributes.getValue("", "style");
    if (style != null) {
        Matcher m = getTextAlignPattern().matcher(style);
        if (m.find()) {
            String alignment = m.group(1);
            if (alignment.equalsIgnoreCase("start")) {
                text.setSpan(new Alignment(Layout.Alignment.ALIGN_NORMAL), len, len,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            } else if (alignment.equalsIgnoreCase("center")) {
                text.setSpan(new Alignment(Layout.Alignment.ALIGN_CENTER), len, len,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            } else if (alignment.equalsIgnoreCase("end")) {
                text.setSpan(new Alignment(Layout.Alignment.ALIGN_OPPOSITE), len, len,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
    }
}

From source file:org.adaway.ui.hosts.HostsSourcesFragment.java

/**
 * Edit selected hosts source.//from   www  .ja  va2  s.  c  o m
 */
private void editSource() {
    // Check action source
    if (this.mActionSource == null) {
        return;
    }
    HostsSource editedSource = this.mActionSource;
    // Create dialog builder
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setTitle(R.string.hosts_edit_dialog_title);
    builder.setCancelable(true);
    // Create dialog view
    LayoutInflater factory = LayoutInflater.from(mActivity);
    View view = factory.inflate(R.layout.hosts_sources_dialog, null);
    builder.setView(view);
    // Set source URL
    EditText inputEditText = view.findViewById(R.id.hosts_add_dialog_url);
    inputEditText.setText(editedSource.getUrl());
    // Move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());
    // Setup buttons
    builder.setPositiveButton(getResources().getString(R.string.button_save), (dialog, which) -> {
        // Close dialog
        dialog.dismiss();
        // Finish action mode
        HostsSourcesFragment.this.mActionMode.finish();
        // Check url validity
        String url = inputEditText.getText().toString();
        if (HostsSource.isValidUrl(url)) {
            // Update hosts source into database
            this.mViewModel.updateSourceUrl(editedSource, url);
        }
    });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel), (dialog, which) -> {
        // Close dialog
        dialog.dismiss();
        // Finish action mode
        HostsSourcesFragment.this.mActionMode.finish();
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    // Set button validation behavior
    inputEditText.addTextChangedListener(new AlertDialogValidator(alertDialog, HostsSource::isValidUrl, true));
}

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.EnterStopCodeFragment.java

/**
 * {@inheritDoc}/*from   w w w.  j av  a 2s .  c  o  m*/
 */
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    // Inflate the View from the XML resource.
    final View v = inflater.inflate(R.layout.enterstopcode, container, false);

    // Get the UI elements.
    submitButton = (Button) v.findViewById(R.id.enterstopcode_submit);
    submitButton.setOnClickListener(this);

    scanButton = (Button) v.findViewById(R.id.enterstopcode_barcode_button);
    scanButton.setOnClickListener(this);

    txt = (EditText) v.findViewById(R.id.enterstopcode_entry);
    txt.setOnKeyListener(this);
    txt.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(final Editable s) {
            // Only enable the confirm button if the length is 8 chars.
            if (s.length() == 8) {
                submitButton.setEnabled(true);
            } else {
                submitButton.setEnabled(false);
            }
        }

        @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) {
        }
    });

    return v;
}

From source file:org.miaowo.miaowo.util.Html.java

private static void startA(Editable text, Attributes attributes) {
    String href = attributes.getValue("", "href");

    int len = text.length();
    text.setSpan(new Href(href), len, len, Spannable.SPAN_MARK_MARK);
}

From source file:com.example.gaurav.calculator.Calculator.java

private void onDelete() {
    // Delete works like backspace; remove the last character from the expression.
    final Editable formulaText = mFormulaEditText.getEditableText();
    final int formulaLength = formulaText.length();
    if (formulaLength > 0) {
        formulaText.delete(formulaLength - 1, formulaLength);
    }/*from  w  ww .  j a v a 2  s  . c  o  m*/
}

From source file:ru.orangesoftware.financisto.widget.AmountInput.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    requestId = EDIT_AMOUNT_REQUEST.incrementAndGet();

    LinearLayout v = new LinearLayout(getActivity());
    v.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams lpWrapWrap = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);//from   w  w w  .j a  v  a 2s.c  o  m
    lpWrapWrap.weight = 1;

    inflater.inflate(R.layout.amount_input, v, true);
    c = new Controls(v);

    c.AmountInput.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), QuickAmountInput.class);
            intent.putExtra(EXTRA_AMOUNT, getAbsAmountString());
            startActivityForResult(intent, requestId);
        }
    });

    c.CalculatorInput.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), CalculatorInput.class);
            intent.putExtra(EXTRA_AMOUNT, getAbsAmountString());
            startActivityForResult(intent, requestId);
        }
    });

    c.primary.setKeyListener(keyListener);
    c.primary.addTextChangedListener(textWatcher);
    c.primary.setOnFocusChangeListener(selectAllOnFocusListener);

    c.secondary.setKeyListener(new DigitsKeyListener(false, false) {

        @Override
        public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                if (content.length() == 0) {
                    c.primary.requestFocus();
                    int pos = c.primary.getText().length();
                    c.primary.setSelection(pos, pos);
                    return true;
                }
            }
            return super.onKeyDown(view, content, keyCode, event);
        }

        @Override
        public int getInputType() {
            return InputType.TYPE_CLASS_PHONE;
        }

    });
    c.secondary.addTextChangedListener(textWatcher);
    c.secondary.setOnFocusChangeListener(selectAllOnFocusListener);

    return v;
}

From source file:org.adaway.ui.BlacklistFragment.java

/**
 * Add Entry Menu Action/*from   www . j a  v  a 2s  . c o  m*/
 */
public void menuAddEntry() {
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_add_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_hostname_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_hostname);

    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_add),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();
                    addEntry(input);
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:org.adaway.ui.WhitelistFragment.java

/**
 * Add Entry Menu Action/*  w  w w . j  a v a 2 s .c  o m*/
 */
public void menuAddEntry() {
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_add_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_whitelist_hostname_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_hostname);

    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_add),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();
                    addEntry(input);
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}