Example usage for android.widget EditText setSelection

List of usage examples for android.widget EditText setSelection

Introduction

In this page you can find the example usage for android.widget EditText setSelection.

Prototype

public void setSelection(int index) 

Source Link

Document

Convenience for Selection#setSelection(Spannable,int) .

Usage

From source file:indrora.atomic.activity.ConversationActivity.java

/**
 * On menu item selected/*from  w w  w. j av  a 2  s.c o m*/
 */
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        break;

    case R.id.disconnect:
        server.setStatus(Status.DISCONNECTED);
        server.setMayReconnect(false);
        binder.getService().getConnection(serverId).quitServer();
        server.clearConversations();
        setResult(RESULT_OK);
        finish();
        break;

    case R.id.close:
        Conversation conversationToClose = pagerAdapter.getItem(pager.getCurrentItem());
        // Make sure we part a channel when closing the channel conversation
        if (conversationToClose.getType() == Conversation.TYPE_CHANNEL) {
            binder.getService().getConnection(serverId).partChannel(conversationToClose.getName());
        } else if (conversationToClose.getType() == Conversation.TYPE_QUERY) {
            server.removeConversation(conversationToClose.getName());
            onRemoveConversation(conversationToClose.getName());
        } else {
            Toast.makeText(this, getResources().getString(R.string.close_server_window), Toast.LENGTH_SHORT)
                    .show();
        }
        break;

    case R.id.join:
        startActivityForResult(new Intent(this, JoinActivity.class), REQUEST_CODE_JOIN);
        break;

    /* Get users in the channel. */
    case R.id.users:
        Conversation conversationForUserList = pagerAdapter.getItem(pager.getCurrentItem());
        if (conversationForUserList.getType() == Conversation.TYPE_CHANNEL) {

            final String[] nicks = binder.getService().getConnection(server.getId())
                    .getUsersAsStringArray(conversationForUserList.getName());

            final Context _tContext = (Context) this;

            AlertDialog.Builder userlistBuilder = new AlertDialog.Builder(_tContext);

            userlistBuilder.setTitle("Users");

            OnClickListener NickSelectorListener = new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    final String nick = nicks[which];
                    // This is the OnClickListener to actually do something.

                    OnClickListener NickActionListener = new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            /* ********************* */

                            String nicknameWithoutPrefix = removeStatusChar(nick);

                            final IRCConnection connection = binder.getService().getConnection(server.getId());
                            final String conversation = server.getSelectedConversation();

                            switch (which) {
                            case 0:
                                final String replyText = nicknameWithoutPrefix + ": ";
                                /*
                                 * handler.post(new Runnable() {
                                 *
                                 * @Override public void run() {
                                 */
                                EditText input = (EditText) findViewById(R.id.input);
                                input.setText(replyText);
                                input.setSelection(replyText.length());
                                openSoftKeyboard(input);
                                input.requestFocus();

                                // }
                                // });
                                break;
                            case 1:
                                Conversation query = server.getConversation(nicknameWithoutPrefix);
                                if (query == null) {
                                    // Open a query if there's none yet
                                    query = new Query(nicknameWithoutPrefix);
                                    query.setHistorySize(binder.getService().getSettings().getHistorySize());
                                    server.addConversation(query);

                                    Intent intent = Broadcast.createConversationIntent(
                                            Broadcast.CONVERSATION_NEW, server.getId(), nicknameWithoutPrefix);
                                    binder.getService().sendBroadcast(intent);
                                }
                                break;
                            case 2:
                                connection.op(conversation, nicknameWithoutPrefix);
                                break;
                            case 3:
                                connection.deOp(conversation, nicknameWithoutPrefix);
                                break;
                            case 4:
                                connection.voice(conversation, nicknameWithoutPrefix);
                                break;
                            case 5:
                                connection.deVoice(conversation, nicknameWithoutPrefix);
                                break;
                            case 6:
                                connection.kick(conversation, nicknameWithoutPrefix);
                                break;
                            case 7:
                                connection.ban(conversation, nicknameWithoutPrefix + "!*@*");
                                break;
                            }

                            /* ********************* */

                        }
                    }; // <-- Thats all for the actions listener.

                    AlertDialog.Builder ActionMenuBuilder = new Builder(_tContext);

                    ActionMenuBuilder.setItems(R.array.user_actions, NickActionListener);

                    ActionMenuBuilder.show();

                }
            };

            userlistBuilder.setItems(nicks, NickSelectorListener);
            userlistBuilder.show();

        } else {
            Toast.makeText(this, getResources().getString(R.string.only_usable_from_channel),
                    Toast.LENGTH_SHORT).show();
        }
        break;
    /* Choose a conversation option. */
    case R.id.chooseConversation:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose Conversation");
        String[] conversationsArr = new String[pagerAdapter.getCount()];
        for (int i = 0; i < pagerAdapter.getCount(); i++) {
            conversationsArr[i] = pagerAdapter.getPageTitle(i);
        }

        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Change the page here.
                pager.setCurrentItem(which);
            }
        };

        builder.setItems(conversationsArr, listener);
        builder.show();

        break;

    }

    return true;
}

