Android Open Source - advanced-textview Fraction Activity






From Project

Back to project page advanced-textview.

License

The source code is released under:

Apache License

If you think the Android project advanced-textview listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sqisland.android.advanced_textview;
// ww  w .j a  v a2 s .  c o  m
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import android.widget.TextView;

import org.xml.sax.XMLReader;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class FractionActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fraction);

    TextView textView = (TextView) findViewById(R.id.text);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "Nutso2.otf");
    textView.setTypeface(typeface);

    String html = getString(R.string.fraction_text);
    textView.setText(Html.fromHtml(html, null, new FractionTagHandler()));
  }

  // http://stackoverflow.com/questions/4044509/android-how-to-use-the-html-taghandler
  private static class FractionTagHandler implements Html.TagHandler {
    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
      if (!"afrc".equalsIgnoreCase(tag)) {
        return;
      }

      int len = output.length();
      if (opening) {
        output.setSpan(new FractionSpan(), len, len, Spannable.SPAN_MARK_MARK);
      } else {
        Object obj = getLast(output, FractionSpan.class);
        int where = output.getSpanStart(obj);

        output.removeSpan(obj);

        if (where != len) {
          output.setSpan(new FractionSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
      }
    }

    private Object getLast(Editable text, Class kind) {
      Object[] objs = text.getSpans(0, text.length(), kind);

      if (objs.length == 0) {
        return null;
      } else {
        for (int i = objs.length - 1; i >= 0; --i) {
          if(text.getSpanFlags(objs[i]) == Spannable.SPAN_MARK_MARK) {
            return objs[i];
          }
        }
        return null;
      }
    }
  }

  private static class FractionSpan extends MetricAffectingSpan {
    private static final String FONT_FEATURE_SETTINGS = "afrc";

    @Override
    public void updateMeasureState(TextPaint textPaint) {
      textPaint.setFontFeatureSettings(FONT_FEATURE_SETTINGS);
    }

    @Override
    public void updateDrawState(TextPaint textPaint) {
      textPaint.setFontFeatureSettings(FONT_FEATURE_SETTINGS);
    }
  }
}




Java Source Code List

com.sqisland.android.advanced_textview.AboutActivity.java
com.sqisland.android.advanced_textview.AlignmentSpanActivity.java
com.sqisland.android.advanced_textview.AnimatedCompoundDrawableActivity.java
com.sqisland.android.advanced_textview.AnimatedRainbowSpanActivity.java
com.sqisland.android.advanced_textview.ClickableSpanActivity.java
com.sqisland.android.advanced_textview.CustomFontActivity.java
com.sqisland.android.advanced_textview.EmojiActivity.java
com.sqisland.android.advanced_textview.FractionActivity.java
com.sqisland.android.advanced_textview.FromHtmlActivity.java
com.sqisland.android.advanced_textview.GradientTextActivity.java
com.sqisland.android.advanced_textview.LinedPaperActivity.java
com.sqisland.android.advanced_textview.MainActivity.java
com.sqisland.android.advanced_textview.PatternedTextActivity.java
com.sqisland.android.advanced_textview.RainbowSpanActivity.java
com.sqisland.android.advanced_textview.ShadowTextActivity.java
com.sqisland.android.advanced_textview.StyledStringActivity.java
com.sqisland.android.advanced_textview.widget.LinedEditText.java