Android Open Source - wolPi Text View Interactor

From Project

Back to project page wolPi.


The source code is released under:

Apache License

If you think the Android project wolPi 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 de.matthesrieke.wolpi.ui;
//from  w ww. java 2 s.  c o  m
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import android.text.Spannable;
import android.widget.TextView;

import de.matthesrieke.wolpi.Interactor;

 * {@link Interactor} implementation writing
 * content to a {@link TextView}, emulating consolish style.
 * @author matthes rieke
public class TextViewInteractor implements Interactor {
  private Activity parentActivity;
  private TextView targetTextView;

  public TextViewInteractor(Activity parentActivity, TextView targetTextView) {
    this.parentActivity = parentActivity;
    this.targetTextView = targetTextView;

  public boolean requestConfirmation(String request) {
    return ConfirmationDialog.issueConfirmationDialog(request, parentActivity);
  public void onOutput(final String output, final Located l, final Object... inlineStrings) {
    parentActivity.runOnUiThread(new Runnable() {
      public void run() {
        int color = resolveColor(l,  targetTextView.getTextColors().getDefaultColor());
        FormattingTuple result = MessageFormatter.arrayFormat(output, inlineStrings);
        appendColoredText(targetTextView, result.getMessage(), color);
  protected int resolveColor(Located l, int defaultColor) {
    if (l == Located.LOCAL) {
      return defaultColor;
    else {
      return Color.RED;

  public void onOutput(String output, Object... inlineStrings) {
    onOutput(output, Located.LOCAL, inlineStrings);
  public void onError(String error, Located l, Object... inlineErrors) {
    onOutput(error, l, inlineErrors);
  public void onError(String error, Object... inlineErrors) {
    onError(error, Located.LOCAL, inlineErrors);
  private void appendColoredText(TextView tv, String text, int color) {
      int start = tv.getText().length();
      int end = tv.getText().length();

      Spannable spannableText = (Spannable) tv.getText();
      spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0);


Java Source Code List