From source file:com.github.irshulx.Components.InputExtensions.java

public void insertLink(String uri) {
    EditorType editorType = editorCore.getControlType(editorCore.getActiveView());
    EditText editText = (EditText) editorCore.getActiveView();
    if (editorType == EditorType.INPUT || editorType == EditorType.UL_LI) {
        String text = Html.toHtml(editText.getText());
        if (TextUtils.isEmpty(text))
            text = "<p dir=\"ltr\"></p>";
        text = trimLineEnding(text);//from  www  .ja  va2 s . com
        Document _doc = Jsoup.parse(text);
        Elements x = _doc.select("p");
        String existing = x.get(0).html();
        x.get(0).html(existing + " <a href='" + uri + "'>" + uri + "</a>");
        Spanned toTrim = Html.fromHtml(x.toString());
        CharSequence trimmed = noTrailingwhiteLines(toTrim);
        editText.setText(trimmed); //
        editText.setSelection(editText.getText().length());
    }
}

From source file:indrora.atomic.activity.ConversationActivity.java

/**
 * Insert a given nick completion into the input line
 *
 * @param input//from w w w .j ava 2  s.c o m
 *            The input line widget, with the incomplete nick selected
 * @param nick
 *            The completed nick
 */
private void insertNickCompletion(EditText input, String nick) {
    int start = input.getSelectionStart();
    int end = input.getSelectionEnd();
    nick = removeStatusChar(nick);

    if (start == 0) {
        nick += ":";
    }

    nick += " ";
    input.getText().replace(start, end, nick, 0, nick.length());
    // put cursor after inserted text
    input.setSelection(start + nick.length());
    input.clearComposingText();
    input.post(new Runnable() {
        @Override
        public void run() {
            // make the softkeyboard come up again (only if no hw keyboard
            // is attached)
            EditText input = (EditText) findViewById(R.id.input);
            openSoftKeyboard(input);
        }
    });

    input.requestFocus();
}

From source file:be.brunoparmentier.wifikeyshare.ui.activities.WifiNetworkActivity.java

