Example usage for android.text Editable replace

List of usage examples for android.text Editable replace

Introduction

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

Prototype

public Editable replace(int st, int en, CharSequence text);

Source Link

Document

Convenience for replace(st, en, text, 0, text.length())

Usage

From source file:Main.java

public static void convertToLowercase(Editable s) {
    String lowerCase = s.toString().toLowerCase();
    if (!lowerCase.equals(s.toString())) {
        s.replace(0, s.length(), lowerCase);
    }/*from w  w w  .jav a 2 s  .c o  m*/
}

From source file:Main.java

/**
 * Substitute the selection of given {@link EditText} with specified text.<br />
 * If no text is selected then specified text will be inserted at current cursor position.<br />
 * Newly inserted text will be selected.
 * @param editText Target {@link EditText}.
 * @param txt Text to insert.// w  ww  .j  a v a 2  s  .  com
 * @param select {@code true} to select newly added text.
 */
public static void setTextInsideSelection(EditText editText, String txt, boolean select) {
    Editable editable;
    int selStart;
    int selEnd;

    editable = editText.getText();

    selStart = editText.getSelectionStart();
    selEnd = editText.getSelectionEnd();
    if (selStart > selEnd) {
        int tmp = selStart;
        selStart = selEnd;
        selEnd = tmp;
    }

    editable.replace(selStart, selEnd, txt);

    if (select) {
        editText.setSelection(selStart, selStart + txt.length());
    } else {
        editText.setSelection(selStart + txt.length());
    }
}

From source file:com.duy.pascal.ui.editor.view.CodeSuggestsEditText.java

protected void replaceText(CharSequence text) {
    clearComposingText();//from www. j a va 2 s .co  m

    int end = getSelectionEnd();
    int start;
    if (SymbolsTokenizer.TOKEN.contains(text.toString().trim())) {
        start = end;
    } else {
        start = mTokenizer.findTokenStart(getText(), end);
    }
    start = Math.max(start, 0);
    end = Math.max(end, 0);
    Editable editable = getText();
    String original = TextUtils.substring(editable, start, end);

    QwertyKeyListener.markAsReplaced(editable, start, end, original);
    editable.replace(start, end, mTokenizer.terminateToken(text));
}

From source file:org.dalol.orthodoxmezmurmedia.utilities.widgets.AmharicKeyboardView.java

private void handleModifiers(List<String> typographyList) {
    modifiersContainer.removeAllViews();

    for (int i = 0; i < typographyList.size(); i++) {
        String typography = typographyList.get(i);
        TextView modifierKey = new TextView(getContext());
        modifierKey.setText(typography);
        modifierKey.setTextColor(Color.WHITE);
        modifierKey.setIncludeFontPadding(false);
        modifierKey.setTypeface(mCharTypeface, Typeface.BOLD);
        modifierKey.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        modifierKey.setBackgroundDrawable(
                ContextCompat.getDrawable(getContext(), R.drawable.keyboard_modifierkey_bg));
        modifierKey.setTextSize(18f);//  ww  w .j a v a2s . co  m
        modifierKey.setGravity(Gravity.CENTER);

        modifierKey.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditText == null) {
                    return;
                }
                TextView textView = (TextView) v;
                if (!mEditText.isFocused())
                    mEditText.requestFocus();
                Editable editableText = mEditText.getText();
                int start = mEditText.getSelectionStart();
                if (start == 0)
                    return;

                CharSequence textViewText = textView.getText();
                if (mEnableModifierFlag) {
                    mEnableModifierFlag = false;
                    editableText.replace(start - 1, start, textViewText);
                } else {
                    editableText.insert(start, textViewText);
                }
            }
        });

        LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
        int margin = getCustomSize(1.5f);
        params.setMargins(margin, margin, margin, margin);
        modifiersContainer.addView(modifierKey, params);
    }
}

From source file:jp.gr.java_conf.piropiroping.bluetoothcommander.MainActivity.java

