Java Code Examples for android.text.Spannable#charAt()

The following examples show how to use android.text.Spannable#charAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DNSProxyActivity.java    From personaldnsfilter with GNU General Public License v2.0 6 votes vote down vote up
private String getSelectedText(boolean fullLine){

		int start= logOutView.getSelectionStart();
		int end = logOutView.getSelectionEnd();
		String selection = "";
		if (end > start) {
			Spannable text = logOutView.getText();
			if (fullLine) {
				while (text.charAt(start) != '\n' && start > 0)
					start--;
				if (start !=0)
					start++;
				while (end < text.length()-1 && text.charAt(end) != '\n')
					end++;

				logOutView.setSelection(start, end);
			}
			selection = text.subSequence(start, end).toString();
		}
		return selection;
	}
 
Example 2
Source File: DNSProxyActivity.java    From personaldnsfilter with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void run() {

	if (!scroll_locked) {
		m_logStr = m_logStr.replace("FILTERED:",IN_FILTER_PREF);
		m_logStr = m_logStr.replace("ALLOWED:",NO_FILTER_PREF);

		addToLogView(m_logStr);

		int logSize = logOutView.getText().length();

		if (logSize >= 10000) {
			Spannable logStr = logOutView.getText();
			int start = logSize / 2;

			while (logStr.charAt(start) != '\n' && start < logStr.length()-1)
				start++;

			logOutView.setText(logStr.subSequence(start, logStr.length()));

		}

		if (!advancedConfigCheck.isChecked()) { //avoid focus lost when editing advanced settings
			logOutView.setSelection(logOutView.getText().length());
			scrollView.fullScroll(ScrollView.FOCUS_DOWN);
		}
	}
	String version = "<unknown>";
	String connCnt = "-1";
	String lastDNS = "<unknown>";
	try {
		version = CONFIG.getVersion();
		connCnt = CONFIG.openConnectionsCount()+"";
		lastDNS= CONFIG.getLastDNSAddress();
	} catch (IOException e){
		addToLogView(e.toString()+"\n");
	}
	setTitle("personalDNSfilter V" + version + " (Connections:" + connCnt + ")");
	dnsField.setText(lastDNS);
}
 
Example 3
Source File: CharSequenceUtils.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static List<CharSequence> split(Spannable charSequence, char c) {
    List<CharSequence> out = new ArrayList<>();
    int begin = 0;
    for (int i = 0; i < charSequence.length(); ++i) {
        if (charSequence.charAt(i) == c) {
            out.add(StylingHelper.subSequence(charSequence, begin, i));
            begin = ++i;
        }
    }
    if (begin < charSequence.length()) {
        out.add(StylingHelper.subSequence(charSequence, begin, charSequence.length()));
    }
    return out;
}
 
Example 4
Source File: CharSequenceUtils.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public static List<CharSequence> split(Spannable charSequence, char c) {
	List<CharSequence> out = new ArrayList<>();
	int begin = 0;
	for (int i = 0; i < charSequence.length(); ++i) {
		if (charSequence.charAt(i) == c) {
			out.add(StylingHelper.subSequence(charSequence, begin, i));
			begin = ++i;
		}
	}
	if (begin < charSequence.length()) {
		out.add(StylingHelper.subSequence(charSequence, begin, charSequence.length()));
	}
	return out;
}
 
Example 5
Source File: EmojiHandler.java    From umeng_community_android with MIT License 4 votes vote down vote up
/**
 * Convert emoji characters of the given Spannable to the according
 * emojicon.
 * 
 * @param context
 * @param text
 * @param emojiSize
 * @param index
 * @param length
 * @param useSystemDefault
 */