void showWifiPasswordDialog() {
    final LayoutInflater inflater = getLayoutInflater();
    final View wifiPasswordDialogLayout = inflater.inflate(R.layout.dialog_wifi_password, null);

    final TextInputLayout wifiPasswordWrapper = (TextInputLayout) wifiPasswordDialogLayout
            .findViewById(R.id.wifi_key_wrapper);
    final EditText passwordEditText = (EditText) wifiPasswordDialogLayout.findViewById(R.id.wifi_key);
    //setPasswordRestrictions(passwordEditText);
    final CheckBox showPasswordCheckBox = (CheckBox) wifiPasswordDialogLayout
            .findViewById(R.id.show_password_checkbox);
    showPasswordCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override/*  w  w  w .j  a  va 2  s .c  o  m*/
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            int selectionIndex = passwordEditText.getSelectionStart();
            if (isChecked) {
                passwordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                passwordEditText
                        .setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
            passwordEditText.setSelection(selectionIndex);
        }
    });

    final AlertDialog wifiPasswordDialog = new AlertDialog.Builder(this)
            .setTitle(getString(R.string.wifi_dialog_password_title))
            .setMessage(String.format(getString(R.string.wifi_dialog_password_msg), wifiNetwork.getSsid()))
            .setView(wifiPasswordDialogLayout)
            .setPositiveButton(getString(R.string.action_ok), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // this method gets overriden after we show the dialog
                }
            }).setNegativeButton(getString(R.string.action_cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            }).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    finish();
                }
            }).create();

    passwordEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable editable) {
            wifiPasswordDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(editable.length() >= 5);
            if (wifiPasswordWrapper.getError() != null) {
                try {
                    if (WifiNetwork.isValidKeyLength(wifiNetwork.getAuthType(), editable.toString())) {
                        wifiPasswordWrapper.setError(null);
                    }
                } catch (final WifiException e) {
                    switch (e.getErrorCode()) {
                    case WifiException.WEP_KEY_LENGTH_ERROR:
                        wifiPasswordWrapper.setError(getString(R.string.error_wep_password_length));
                        break;
                    case WifiException.WPA_KEY_LENGTH_ERROR:
                        wifiPasswordWrapper.setError(getString(R.string.error_wpa_password_length));
                        break;
                    default:
                        wifiPasswordWrapper.setError(e.getMessage());
                        break;
                    }
                }
            }
        }
    });

    wifiPasswordDialog.show();

    wifiPasswordDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); // disabled by default
    wifiPasswordDialog.getButton(DialogInterface.BUTTON_POSITIVE)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        if (WifiNetwork.isValidKeyLength(wifiNetwork.getAuthType(),
                                passwordEditText.getText().toString())) {

                            wifiPasswordWrapper.setError(null);
                            wifiNetwork.setKey(passwordEditText.getText().toString());

                            // Update QR code image
                            FragmentManager fm = getSupportFragmentManager();
                            QrCodeFragment qrCodeFragment = (QrCodeFragment) fm.getFragments().get(0);
                            qrCodeFragment.updateQrCode(wifiNetwork);

                            WifiKeysDataSource.getInstance().insertWifiKey(wifiNetwork);

                            Intent passwordResultIntent = new Intent();
                            passwordResultIntent.putExtra(KEY_NETWORK_ID, wifiNetworkId);
                            setResult(RESULT_OK, passwordResultIntent);

                            wifiPasswordDialog.dismiss();
                        }
                    } catch (WifiException e) {
                        switch (e.getErrorCode()) {
                        case WifiException.WEP_KEY_LENGTH_ERROR:
                            wifiPasswordWrapper.setError(getString(R.string.error_wep_password_length));
                            break;
                        case WifiException.WPA_KEY_LENGTH_ERROR:
                            wifiPasswordWrapper.setError(getString(R.string.error_wpa_password_length));
                            break;
                        default:
                            wifiPasswordWrapper.setError(null);
                            break;
                        }
                    }
                }
            });
}

From source file:org.anhonesteffort.flock.SubscriptionStripeFragment.java