@Override
public void onSelectCommandClick(View view) {
    Button selectedCommand = (Button) view;
    String commandName = selectedCommand.getText().toString();
    Editable editable = mOutEditText.getText();
    int start = mOutEditText.getSelectionStart();
    int end = mOutEditText.getSelectionEnd();
    editable.replace(Math.min(start, end), Math.max(start, end), STX + commandName + ETX);
    setCommandData();/*w w  w.j  av a  2s .c om*/
}

From source file:org.tpmkranz.notifyme.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.main_menu_checkaccessibility:
        prefs.setPrevVersion(0);/*from www .ja  v  a 2 s  .  c  o m*/
        ((TemporaryStorage) getApplicationContext()).accessGranted(false);
        finish();
        startActivity(getIntent());
        return true;
    case R.id.main_menu_popup:
        final View view = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.main_menu_popup, null);
        if (android.os.Build.VERSION.SDK_INT >= 11)
            view.findViewById(R.id.main_menu_popup_background).setVisibility(View.GONE);
        ((CheckBox) view.findViewById(R.id.main_menu_popup_background_checkbox))
                .setChecked(prefs.isBackgroundColorInverted());
        view.findViewById(R.id.main_menu_popup_background_caption)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CheckBox) view.findViewById(R.id.main_menu_popup_background_checkbox)).toggle();
                    }
                });
        ((CheckBox) view.findViewById(R.id.main_menu_popup_orientation_checkbox))
                .setChecked(prefs.isOrientationFixed());
        view.findViewById(R.id.main_menu_popup_orientation_caption)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CheckBox) view.findViewById(R.id.main_menu_popup_orientation_checkbox)).toggle();
                    }
                });
        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r)).setMax(255);
        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r))
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        if (fromUser) {
                            ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_r))
                                    .setText((progress == 0 ? "" : String.valueOf(progress)));
                        }

                        ((ImageView) view.findViewById(R.id.main_menu_popup_color_preview))
                                .setImageDrawable(new ColorDrawable(Color.rgb(progress,
                                        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g))
                                                .getProgress(),
                                        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b))
                                                .getProgress())));
                    }
                });
        ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_r))
                .addTextChangedListener(new TextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        try {
                            if (Integer.parseInt(s.toString()) > 255) {
                                s.replace(0, s.length(), "255");
                                return;
                            } else if (Integer.parseInt(s.toString()) < 0) {
                                s.replace(0, s.length(), "0");
                                return;
                            }
                            ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r))
                                    .setProgress(Integer.parseInt(s.toString()));
                        } catch (Exception e) {
                            ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r)).setProgress(0);
                            s.clear();
                        }
                    }

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

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                });
        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g)).setMax(255);
        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g))
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        if (fromUser) {
                            ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_g))
                                    .setText((progress == 0 ? "" : String.valueOf(progress)));
                        }
                        ((ImageView) view.findViewById(R.id.main_menu_popup_color_preview))
                                .setImageDrawable(new ColorDrawable(Color.rgb(
                                        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r))
                                                .getProgress(),
                                        progress,
                                        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b))
                                                .getProgress())));
                    }
                });
        ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_g))
                .addTextChangedListener(new TextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        try {
                            if (Integer.parseInt(s.toString()) > 255) {
                                s.replace(0, s.length(), "255");
                                return;
                            } else if (Integer.parseInt(s.toString()) < 0) {
                                s.replace(0, s.length(), "0");
                                return;
                            }
                            ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g))
                                    .setProgress(Integer.parseInt(s.toString()));
                        } catch (Exception e) {
                            ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g)).setProgress(0);
                            s.clear();
                        }
                    }

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

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                });
        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b)).setMax(255);
        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b))
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        if (fromUser) {
                            ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_b))
                                    .setText((progress == 0 ? "" : String.valueOf(progress)));
                        }
                        ((ImageView) view.findViewById(R.id.main_menu_popup_color_preview))
                                .setImageDrawable(new ColorDrawable(Color.rgb(
                                        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r))
                                                .getProgress(),
                                        ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g))
                                                .getProgress(),
                                        progress)));
                    }
                });
        ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_b))
                .addTextChangedListener(new TextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        try {
                            if (Integer.parseInt(s.toString()) > 255) {
                                s.replace(0, s.length(), "255");
                                return;
                            } else if (Integer.parseInt(s.toString()) < 0) {
                                s.replace(0, s.length(), "0");
                                return;
                            }
                            ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b))
                                    .setProgress(Integer.parseInt(s.toString()));
                        } catch (Exception e) {
                            ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b)).setProgress(0);
                            s.clear();
                        }
                    }

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

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                });
        ((ImageView) view.findViewById(R.id.main_menu_popup_color_preview))
                .setImageDrawable(new ColorDrawable(Color.rgb(prefs.getSliderBackgroundR(),
                        prefs.getSliderBackgroundG(), prefs.getSliderBackgroundB())));
        ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_r))
                .setText(String.valueOf(prefs.getSliderBackgroundR()));
        ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_g))
                .setText(String.valueOf(prefs.getSliderBackgroundG()));
        ((EditText) view.findViewById(R.id.main_menu_popup_color_edit_b))
                .setText(String.valueOf(prefs.getSliderBackgroundB()));
        ((EditText) view.findViewById(R.id.main_menu_popup_timeout_editor)).setText(
                (prefs.getScreenTimeout() == 0L ? "" : String.valueOf(prefs.getScreenTimeout() / 1000L)));
        ((EditText) view.findViewById(R.id.main_menu_popup_timeout_editor))
                .addTextChangedListener(new TextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        if (s.toString().equals(""))
                            return;
                        try {
                            if (Long.parseLong(s.toString()) > 9999L)
                                s.replace(0, s.length(), "9999");
                            else if (Long.parseLong(s.toString()) < 0L)
                                s.replace(0, s.length(), "0");
                        } catch (Exception e) {
                            s.clear();
                        }
                    }

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

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                });
        ((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                .setChecked(prefs.isInterfaceSlider());
        ((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(0)
                                .setEnabled(((CheckBox) v).isChecked());
                        for (int i = 0; i < 3; i++) {
                            ((ViewGroup) ((ViewGroup) ((ViewGroup) view
                                    .findViewById(R.id.main_menu_popup_color)).getChildAt(1)).getChildAt(i))
                                            .getChildAt(0).setEnabled(((CheckBox) v).isChecked());
                            ((ViewGroup) ((ViewGroup) ((ViewGroup) view
                                    .findViewById(R.id.main_menu_popup_color)).getChildAt(1)).getChildAt(i))
                                            .getChildAt(1).setEnabled(((CheckBox) v).isChecked());
                        }
                        ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(2)
                                .setVisibility((((CheckBox) v).isChecked() ? View.VISIBLE : View.INVISIBLE));
                    }
                });
        ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(0).setEnabled(
                ((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox)).isChecked());
        for (int i = 0; i < 3; i++) {
            ((ViewGroup) ((ViewGroup) ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(1))
                    .getChildAt(i)).getChildAt(0)
                            .setEnabled(((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                                    .isChecked());
            ((ViewGroup) ((ViewGroup) ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(1))
                    .getChildAt(i)).getChildAt(1)
                            .setEnabled(((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                                    .isChecked());
        }
        ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(2).setVisibility(
                (((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox)).isChecked()
                        ? View.VISIBLE
                        : View.INVISIBLE));
        view.findViewById(R.id.main_menu_popup_interface_caption)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox)).toggle();
                        ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(0).setEnabled(
                                ((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                                        .isChecked());
                        for (int i = 0; i < 3; i++) {
                            ((ViewGroup) ((ViewGroup) ((ViewGroup) view
                                    .findViewById(R.id.main_menu_popup_color)).getChildAt(1)).getChildAt(i))
                                            .getChildAt(0)
                                            .setEnabled(((CheckBox) view
                                                    .findViewById(R.id.main_menu_popup_interface_checkbox))
                                                            .isChecked());
                            ((ViewGroup) ((ViewGroup) ((ViewGroup) view
                                    .findViewById(R.id.main_menu_popup_color)).getChildAt(1)).getChildAt(i))
                                            .getChildAt(1)
                                            .setEnabled(((CheckBox) view
                                                    .findViewById(R.id.main_menu_popup_interface_checkbox))
                                                            .isChecked());
                        }
                        ((ViewGroup) view.findViewById(R.id.main_menu_popup_color)).getChildAt(2).setVisibility(
                                (((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                                        .isChecked() ? View.VISIBLE : View.INVISIBLE));
                    }
                });
        new AlertDialog.Builder(this).setView(view)
                .setPositiveButton(R.string.main_menu_popup_save_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        prefs.setBackgroundColorInverted(
                                ((CheckBox) view.findViewById(R.id.main_menu_popup_background_checkbox))
                                        .isChecked());
                        prefs.setInterfaceSlider(
                                ((CheckBox) view.findViewById(R.id.main_menu_popup_interface_checkbox))
                                        .isChecked());
                        prefs.setSliderBackground(
                                ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_r))
                                        .getProgress(),
                                ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_g))
                                        .getProgress(),
                                ((SeekBar) view.findViewById(R.id.main_menu_popup_color_slider_b))
                                        .getProgress());
                        prefs.setScreenTimeout(
                                (((EditText) view.findViewById(R.id.main_menu_popup_timeout_editor)).getText()
                                        .toString().equals("")
                                                ? 0L
                                                : Long.parseLong(((EditText) view
                                                        .findViewById(R.id.main_menu_popup_timeout_editor))
                                                                .getText().toString())
                                                        * 1000L));
                        prefs.setOrientationFixed(
                                ((CheckBox) view.findViewById(R.id.main_menu_popup_orientation_checkbox))
                                        .isChecked());
                    }
                }).setNegativeButton(R.string.main_menu_popup_cancel_button, null).show();
        return true;
    case R.id.main_menu_help:
        new AlertDialog.Builder(this).setMessage(R.string.main_menu_help_message)
                .setTitle(R.string.main_menu_help_title)
                .setPositiveButton(R.string.main_menu_help_ok_button, null).setNegativeButton(
                        R.string.main_menu_help_question_button, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivity(new Intent(Intent.ACTION_VIEW,
                                        Uri.parse("http://forum.xda-developers.com/showthread.php?t=2173226"))
                                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                            }
                        })
                .show();
        return true;
    case R.id.main_menu_about:
        ViewGroup about = (ViewGroup) ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.main_menu_about, null);
        ((TextView) about.getChildAt(0)).setMovementMethod(LinkMovementMethod.getInstance());
        new AlertDialog.Builder(this).setView(about).setTitle(R.string.main_menu_about_title)
                .setPositiveButton(R.string.main_menu_about_ok_button, null).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.FanWallPlugin.java