public static void addEmojis(Context context, Spannable text, int emojiSize, int textSize,
        int index, int length, boolean useSystemDefault) {
    if (useSystemDefault) {
        return;
    }

    // It's delete icon 
    if (text.toString().equals(EmojiBorad.DELETE_KEY)) {
        text.setSpan(new EmojiSpan(context, sSoftbanksMap.get(DELETE_KEY), emojiSize,
                textSize), 0, EmojiBorad.DELETE_KEY.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return;
    }

    int textLength = text.length();
    int textLengthToProcessMax = textLength - index;
    int textLengthToProcess = length < 0 || length >= textLengthToProcessMax ? textLength
            : (length + index);

    // remove spans throughout all text
    EmojiSpan[] oldSpans = text.getSpans(0, textLength, EmojiSpan.class);
    for (int i = 0; i < oldSpans.length; i++) {
        text.removeSpan(oldSpans[i]);
    }

    int skip;
    for (int i = index; i < textLengthToProcess; i += skip) {
        skip = 0;
        int icon = 0;
        char c = text.charAt(i);
        if (isSoftBankEmoji(c)) {
            icon = getSoftbankEmojiResource(c);
            skip = icon == 0 ? 0 : 1;
        }

        if (icon == 0) {
            int unicode = Character.codePointAt(text, i);
            skip = Character.charCount(unicode);

            if (unicode > 0xff) {
                icon = getEmojiResource(context, unicode);
            }
        }

        if (icon > 0) {
            text.setSpan(new EmojiSpan(context, icon, emojiSize, textSize), i, i + skip,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 6
Source File: CommentTextView.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
private void startSelection(int x, int y, int start, int end) {
	CharSequence text = getText();
	if (!(text instanceof Spannable)) {
		return;
	}
	Spannable spannable = (Spannable) text;
	int length = spannable.length();
	Layout layout = getLayout();
	if (x != Integer.MAX_VALUE && y != Integer.MAX_VALUE &&
			(start < 0 || end < 0 || start > length || end > length || start >= end)) {
		start = 0;
		end = spannable.length();
		int lx = x - getTotalPaddingLeft();
		int ly = y - getTotalPaddingTop();
		if (lx >= 0 && ly >= 0 && lx < getWidth() - getTotalPaddingRight() &&
				ly < getHeight() - getTotalPaddingBottom()) {
			int offset = layout.getOffsetForHorizontal(layout.getLineForVertical(ly), lx);
			if (offset >= 0 && offset < length) {
				for (int i = offset; i >= 0; i--) {
					if (spannable.charAt(i) == '\n') {
						start = i + 1;
						break;
					}
				}
				for (int i = offset; i < length; i++) {
					if (spannable.charAt(i) == '\n') {
						end = i;
						break;
					}
				}
				if (end > start) {
					String part = spannable.subSequence(start, end).toString();
					Matcher matcher = LIST_PATTERN.matcher(part);
					if (matcher.find()) {
						start += matcher.group().length();
					}
				}
			}
		}
	}
	if (end <= start || start < 0) {
		start = 0;
		end = spannable.length();
	}
	x = getTotalPaddingLeft();
	y = getTotalPaddingRight();
	setSelectionMode(true);
	int finalX = x;
	int finalY = y;
	int finalStart = start;
	int finalEnd = end;
	post(() -> {
		removeCallbacks(resetSelectionRunnable);
		CharSequence newText = getText();
		if (!(newText instanceof Spannable)) {
			resetSelectionRunnable.run();
			return;
		}
		Spannable newSpannable = (Spannable) text;
		int max = newSpannable.length();
		Runnable restoreSelectionRunnable = () ->
				Selection.setSelection(newSpannable, Math.min(finalStart, max), Math.min(finalEnd, max));
		// restoreSelectionRunnable can be nullified during sending motion event
		this.restoreSelectionRunnable = restoreSelectionRunnable;
		sendFakeMotionEvent(MotionEvent.ACTION_DOWN, finalX, finalY);
		sendFakeMotionEvent(MotionEvent.ACTION_UP, finalX, finalY);
		sendFakeMotionEvent(MotionEvent.ACTION_DOWN, finalX, finalY);
		sendFakeMotionEvent(MotionEvent.ACTION_UP, finalX, finalY);
		restoreSelectionRunnable.run();
		postDelayed(resetSelectionRunnable, 500);
	});
}