private void initCardExpirationHelper() {
    final EditText cardExpirationView = (EditText) subscriptionActivity.findViewById(R.id.card_expiration);
    final EditText cardCvcView = (EditText) subscriptionActivity.findViewById(R.id.card_cvc);

    if (cardExpirationTextWatcher != null)
        cardExpirationView.removeTextChangedListener(cardExpirationTextWatcher);

    cardExpirationTextWatcher = new TextWatcher() {

        @Override/*from  ww w.  ja v  a 2s  . c o  m*/
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            String formattedCardExpiration = s.toString();

            if (lastCardExpirationLength <= formattedCardExpiration.length()
                    && formattedCardExpiration.length() == 2) {
                formattedCardExpiration = formattedCardExpiration + "/";
            }

            lastCardExpirationLength = formattedCardExpiration.length();

            cardExpirationView.removeTextChangedListener(this);
            cardExpirationView.setText(formattedCardExpiration);
            cardExpirationView.setSelection(formattedCardExpiration.length());
            cardExpirationView.addTextChangedListener(this);

            if (formattedCardExpiration.length() == 5)
                cardCvcView.requestFocus();
        }
    };

    cardExpirationView.addTextChangedListener(cardExpirationTextWatcher);
}

From source file:com.mishiranu.dashchan.ui.navigator.DrawerForm.java

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    if (chanSelectMode) {
        return listView.startSorting(2, chans.size(), position);
    }//w  w  w .j a v  a  2s .c o m
    final ListItem listItem = getItem(position);
    if (listItem != null) {
        if (listItem.type == ListItem.ITEM_FAVORITE && listItem.threadNumber != null
                && FavoritesStorage.getInstance().canSortManually()) {
            long time = multipleFingersTime
                    + (multipleFingersCountingTime ? System.currentTimeMillis() - multipleFingersStartTime
                            : 0L);
            if (time >= ViewConfiguration.getLongPressTimeout() / 10) {
                int start = position;
                int end = position;
                for (int i = position - 1;; i--) {
                    ListItem checkingListItem = getItem(i);
                    if (checkingListItem == null) {
                        break;
                    }
                    if (checkingListItem.type != ListItem.ITEM_FAVORITE
                            || !listItem.chanName.equals(checkingListItem.chanName)) {
                        start = i + 1;
                        break;
                    }
                }
                for (int i = position + 1;; i++) {
                    ListItem checkingListItem = getItem(i);
                    if (checkingListItem == null) {
                        break;
                    }
                    if (checkingListItem.type != ListItem.ITEM_FAVORITE
                            || !listItem.chanName.equals(checkingListItem.chanName)) {
                        end = i - 1;
                        break;
                    }
                }
                listView.startSorting(start, end, position);
                return true;
            }
        }
        switch (listItem.type) {
        case ListItem.ITEM_PAGE:
        case ListItem.ITEM_FAVORITE: {
            DialogMenu dialogMenu = new DialogMenu(context, new DialogMenu.Callback() {
                @Override
                public void onItemClick(Context context, int id, Map<String, Object> extra) {
                    switch (id) {
                    case MENU_COPY_LINK:
                    case MENU_SHARE_LINK: {
                        ChanLocator locator = ChanLocator.get(listItem.chanName);
                        Uri uri = listItem.isThreadItem()
                                ? locator.safe(true).createThreadUri(listItem.boardName, listItem.threadNumber)
                                : locator.safe(true).createBoardUri(listItem.boardName, 0);
                        if (uri != null) {
                            switch (id) {
                            case MENU_COPY_LINK: {
                                StringUtils.copyToClipboard(context, uri.toString());
                                break;
                            }
                            case MENU_SHARE_LINK: {
                                String subject = listItem.title;
                                if (StringUtils.isEmptyOrWhitespace(subject)) {
                                    subject = uri.toString();
                                }
                                NavigationUtils.share(context, subject, null, uri);
                                break;
                            }
                            }
                        }
                        break;
                    }
                    case MENU_ADD_TO_FAVORITES: {
                        if (listItem.isThreadItem()) {
                            FavoritesStorage.getInstance().add(listItem.chanName, listItem.boardName,
                                    listItem.threadNumber, listItem.title, 0);
                        } else {
                            FavoritesStorage.getInstance().add(listItem.chanName, listItem.boardName);
                        }
                        break;
                    }
                    case MENU_REMOVE_FROM_FAVORITES: {
                        FavoritesStorage.getInstance().remove(listItem.chanName, listItem.boardName,
                                listItem.threadNumber);
                        break;
                    }
                    case MENU_RENAME: {
                        final EditText editText = new SafePasteEditText(context);
                        editText.setSingleLine(true);
                        editText.setText(listItem.title);
                        editText.setSelection(editText.length());
                        LinearLayout linearLayout = new LinearLayout(context);
                        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                        linearLayout.addView(editText, LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT);
                        int padding = context.getResources().getDimensionPixelSize(R.dimen.dialog_padding_view);
                        linearLayout.setPadding(padding, padding, padding, padding);
                        AlertDialog dialog = new AlertDialog.Builder(context).setView(linearLayout)
                                .setTitle(R.string.action_rename)
                                .setNegativeButton(android.R.string.cancel, null)
                                .setPositiveButton(android.R.string.ok, (d, which) -> {
                                    String newTitle = editText.getText().toString();
                                    FavoritesStorage.getInstance().modifyTitle(listItem.chanName,
                                            listItem.boardName, listItem.threadNumber, newTitle, true);
                                }).create();
                        dialog.getWindow()
                                .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                        dialog.show();
                        break;
                    }
                    }
                }
            });
            dialogMenu.addItem(MENU_COPY_LINK, R.string.action_copy_link);
            if (listItem.isThreadItem()) {
                dialogMenu.addItem(MENU_SHARE_LINK, R.string.action_share_link);
            }
            if (listItem.type != ListItem.ITEM_FAVORITE && !FavoritesStorage.getInstance()
                    .hasFavorite(listItem.chanName, listItem.boardName, listItem.threadNumber)) {
                dialogMenu.addItem(MENU_ADD_TO_FAVORITES, R.string.action_add_to_favorites);
            }
            if (listItem.type == ListItem.ITEM_FAVORITE) {
                dialogMenu.addItem(MENU_REMOVE_FROM_FAVORITES, R.string.action_remove_from_favorites);
                if (listItem.threadNumber != null) {
                    dialogMenu.addItem(MENU_RENAME, R.string.action_rename);
                }
            }
            dialogMenu.show();
            return true;
        }
        }
    }
    return false;
}