@Override
public void afterTextChanged(Editable editable) {
    if (editable.length() > 150) {
        editable.replace(editable.length() - 1, editable.length(), "");
        Toast.makeText(this, R.string.romanblack_fanwall_alert_big_text, Toast.LENGTH_SHORT).show();
    }/*from  w w  w  . j  av a 2  s  .c o m*/
}

From source file:com.krayzk9s.imgurholo.ui.SingleImageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    final ImgurHoloActivity activity = (ImgurHoloActivity) getActivity();
    final SharedPreferences settings = activity.getApiCall().settings;
    sort = settings.getString("CommentSort", "Best");
    boolean newData = true;
    if (commentData != null) {
        newData = false;//from  w ww.jav a 2s  .c  o  m
    }

    mainView = inflater.inflate(R.layout.single_image_layout, container, false);
    String[] mMenuList = getResources().getStringArray(R.array.emptyList);
    if (commentAdapter == null)
        commentAdapter = new CommentAdapter(mainView.getContext());
    commentLayout = (ListView) mainView.findViewById(R.id.comment_thread);
    commentLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    if (settings.getString("theme", MainActivity.HOLO_LIGHT).equals(MainActivity.HOLO_LIGHT))
        imageLayoutView = (LinearLayout) View.inflate(getActivity(), R.layout.image_view, null);
    else
        imageLayoutView = (LinearLayout) View.inflate(getActivity(), R.layout.dark_image_view, null);

    mPullToRefreshLayout = (PullToRefreshLayout) mainView.findViewById(R.id.ptr_layout);
    ActionBarPullToRefresh.from(getActivity())
            // Mark All Children as pullable
            .allChildrenArePullable()
            // Set the OnRefreshListener
            .listener(this)
            // Finally commit the setup to our PullToRefreshLayout
            .setup(mPullToRefreshLayout);
    if (savedInstanceState != null && newData) {
        imageData = savedInstanceState.getParcelable("imageData");
        inGallery = savedInstanceState.getBoolean("inGallery");
    }
    LinearLayout layout = (LinearLayout) imageLayoutView.findViewById(R.id.image_buttons);
    TextView imageDetails = (TextView) imageLayoutView.findViewById(R.id.single_image_details);
    layout.setVisibility(View.VISIBLE);
    ImageButton imageFullscreen = (ImageButton) imageLayoutView.findViewById(R.id.fullscreen);
    imageUpvote = (ImageButton) imageLayoutView.findViewById(R.id.rating_good);
    imageDownvote = (ImageButton) imageLayoutView.findViewById(R.id.rating_bad);
    ImageButton imageFavorite = (ImageButton) imageLayoutView.findViewById(R.id.rating_favorite);
    imageComment = (ImageButton) imageLayoutView.findViewById(R.id.comment);
    ImageButton imageUser = (ImageButton) imageLayoutView.findViewById(R.id.user);
    imageScore = (TextView) imageLayoutView.findViewById(R.id.single_image_score);
    TextView imageInfo = (TextView) imageLayoutView.findViewById(R.id.single_image_info);
    Log.d("imageData", imageData.getJSONObject().toString());
    if (imageData.getJSONObject().has("ups")) {
        imageUpvote.setVisibility(View.VISIBLE);
        imageDownvote.setVisibility(View.VISIBLE);
        imageScore.setVisibility(View.VISIBLE);
        imageComment.setVisibility(View.VISIBLE);
        ImageUtils.updateImageFont(imageData, imageScore);
    }
    imageInfo.setVisibility(View.VISIBLE);
    ImageUtils.updateInfoFont(imageData, imageInfo);
    imageUser.setVisibility(View.VISIBLE);
    imageFavorite.setVisibility(View.VISIBLE);
    try {
        if (!imageData.getJSONObject().has("account_url")
                || imageData.getJSONObject().getString("account_url").equals("null")
                || imageData.getJSONObject().getString("account_url").equals("[deleted]"))
            imageUser.setVisibility(View.GONE);
        if (!imageData.getJSONObject().has("vote")) {
            imageUpvote.setVisibility(View.GONE);
            imageDownvote.setVisibility(View.GONE);
        } else {
            if (imageData.getJSONObject().getString("vote") != null
                    && imageData.getJSONObject().getString("vote").equals("up"))
                imageUpvote.setImageResource(R.drawable.green_rating_good);
            else if (imageData.getJSONObject().getString("vote") != null
                    && imageData.getJSONObject().getString("vote").equals("down"))
                imageDownvote.setImageResource(R.drawable.red_rating_bad);
        }
        if (imageData.getJSONObject().getString("favorite") != null
                && imageData.getJSONObject().getBoolean("favorite"))
            imageFavorite.setImageResource(R.drawable.green_rating_favorite);
    } catch (JSONException e) {
        Log.e("Error!", e.toString());
    }
    imageFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.favoriteImage(singleImageFragment, imageData, (ImageButton) view, activity.getApiCall());
        }
    });
    imageUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.gotoUser(singleImageFragment, imageData);
        }
    });
    imageComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Activity activity = getActivity();
            final EditText newBody = new EditText(activity);
            newBody.setHint(R.string.body_hint_body);
            newBody.setLines(3);
            final TextView characterCount = new TextView(activity);
            characterCount.setText("140");
            LinearLayout commentReplyLayout = new LinearLayout(activity);
            newBody.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) {
                    characterCount.setText(String.valueOf(140 - charSequence.length()));
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    for (int i = editable.length(); i > 0; i--) {
                        if (editable.subSequence(i - 1, i).toString().equals("\n"))
                            editable.replace(i - 1, i, "");
                    }
                }
            });
            commentReplyLayout.setOrientation(LinearLayout.VERTICAL);
            commentReplyLayout.addView(newBody);
            commentReplyLayout.addView(characterCount);
            new AlertDialog.Builder(activity).setTitle(R.string.dialog_comment_on_image_title)
                    .setView(commentReplyLayout)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            if (newBody.getText() != null && newBody.getText().toString().length() < 141) {
                                HashMap<String, Object> commentMap = new HashMap<String, Object>();
                                try {
                                    commentMap.put("comment", newBody.getText().toString());
                                    commentMap.put("image_id", imageData.getJSONObject().getString("id"));
                                    Fetcher fetcher = new Fetcher(singleImageFragment, "3/comment/",
                                            ApiCall.POST, commentMap,
                                            ((ImgurHoloActivity) getActivity()).getApiCall(), POSTCOMMENT);
                                    fetcher.execute();
                                } catch (JSONException e) {
                                    Log.e("Error!", e.toString());
                                }
                            }
                        }
                    }).setNegativeButton(R.string.dialog_answer_cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // Do nothing.
                        }
                    }).show();
        }
    });
    imageUpvote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.upVote(singleImageFragment, imageData, imageUpvote, imageDownvote,
                    activity.getApiCall());
            ImageUtils.updateImageFont(imageData, imageScore);
        }
    });
    imageDownvote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.downVote(singleImageFragment, imageData, imageUpvote, imageDownvote,
                    activity.getApiCall());
            ImageUtils.updateImageFont(imageData, imageScore);
        }
    });
    if (popupWindow != null) {
        popupWindow.dismiss();
    }
    popupWindow = new PopupWindow();
    imageFullscreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.fullscreen(singleImageFragment, imageData, popupWindow, mainView);
        }
    });
    ArrayAdapter<String> tempAdapter = new ArrayAdapter<String>(mainView.getContext(),
            R.layout.drawer_list_item, mMenuList);
    Log.d("URI", "YO I'M IN YOUR SINGLE FRAGMENT gallery:" + inGallery);
    imageView = (ImageView) imageLayoutView.findViewById(R.id.single_image_view);
    loadImage();
    TextView imageTitle = (TextView) imageLayoutView.findViewById(R.id.single_image_title);
    TextView imageDescription = (TextView) imageLayoutView.findViewById(R.id.single_image_description);

    try {
        String size = String
                .valueOf(NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_WIDTH)))
                + "x"
                + NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_HEIGHT))
                + " (" + NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_SIZE))
                + "B)";
        String initial = imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE) + " "
                + Html.fromHtml("&#8226;") + " " + size + " " + Html.fromHtml("&#8226;") + " " + "Views: "
                + NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_VIEWS));
        imageDetails.setText(initial);
        Log.d("imagedata", imageData.getJSONObject().toString());
        if (!imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TITLE).equals("null"))
            imageTitle.setText(imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TITLE));
        else
            imageTitle.setVisibility(View.GONE);
        if (!imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_DESCRIPTION).equals("null")) {
            imageDescription
                    .setText(imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_DESCRIPTION));
            imageDescription.setVisibility(View.VISIBLE);
        } else
            imageDescription.setVisibility(View.GONE);
        commentLayout.addHeaderView(imageLayoutView);
        commentLayout.setAdapter(tempAdapter);
    } catch (JSONException e) {
        Log.e("Text Error!", e.toString());
    }
    if ((savedInstanceState == null || commentData == null) && newData) {
        commentData = new JSONParcelable();
        getComments();
        commentLayout.setAdapter(commentAdapter);
    } else if (newData) {
        commentArray = savedInstanceState.getParcelableArrayList("commentData");
        commentAdapter.addAll(commentArray);
        commentLayout.setAdapter(commentAdapter);
        commentAdapter.notifyDataSetChanged();
    } else if (commentArray != null) {
        commentAdapter.addAll(commentArray);
        commentLayout.setAdapter(commentAdapter);
        commentAdapter.notifyDataSetChanged();
    }
    return mainView;
}

