Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) Azureus Software, Inc, All Rights Reserved.
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;

import android.text.SpannableString;

import android.text.style.CharacterStyle;
import android.text.style.ImageSpan;

public class Main {
    public static void setSpanBetweenTokens(SpannableString ss, String text, String token, CharacterStyle... cs) {
        // Start and end refer to the points where the span will apply
        int tokenLen = token.length();
        int base = 0;

        while (true) {
            int start = text.indexOf(token, base);
            int end = text.indexOf(token, start + tokenLen);

            if (start < 0 || end < 0) {
                break;
            }

            base = end + tokenLen;

            for (CharacterStyle c : cs) {
                ss.setSpan(CharacterStyle.wrap(c), start + tokenLen, end, 0);
            }

            Drawable blankDrawable = new Drawable() {

                @Override
                public void setColorFilter(ColorFilter cf) {
                }

                @Override
                public void setAlpha(int alpha) {
                }

                @Override
                public int getOpacity() {
                    return 0;
                }

                @Override
                public void draw(Canvas canvas) {
                }
            };

            // because AbsoluteSizeSpan(0) doesn't work on older versions
            ss.setSpan(new ImageSpan(blankDrawable), start, start + tokenLen, 0);
            ss.setSpan(new ImageSpan(blankDrawable), end, end + tokenLen, 0);
        }
    }
}