From source file:im.getsocial.testapp.MainActivity.java

private void changeDisplayName() {
    final View changeDisplayNameView = LayoutInflater.from(this).inflate(R.layout.dialog_change_display_name,
            null, false);//from  w ww  . ja  v  a  2  s .  c  om
    final EditText displayNameEditText = (EditText) changeDisplayNameView.findViewById(R.id.display_name);
    displayNameEditText.setText(UserIdentityUtils.getRandomDisplayName());
    displayNameEditText.setSelection(displayNameEditText.getText().length());
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new AlertDialog.Builder(MainActivity.this).setView(changeDisplayNameView)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            final String name = displayNameEditText.getText().toString();

                            getSocial.getCurrentUser().setDisplayName(name, new OperationVoidCallback() {
                                @Override
                                public void onSuccess() {
                                    logInfoAndToast("User display name was changed to" + name);
                                    updateUserInfoView();
                                }

                                @Override
                                public void onFailure(Exception error) {
                                    logInfoAndToast(
                                            "Cannot change display name. Reason %@" + error.getMessage());
                                }
                            });

                        }
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }).show();
        }
    });
}

From source file:org.anhonesteffort.flock.SubscriptionStripeFragment.java

private void initCardNumberHelper() {
    final EditText cardNumberView = (EditText) subscriptionActivity.findViewById(R.id.card_number);
    final EditText cardExpirationView = (EditText) subscriptionActivity.findViewById(R.id.card_expiration);

    cardNumberView.setOnTouchListener(new View.OnTouchListener() {
        @Override//www. j a  v  a 2  s  .  c om
        public boolean onTouch(View v, MotionEvent event) {
            if (cardNumberView.getText() != null && cardNumberView.getText().toString().contains("*")) {
                cardNumberView.setText("");
            }

            return false;
        }
    });

    if (cardNumberTextWatcher != null)
        cardNumberView.removeTextChangedListener(cardNumberTextWatcher);

    cardNumberTextWatcher = 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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String cardNumber = s.toString().replace(" ", "");
            String formattedCardNumber = "";

            for (int i = 0; i < cardNumber.length(); i++) {
                if (i > 0 && i % 4 == 0)
                    formattedCardNumber += " ";

                formattedCardNumber += cardNumber.charAt(i);
            }

            cardNumberView.removeTextChangedListener(this);
            cardNumberView.setText(formattedCardNumber);
            cardNumberView.setSelection(formattedCardNumber.length());
            cardNumberView.addTextChangedListener(this);

            if (!cardNumber.contains("*") && cardNumber.length() == 16)
                cardExpirationView.requestFocus();
        }
    };

    cardNumberView.addTextChangedListener(cardNumberTextWatcher);
}