From source file:com.ruesga.rview.widget.TagEditTextView.java

private void createChip(Editable s, boolean nextIsTag) {
    int start = mTagList.size();
    int end = s.length() + (nextIsTag ? -1 : 0);
    String tagText = s.subSequence(start, end).toString().trim();
    tagText = NON_UNICODE_CHAR_PATTERN.matcher(tagText).replaceAll("");
    if (tagText.isEmpty() || tagText.length() <= 1) {
        // User is still writing
        return;/*from w w w.  j  a  v a 2  s  .  com*/
    }
    String charText = tagText.substring(0, 1);
    if (!VALID_TAGS.contains(charText) || (charText.charAt(0) == VALID_TAGS.charAt(1) && !mSupportsUserTags)) {
        char tag = mDefaultTagMode == TAG_MODE.HASH ? VALID_TAGS.charAt(0) : VALID_TAGS.charAt(1);
        tagText = tag + tagText;
    }

    // Replace the new tag
    s.replace(start, end, CHIP_REPLACEMENT_CHAR);

    // Create the tag and its spannable
    final Tag tag = new Tag();
    tag.mTag = NON_UNICODE_CHAR_PATTERN.matcher(tagText).replaceAll("");
    Bitmap b = createTagChip(tag);
    ImageSpan span = new ImageSpan(getContext(), b);
    s.setSpan(span, start, start + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tag.w = b.getWidth();
    tag.h = b.getHeight();
    mTagList.add(tag);

    notifyTagCreated(tag);
}

From source file:org.zywx.wbpalmstar.plugin.chatkeyboard.ACEChatKeyboardView.java

public void insertTextByKeyword(String keyword, String insertText, String color, boolean isReplaceKeyword) {
    if (TextUtils.isEmpty(keyword) || TextUtils.isEmpty(insertText)) {
        return;/*from  w ww  .  j  av a2s . c o  m*/
    }
    if (TextUtils.isEmpty(color)) {
        color = "#507daf";
    }
    Editable editable = mEditText.getText();
    int lastAtPosition = mLastAtPosition;
    if (editable.length() < keyword.length() || editable.length() < lastAtPosition + keyword.length()) {
        return;
    }
    if (!keyword.equals(editable.subSequence(lastAtPosition, lastAtPosition + keyword.length()).toString())) {
        if (!editable.toString().contains(keyword)) {
            return;
        }
        lastAtPosition = editable.toString().lastIndexOf(keyword);
    }
    if (isReplaceKeyword) {
        editable.replace(lastAtPosition, lastAtPosition + keyword.length(),
                Html.fromHtml("<font color=\"" + color + "\">" + insertText + "</font>"));
    } else {
        editable.insert(lastAtPosition + keyword.length(),
                Html.fromHtml("<font color=\"" + color + "\">" + insertText + "</font>"));
    }
    lastAtPosition += insertText.length() + (isReplaceKeyword ? 0 : keyword.length());
    mEditText.setSelection(lastAtPosition);
}