Search in sources :

Example 1 with CodepointsLengthInputFilter

use of org.telegram.ui.Components.CodepointsLengthInputFilter in project Telegram-FOSS by Telegram-FOSS-Team.

the class ChangeBioActivity method createView.

@Override
public View createView(Context context) {
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    actionBar.setTitle(LocaleController.getString("UserBio", R.string.UserBio));
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {

        @Override
        public void onItemClick(int id) {
            if (id == -1) {
                finishFragment();
            } else if (id == done_button) {
                saveName();
            }
        }
    });
    ActionBarMenu menu = actionBar.createMenu();
    doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56));
    doneButton.setContentDescription(LocaleController.getString("Done", R.string.Done));
    fragmentView = new LinearLayout(context);
    LinearLayout linearLayout = (LinearLayout) fragmentView;
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    fragmentView.setOnTouchListener((v, event) -> true);
    FrameLayout fieldContainer = new FrameLayout(context);
    linearLayout.addView(fieldContainer, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 24, 24, 20, 0));
    firstNameField = new EditTextBoldCursor(context);
    firstNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    firstNameField.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
    firstNameField.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
    firstNameField.setBackgroundDrawable(Theme.createEditTextDrawable(context, false));
    firstNameField.setMaxLines(4);
    firstNameField.setPadding(AndroidUtilities.dp(LocaleController.isRTL ? 24 : 0), 0, AndroidUtilities.dp(LocaleController.isRTL ? 0 : 24), AndroidUtilities.dp(6));
    firstNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
    firstNameField.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    firstNameField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    firstNameField.setImeOptions(EditorInfo.IME_ACTION_DONE);
    InputFilter[] inputFilters = new InputFilter[1];
    inputFilters[0] = new CodepointsLengthInputFilter(70) {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if (source != null && source.length() > 0 && TextUtils.indexOf(source, '\n') == source.length() - 1) {
                doneButton.performClick();
                return "";
            }
            CharSequence result = super.filter(source, start, end, dest, dstart, dend);
            if (result != null && source != null && result.length() != source.length()) {
                Vibrator v = (Vibrator) getParentActivity().getSystemService(Context.VIBRATOR_SERVICE);
                if (v != null) {
                    v.vibrate(200);
                }
                AndroidUtilities.shakeView(checkTextView, 2, 0);
            }
            return result;
        }
    };
    firstNameField.setFilters(inputFilters);
    firstNameField.setMinHeight(AndroidUtilities.dp(36));
    firstNameField.setHint(LocaleController.getString("UserBio", R.string.UserBio));
    firstNameField.setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
    firstNameField.setCursorSize(AndroidUtilities.dp(20));
    firstNameField.setCursorWidth(1.5f);
    firstNameField.setOnEditorActionListener((textView, i, keyEvent) -> {
        if (i == EditorInfo.IME_ACTION_DONE && doneButton != null) {
            doneButton.performClick();
            return true;
        }
        return false;
    });
    firstNameField.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) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            checkTextView.setNumber(70 - Character.codePointCount(s, 0, s.length()), true);
        }
    });
    fieldContainer.addView(firstNameField, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 0, 0, 4, 0));
    checkTextView = new NumberTextView(context);
    checkTextView.setCenterAlign(true);
    checkTextView.setTextSize(15);
    checkTextView.setNumber(70, false);
    checkTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText4));
    checkTextView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
    fieldContainer.addView(checkTextView, LayoutHelper.createFrame(20, 20, LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT, 0, 4, 4, 0));
    helpTextView = new TextView(context);
    helpTextView.setFocusable(true);
    helpTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    helpTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText8));
    helpTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
    helpTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("UserBioInfo", R.string.UserBioInfo)));
    linearLayout.addView(helpTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 10, 24, 0));
    TLRPC.UserFull userFull = MessagesController.getInstance(currentAccount).getUserFull(UserConfig.getInstance(currentAccount).getClientUserId());
    if (userFull != null && userFull.about != null) {
        firstNameField.setText(userFull.about);
        firstNameField.setSelection(firstNameField.length());
    }
    return fragmentView;
}
Also used : CodepointsLengthInputFilter(org.telegram.ui.Components.CodepointsLengthInputFilter) InputFilter(android.text.InputFilter) CodepointsLengthInputFilter(org.telegram.ui.Components.CodepointsLengthInputFilter) ActionBarMenu(org.telegram.ui.ActionBar.ActionBarMenu) Spanned(android.text.Spanned) TLRPC(org.telegram.tgnet.TLRPC) FrameLayout(android.widget.FrameLayout) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) NumberTextView(org.telegram.ui.Components.NumberTextView) TextView(android.widget.TextView) NumberTextView(org.telegram.ui.Components.NumberTextView) Vibrator(android.os.Vibrator) EditTextBoldCursor(org.telegram.ui.Components.EditTextBoldCursor) ActionBar(org.telegram.ui.ActionBar.ActionBar) LinearLayout(android.widget.LinearLayout)

Aggregations

Vibrator (android.os.Vibrator)1 Editable (android.text.Editable)1 InputFilter (android.text.InputFilter)1 Spanned (android.text.Spanned)1 TextWatcher (android.text.TextWatcher)1 FrameLayout (android.widget.FrameLayout)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 TLRPC (org.telegram.tgnet.TLRPC)1 ActionBar (org.telegram.ui.ActionBar.ActionBar)1 ActionBarMenu (org.telegram.ui.ActionBar.ActionBarMenu)1 CodepointsLengthInputFilter (org.telegram.ui.Components.CodepointsLengthInputFilter)1 EditTextBoldCursor (org.telegram.ui.Components.EditTextBoldCursor)1 NumberTextView (org.telegram.ui.Components.NumberTextView)1