From source file:org.numixproject.hermes.activity.ConversationActivity.java

/**
 * Insert a given nick completion into the input line
 *
 * @param input The input line widget, with the incomplete nick selected
 * @param nick The completed nick/*  www  . j ava2 s  . co m*/
 */
private void insertNickCompletion(EditText input, String nick) {
    int start = input.getSelectionStart();
    int end = input.getSelectionEnd();
    nick = removeStatusChar(nick);

    if (start == 0) {
        nick += ":";
    }

    nick += " ";
    input.getText().replace(start, end, nick, 0, nick.length());
    // put cursor after inserted text
    input.setSelection(start + nick.length());
    input.clearComposingText();
    input.post(new Runnable() {
        @Override
        public void run() {
            // make the softkeyboard come up again (only if no hw keyboard is attached)
            AutoCompleteTextView input = (AutoCompleteTextView) findViewById(R.id.input);
            openSoftKeyboard(input);
        }
    });

    input.requestFocus();
}

From source file:org.anhonesteffort.flock.EditAutoRenewActivity.java

private void initCardExpirationHelper() {
    Log.d(TAG, "initCardExpirationHelper()");

    final EditText cardExpirationView = (EditText) findViewById(R.id.card_expiration);
    final EditText cardCvcView = (EditText) findViewById(R.id.card_cvc);
    final CheckBox autoRenewIsEnabled = (CheckBox) findViewById(R.id.checkbox_enable_auto_renew);

    cardExpirationView.setOnTouchListener(new View.OnTouchListener() {
        @Override//from  w w  w.  java 2  s .com
        public boolean onTouch(View v, MotionEvent event) {
            if (autoRenewIsEnabled.isChecked())
                handleInitFormForEditing();

            return false;
        }
    });

    if (cardExpirationTextWatcher != null)
        cardExpirationView.removeTextChangedListener(cardExpirationTextWatcher);

    cardExpirationTextWatcher = 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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String formattedCardExpiration = s.toString();

            if (lastCardExpirationLength <= formattedCardExpiration.length()
                    && formattedCardExpiration.length() == 2) {
                formattedCardExpiration = formattedCardExpiration + "/";
            }

            lastCardExpirationLength = formattedCardExpiration.length();

            cardExpirationView.removeTextChangedListener(this);
            cardExpirationView.setText(formattedCardExpiration);
            cardExpirationView.setSelection(formattedCardExpiration.length());
            cardExpirationView.addTextChangedListener(this);

            if (formattedCardExpiration.length() == 5)
                cardCvcView.requestFocus();
        }
    };

    cardExpirationView.addTextChangedListener(cardExpirationTextWatcher);
}